一、hello world程序
#!/usr/bin/env python print("Hello World!")
二、变量
#!/usr/bin/env python name = "hhh" name2 = name print("My name is",name,name2) name = "ggg" print(name,name2)
上述代码声明了变量名name,变量name的值为“hhh”
变量定义的规则:
- 变量名只能是 字母、数字或下划线的任意组合
- 变量名的第一个字符不能是数字
- 以下关键字不能声明为变量名
['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']三、字符编码
三、字符编码
python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill)
ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256-1,所以,ASCII码最多只能表示 255 个符号。
显然ASCII码无法将世界上的各种文字和符号全部表示,所以,就需要新出一种可以代表所有字符和符号的编码,即:Unicode
Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节),即:2 **16 = 65536,
注:此处说的的是最少2个字节,可能更多
UTF-8,是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存...
#!/usr/bin/env python # -*- coding:utf-8 -*- name = "你好,世界" print(name)
四、用户输入
#!/usr/bin/env python # -*- coding:utf-8 -*- name = input("name:") age = input("age:") job = input("job:") salary = input("salary:") info = ''' ----------- info of %s ------------ Name:%s Age:%s Job:%s Salary:%s ''' % (name,name,age,job,salary) info2 = ''' ----------- info of {_name} ------------ Name:{_name} Age:{_age} Job:{_job} Salary:{_salary} '''.format(_name=name, _age=age, _job=job, _salary=salary) info3 = ''' ----------- info of {0} ------------ Name:{0} Age:{1} Job:{2} Salary:{3} '''.format(name,age,job,salary) print(info3)
五、表达式if...else
场景一:用户登陆验证
#!/usr/bin/env python # -*- coding:utf-8 -*- import getpass _username = 'hhh' _password = '123' username = input("username:") # password = getpass.getpass("password:") password = input("password:") if _username == username and _password == password: print("Welcome user {name} login...".format(name=username)) else: print("Invalid username or password!")
场景二:猜年龄
#!/usr/bin/env python # -*- coding:utf-8 -*- my_age = 28 guess_age = int(input("input your guess num:")) if guess_age == my_age: print("Congretulations,you got it!") elif guess_age < my_age: print("Oops,think bigger!") else: print("think smaller!")
六、表达式while循环
#!/usr/bin/env python # -*- coding:utf-8 -*- count = 0 while True: print("count:",count) count = count +1 if ount == 1000: break
优化猜年龄:
#!/usr/bin/env python # -*- coding:utf-8 -*- my_age = 28 count = 0 while count <3: guess_age = int(input("input your guess num:")) if guess_age == my_age: print("Congretulations,you got it!") break elif guess_age < my_age: print("Oops,think bigger!") else: print("think smaller!") count +=1 else: print("you have tried too many times...fuck off")
七、表达式for循环
#!/usr/bin/env python # -*- coding:utf-8 -*- for i in range(0,10,1): print("loop",i)
优化猜年龄:
#!/usr/bin/env python # -*- coding:utf-8 -*- my_age = 28 for i in range(3): guess_age = int(input("input your guess num:")) if guess_age == my_age: print("Congretulations,you got it!") break elif guess_age < my_age: print("Oops,think bigger!") else: print("think smaller!") else: print("you have tried too many times...fuck off")
无限次猜年龄:
#!/usr/bin/env python # -*- coding:utf-8 -*- my_age = 28 count = 0 while count <3: guess_age = int(input("input your guess num:")) if guess_age == my_age: print("Congretulations,you got it!") break elif guess_age < my_age: print("Oops,think bigger!") else: print("think smaller!") count +=1 if count ==3: countine_confirm = input("do you want to keep guessing..?") if countine_confirm != 'n': count = 0 else: print("you have tried too many times...fuck off")
八、表达式continue
#!/usr/bin/env python # -*- coding:utf-8 -*- for i in range(0,10): if i<3: print("loop",i) else: continue print("hehe...")
九、循环套循环
#!/usr/bin/env python # -*- coding:utf-8 -*- for i in range(10): print("--------------",i) for j in range(10): print(j) if j>5: break
十、作业
作业二:编写登陆接口
- 输入用户名密码
- 认证成功后显示欢迎信息
- 输错三次后锁定
#!/usr/bin/env python # -*- coding:utf-8 -*- message_list = open("message.txt").readlines() locked_list = open("locked.txt").readlines() d = {} for i in range(len(message_list)): d[message_list[i].split(' ')[0]]=message_list[i].split(' ')[1] count = 0 while count<3: username = input("username:") password = input("password:") if username in locked_list: print("Your username is locked! ") break else: if int(d[username]) == int(password): print("Welcome user {name} login...".format(name=username)) break else: print("Invalid username or password!") count +=1 if count ==3: open("locked.txt",'w').write(username)
作业三:多级菜单
- 三级菜单
- 可依次选择进入各子菜单
- 所需新知识点:列表、字典
#!/usr/bin/env python # -*- coding:utf-8 -*- data = {"福建": {"漳州":{"南靖":["龙山","小山城"],"平和":{},"龙海":{}}, "福州":{"鼓楼":{},"台江":{},"晋安":{}}, "厦门":{"思明":{},"湖里":{},"海沧":{}} }, "广东": {"广州":{"越秀":{},"天河":{},"海珠":{}}, "深圳":{"罗湖":{},"福田":{},"南山":{}}, "东莞":{"罗湖":{},"福田":{},"南山":{}} }, "四川": {"成都":{"青羊":{},"金牛":{},"武侯":{}}, "绵阳":{"游仙":{},"安州":{},"江油":{}}, "宜宾":{"南溪":{},"江安":{},"长宁":{}} }, } exit_flag = False while not exit_flag: for i in data: print(i) choice = input("选择进入1>>:") if choice in data: while not exit_flag: for i2 in data[choice]: print(i2) choice2 = input("选择进入2>>:") if choice2 in data[choice]: while not exit_flag: for i3 in data[choice][choice2]: print(i3) choice3 = input("选择进入3>>:") if choice3 in data[choice][choice2]: for i4 in data[choice][choice2][choice3]: print(i4) choice4 = input("最后一层,按b返回>>:") if choice4 == "b": pass elif choice4 == "q": exit_flag = True if choice3 == "b": break elif choice3== "q": exit_flag =True if choice2 == "b": break elif choice2 == "q": exit_flag = True