python 基本语法
一、Hello World
python下的Hello World写法:
1 #!/usr/bin/env python 2 # _*_ coding: UTF-8 _*_ 3 # Author:taoke 4 print("Hello world!!!")
[#!usr/bin/env python] 解释:
寻找linux系统下的python环境变量
[# _*_ coding: UTF-8 _*_] 解释:
当前文件使用UTF-8 编码
[# Author:taoke] 解释:
没错,就是我>_<
二、变量
变量定义的规则:
1、变量名只能够是数字、字母、下划线的任意组合
2、变量名的第一个字符不能够是数字
3、以下关键字不能够作为变量名
[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中没有常量的概念,个人定义把变量名全大写的变量作为常量来使用)
三、编码
ASCII编码 255 1bytes
-->1980 GB2312 2bytes
-->1995 GBK1.0 2w+
-->2000 GB18030 27xxx
-->1994 unicode 2bytes (万国码)
-->UTF8 en(ascii):1bytes
ch:3bytes
四、注释
1、# 注释内容
2、‘‘‘
注释内容
’’’
五、用户交互程序(input和格式化输出)
字符串拼接尽量不使用,
用以下三种格式化输出的方式:
1 #!/usr/bin/env python 2 # _*_ coding: UTF-8 _*_ 3 # Author:taoke 4 5 name = input("name:") 6 job = input("job:") 7 salary = input("salary:") 8 age = int(input("age:")) 9 print(type(age),type(str(age))) 10 outstr1 = """ 11 ------info %s------- 12 name:%s 13 job:%s 14 salary:%s 15 age:%d 16 """%(name,name,job,salary,age) 17 outstr2 = """ 18 ------info {_name}------- 19 name:{_name} 20 job:{_job} 21 salary:{_salary} 22 age:{_age} 23 """.format(_name = name, 24 _job = job, 25 _salary = salary, 26 _age = age) 27 outstr3 = """ 28 ------info {0}------- 29 name:{0} 30 job:{1} 31 salary:{2} 32 age:{3} 33 """.format( name, 34 job, 35 salary, 36 age) 37 38 print(outstr3)
六、猜年纪的游戏(while 和 if)
1 #!/usr/bin/env python 2 # _*_ coding: UTF-8 _*_ 3 # Author:taoke 4 reallyAge = 43 5 guessage = 0 6 while guessage != reallyAge: 7 guessage = int(input("please input guess age:")) 8 if guessage<reallyAge: 9 print("小了") 10 elif guessage>reallyAge: 11 print("大了") 12 else: 13 print("猜对了")
七、作业
1、编写登录接口
1) 输入用户名密码
2)登录成功后显示欢迎信息
3)输错三次后锁定(即:输出用户已被锁定)
1 #!/usr/bin/env python 2 # _*_ coding: UTF-8 _*_ 3 # Author:taoke 4 5 username = 'huangye' 6 password = '123456' 7 flag=0 8 count = 0 9 while True: 10 inputname = input("用户名:") 11 inputpassword = input("密码:") 12 if inputname == username: 13 if count<3: 14 if inputpassword == password: 15 print("欢迎{_username}使用".format(_username = inputname)) 16 break 17 else: 18 count+=1 19 print("用户名或密码错误") 20 else: 21 print("用户已被锁定") 22 else: 23 print("用户名或密码错误")
2、编写一个多级菜单程序
1) 三级菜单
2)可一次选择进入各级子菜单
1 #!/usr/bin/env python 2 # _*_ coding: UTF-8 _*_ 3 # Author:taoke 4 #三级菜单 5 dic_ = { "tap_main":("tap1","tap2","tap3"), 6 "tap1":("tap1_1","tap1_2","tap1_3"), 7 "tap2":("tap2_1","tap2_2","tap3_3"), 8 "tap3":("tap3_1","tap3_2","tap3_3"), 9 "tap1_1":('tap1_1 msg1','tap1_1 msg2','tap1_1 msg3'), 10 "tap1_2":('tap1_2 msg1','tap1_2 msg2','tap1_2 msg3'), 11 "tap1_3":('tap1_3 msg1','tap1_3 msg2','tap1_3 msg3'), 12 "tap2_1":('tap2_1 msg1','tap2_1 msg2','tap2_1 msg3'), 13 "tap2_2":('tap2_2 msg1','tap2_2 msg2','tap2_2 msg3'), 14 "tap2_3":('tap2_3 msg1','tap2_3 msg2','tap3_3 msg3'), 15 "tap3_1":('tap3_1 msg1','tap3_1 msg2','tap3_1 msg3'), 16 "tap3_2":('tap3_2 msg1','tap3_2 msg2','tap3_2 msg3'), 17 "tap3_3":('tap3_3 msg1','tap3_3 msg2','tap3_3 msg3'), 18 } 19 20 menuName = "tap_main" 21 curmenu = "tap_main" 22 print(dic_[curmenu]) 23 while True: 24 menuName = input("需要访问的菜单名:") 25 if menuName == 'b': 26 #输出上一级菜单 27 if curmenu == "tap_main": 28 print("输入错误") 29 elif curmenu == "tap1" or curmenu == "tap2" or curmenu == "tap3": 30 curmenu = "tap_main" 31 elif curmenu == "tap1_1" or curmenu == "tap1_2" or curmenu == "tap1_3": 32 curmenu = "tap1" 33 elif curmenu == "tap2_1" or curmenu == "tap2_2" or curmenu == "tap2_3": 34 curmenu = "tap2" 35 elif curmenu == "tap3_1" or curmenu == "tap3_2" or curmenu == "tap3_3": 36 curmenu = "tap3" 37 elif menuName == 'q': 38 break 39 else: 40 if menuName in dic_.keys(): 41 curmenu = menuName 42 else: 43 print("输入错误") 44 print(dic_[curmenu]) 45 46 print("程序结束")