Python基础一

一、变量

声明变量

name = 'kevin'

上述代码声明了一个变量,变量名为: name,变量name的值为:'kevin'

变量定义的规则:

    • 变量名只能是 字母、数字或下划线的任意组合
    • 变量名的第一个字符不能是数字
    • 以下关键字不能声明为变量名
      ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
 1 #变量是存东西的,存到内存里,给其它调用,由数字字母或者下划线表示,首字母不能为数字
 2 name = "Kevin Zou"
 3 print("My name is ",name)
 4 name2 = name
 5 print(name,name2)
 6 name = "test"
 7 print(name)
 8 
 9 gf_of_kevin = 'cc'  #注意命名规范,不要用单个字母,拼音,中文
10 print(gf_of_kevin)
11 
12 PIE = 3.14    #常量
13 print(PIE)
14 
15 #当行注释
16 #''' '''多行注释或者打印多行
17 msg = ''' hello
18 test'''
19 print(msg)
View Code

 

二、字符编码

python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill)

ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256-1,所以,ASCII码最多只能表示 255 个符号。

一个字符在ascii码占用一个字节,占8位,在unicode占2个或者2个以上字节
ASCII 255 1bytes (字节)
-->> 1980 gb2312 7445个 -->>1995 GBK1.0 21866 -->>2000 GB18030 27484
-->> unicode 一个字符至少2个字节 -->>UTF-8 ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存

python3.X默认采用utf-8

三、用户输入

输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:

import getpass
username = input("username:")
password = getpass.getpass("password:")
print(username,password)

三种格式化输出:

 1 #Author:Kevin Zou
 2 
 3 #输入
 4 '''
 5 username = input("username:")
 6 password = input("password:")
 7 print(username,password)
 8 '''
 9 
10 #python2.x raw_input 等同于python3.x input,python2.x input代表你输入什么格式就是什么格式
11 name = input("name:")
12 #age = input("age:")
13 age = int (input("age:"))   #int()string类型转化成int类型,str() 转化为string类型
14 #print(type(age))
15 job = input("job:")
16 salary = input("salary:")
17 
18 #格式化输出 1 :字符串拼接用+,不建议使用,在内存里占用空间大
19 #info1 = ''' ------info of ''' + name + '''-----
20 #name:''' + name + '''
21 #age:'''+ age + '''
22 #job:''' + job + '''
23 #salary:''' + salary
24 
25 #格式化输出 2:s:字符串 d:数字 f:浮点 d/f可以帮助检测验证数据类型
26 #info ='''------info of %s-------
27 #Name:%s
28 #Age:%d
29 #Job:%s
30 #Salary:%s''' % (name,name,age,job,salary)
31 
32 #格式化输出 3:{} .format
33 info = '''----info of {_name}----
34 Name:{_name}
35 Age:{_age}
36 Job:{_job}
37 Salary:{_salary}'''.format(_name = name,_age = age,_job = job,_salary = salary)
38 
39 #另一个写法
40 info2 = info = '''----info of {0}----
41 Name:{0}
42 Age:{1}
43 Job:{2}
44 Salary:{3}'''.format(name,age,job,salary)
45 
46 print(info2)
View Code

 

四、if ... else示例

#Author:Kevin Zou
kevin_age = 28
count = 0
while count < 3:
    guess_age = int(input("Guess age:"))
    if guess_age == kevin_age:
        print("You get it,kevin's age is",guess_age)
        break                  #break跳出循环;continue跳出当前循环,继续执行下一个循环
    elif guess_age < kevin_age:
        print("Guess wrong,think bigger...")
    else:
        print("Guess wrong,think smaller...")
    count += 1
    if count == 3:
        continue_guess = input("do you want to continue guess ?")
        if continue_guess != 'n':      
            count = 0
#else:                        #当while 循环全部执行后才执行else语句
   # print("fuck,you guess many times")
View Code

 

五、循环

while示例

#Author:Kevin Zou
age_of_kevin = 27
count = 0
'''
while True:
    if count == 100:
        print("ok,count is ",count)
        break
     print("count:",count)
    count = count + 1   #等同于 count += 1
'''

'''
while count < 3:
    print("count:",count)
    count += 1
'''
#while esle 写法
while count < 3:
    guess_age = int(input("guess age:"))
    if guess_age == age_of_kevin:
        print("yes,you get it,kevin's age is %s" % (guess_age))
        break
    elif guess_age > age_of_kevin:
        print("No, you guess error.Think smaller...")
    else:
        print('No,you guess error.Think bigger...')
    count = count + 1
else:
    print("bye! you have no choice!")
View Code

for示例

#Author:Kevin Zou

for i in range(3):
    print("loop ",i)
for i in range(0,5,2):
    print("loop ",i)

'''
age_of_kevin = 27
for i in range(3):
    guess_age = int(input("guess age:"))
    if guess_age == age_of_kevin:
        print("yes,you get it,kevin's age is %s" % (guess_age))
        break
    elif guess_age > age_of_kevin:
        print("No, you guess error.Think smaller...")
    else:
        print('No,you guess error.Think bigger...')
else:
    print("bye! you have no choice!")
'''
View Code

 


练习:

编写登陆接口

输入用户名密码,先判断用户名是否在lock.txt,如果存在输出锁定信息,如果不存在继续执行。
判断是否成功,从userinfo文件(kevin  123456)中读取用户密码进行判断,认证成功后显示欢迎信息
输错三次后锁定,将用户名保存到lock.txt文件

 
#Author:Kevin Zou
tag = 0
count = 0
while count < 3:
    username = input("请输入用户名:")
    password = input("请输入密码:")

    with open('lock.txt','r') as f_lock_read:
        for line in f_lock_read.readlines():
            line = line.strip('\n')
            user = line.split('\t')[0]
            if username == user:
                print("you are locked!")
                tag = 1
    if tag == 1:
        break

    with open('userinfo.txt','r') as f_user_read:
        for line in f_user_read.readlines():
            line = line.strip('\n')
            user = line.split('\t')[0]
            pwd = line.split('\t')[1]
            if username == user and password == pwd:
                print("Welcome %s ,you login successful!" % (username))
                tag = 1
                break
        else:
            print("login failed!")
    if tag == 1:
        break
    count = count + 1
else:
    with open('lock.txt','w') as f_lock_write:
        f_lock_write.write(username)
View Code

 



posted on 2017-07-18 22:56  kevin_zou  阅读(93)  评论(0)    收藏  举报

导航