Program01 基本的输出
作为一种语言的学习,他的仪式感很重要
print("Hello World!")
这就是python3里面的 Hello World! 打印输出
Program02 注释符号的使用
name = "你好,世界"msg= """print(name)
666
777
888"""name2= name+"111"
print(name)print(name2)print("My name is",name,"Hello world")print(msg)
输出结果:
你好,世界
你好,世界111
My nameis你好,世界 Hello worldprint(name)666
777
888
'''在python3中 三个连续的引号是多行注释'''
此程序中可以学到:python3中 ''' 表示多行注释,如果将注释掉的内容赋值给一个变量,
print一下是可以打印输出的,单行注释使用的是 #
Program03 多种方式打印个人信息
name = input("name:")
age= int(input("age:")) #integer 将字符串转化为整数型
print( type(age), type( str(age)))
job= input("job:")
salary= input("salary:")
info= '''-------- info of %s--------
Name: %s
age: %d
job: %s
salary: %s'''%(name,name,age,job,salary)print(info)
输出结果:
name:XiaoBai
age:11
job:Student
salary:1000
-------- info of XiaoBai--------Name: XiaoBai
age:11job: Student
salary:1000
name = input("name:")
age= int(input("age:")) #integer 将字符串转化为整数型
print( type(age), type( str(age)))
job= input("job:")
salary= input("salary:")
info= '''-------- info of %s--------
Name: %s
age: %d
job: %s
salary: %s'''%(name,name,age,job,salary)
info2= '''-------- info of {_name} --------
# 此处下划线仅仅是为了区别name 这一个变量
Name: {_name}
age: {_age}
job: {_job}
salary: {_salary}'''.format(_name =name,
_age=age,
_job=job,
_salary=salary)print(info2)
输出结果:
name:xiaobai
age:11
job:Student
salary:1000
-------- info2 of xiaobai --------
# 此处下划线仅仅是为了区别name 这一个变量
Name: xiaobai
age: 11
job: Student
salary: 1000
name = input("name:")
age= int(input("age:")) #integer 将字符串转化为整数型
job = input("job:")
salary= input("salary:")
info= '''-------- info of %s--------
Name: %s
age: %d
job: %s
salary: %s'''%(name,name,age,job,salary)
info3= '''-------- info3 of {0} --------
Name: {0}
age: {1}
job: {2}
salary: {3}'''.format(name,age,job,salary)print(info3)#打印整段,%s 是段内调用段外的字符串
# 在此方法中输入时,format()中的name只写了一次!
输出结果:
name:xiaobai
age:11job:Stu
salary:1000
-------- info3 of xiaobai --------Name: xiaobai
age:11job: Stu
salary:1000
总结:
'''时间 2018年2月6日21:08:12
目的 学习python输入、输出
记录 通过input输入的默认格式是str,可以通过int()强制类型转化为int类型
通过type 可以查询目标元素的格式和类型
三个引号(不区分单双)可以注释一整段,如果将注释掉的内容赋值
给一个变量那么可以打印输出该段内容
使用format的时候不要忘记加 .
灵活运用三种打印输出段落,并对外部内容引用的方式。其中第二种最为常见
而且一个{} 中只写一个变量名称
input("登录用户{m},{n}?".format(m = user_name01,n = user_name02)) correct
input("登录用户{m,n}?".format(m = user_name01,n = user_name02)) error'''
Program04 判断语句
'''时间 2018年2月6日21:14:05
目的 if else 流程判断
记录 隐藏密文
而且python是强制缩进,同级会同时输出'''
importgetpass
_username= 'abc'_password= 'abc123'username= input("username:")#password = getpass.getpass("password:") 此时是密文,隐藏密码
password = input("password:")print(username,password)if _username == username and _password ==password:print("Worlcome user {name} login...".format(name =username))else:print("Invalid username or password!")
输出结果:
username:abc
password:abc123
abc abc123
Worlcome user abc login...
总结:
#if执行语句必须含有缩进,否则会报错!
Program05 while循环语句
'''时间 2018年2月6日22:03:14
目的 继续测试while 循环
记录 while 循环需要加冒号'''age_of_boy= 56count=0while count < 3:
guess_age= int(input("guess age:"))if guess_age ==age_of_boy:print("yes,you got it.")break
elif guess_age >age_of_boy:print("think smaller...")else:print("think bigger...")
count+= 1
if count == 3:
continue_confirm= input("do you want to keep gussing?")if continue_confirm != 'n':
count= 0
输出结果:
guess age:10think bigger...
guess age:100think smaller...
guess age:50think bigger...
do you want to keep gussing?n
Program06 for循环语句
'''目的 测试for 循环
记录 从此可以看出,python中的循环和MATLAB中的循环与构建向量有相似之处'''age_of_boy= 56
for i in range(3):
guess_age= int(input("guess age:"))if guess_age ==age_of_boy:print("yes,you got it.")break
elif guess_age >age_of_boy:print("think smaller...")else:print("think bigger...")else:print("you have tried too many times...have a rest")#特别值得注意的一点是 在python中if-elif-else语句的写法#break 和 continue的区别是 前者终止当前循环,后者是跳出本次循环,进入下一次循环
输出结果:
think bigger...
guess age:70think smaller...
guess age:60think smaller...
you have tried too many times...have a rest
Program07 简易用户登录界面
'''时间 2018年2月10日19:51:29
目的 制作一个用户登录界面,能够提示用户输入密码,三次出错锁定
# 还有小问题是不会使用python对文件进行增删改查的操作,此次操作的用户名和用户密码最好是在文件中'''user_name01= 'abc'user_name02= 'bcd'user_pass01= 'abc123'user_pass02= 'bcd123'
#用于提示用户被锁住,无法登录
user_flag01 =True
user_flag02=True
count=0
choice= input("登录用户{m},{n}?".format(m = user_name01,n =user_name02))if choice == user_name01 and user_flag01 isTrue:while count < 3:
pass_01= input("please input the pass word of {na}:".format(na =user_name01))if pass_01 ==user_pass01:print("congratulations!you passed \t")break
else:print("try again:")
count+= 1
else:
userflag01=Falseprint("you hava tried so many times,the id is locked")elif choice == user_name02 and user_flag02 isTrue:while count < 3:
pass_02= input("please input the pass word of {nb}:".format(nb =user_name02))if pass_02 ==user_pass02:print("congratulations!you passed \t")break
else:print("try again:")
count+= 1
else:
userflag02=Falseprint("you hava tried so many times,the id is locked")
输出结果:
登录用户abc,bcd?abc
please input thepassword of abc:abcabctryagain:
please input thepassword of abc:acb123tryagain:
please input thepassword of abc:abc123
congratulations!you passed
Program08 字典的使用
#三级菜单的创建#思路 通过字典存储
'''时间 2018年2月10日20:34:03
目的 创建一个三级菜单
记录 利用函数简化此代码,pass不进行任何操作
总结 2018年2月11日重新编写并运行此代码,发现一些错误
在重新编写之后,发现python是严格的格式对齐语言,如果同时存在while 和 for 的时候,
二者没有区分循环层次,误将二者对齐,则会报错。在之后的一系列测试中发现,循环体的
书写位置也会影响代码的运行结果!'''data={'北京':{"昌平":{"沙河":["oldboy","test"],"天通苑":["链家地产","我爱我家"]
},"朝阳":{"望京":["奔驰","陌陌"],"国贸":{"CICC","HP"},"东直门":{"Advent","飞信"},
},"海淀":{},
},'山东':{"德州":{},"青岛":{},'济宁':{'兖州':["BBQ","TYZY"],'太白':["JNYX","FSYY"]
},'济南':{'历下':["山大","山交通"],'长清':["山师","山工艺"]
}
},
}
exit_flag=Falsewhile notexit_flag:for i indata:print(i)
choice= input("选择进入1>>:")if choice indata:while notexit_flag:for i2 indata[choice]:print("\t",i2)
choice2= input("选择进入2>>:")if choice2 indata[choice]:while notexit_flag:for i3 indata[choice][choice2]:print("\t\t", i3)
choice3= input("选择进入3>>:")if choice3 indata[choice][choice2]:for i4 indata[choice][choice2][choice3]:print("\t\t",i4)
choice4= input("最后一层,按b返回>>:")if choice4 == "b":pass
elif choice4 == "q":
exit_flag=Trueif choice3 == "b":break
elif choice3 == "q":
exit_flag=Trueif choice2 == "b":break
elif choice2 == "q":
exit_flag=True
MYK
2018年2月11日