目录
第一章... 1
1:新建项目... 1
2:修改默认模板... 3
3:什么是变量... 3
4:重指向... 3
5:常量的表示... 4
6:格式化输出(三种方法)... 4
7:打印一个变量类型... 5
8:强制转换类型... 5
9:密码密文显示... 5
10:添加判断... 5
11:多层判断... 5
12:循环... 6
13:while循环并结束... 6
14:循环次数限制... 6
15:循环次数超限提示警告... 8
16:for循环... 8
17:跳着打印 补偿... 9
18:判断用户是否需要继续执行... 10
19:跳出本次循环执行下次循环。... 10
20:结束当前循环... 11
21:循环套循环... 11
第一章
1:新建项目
2:修改默认模板
3:什么是变量
变量用来存东西。为了后面调用。存在内存里。
4:重指向
name = "shenzhiwei"
name2 = name
print("my
name is ",name,name2)
name = ("sdsdf")
print(name,name2)
结果:my name is shenzhiwei shenzhiwei
sdsdf shenzhiwei
5:常量的表示
用大写表示常量,可以改值,但是不建议改。
6:格式化输出(三种方法)
name = input("name:")
age = int(input("age:"))#integer 强制转成整形
print(type(age))
job = input("job:")
info = '''
-------------info of %s----------
name:%s
age:%d
job:%s
'''%(name,name,age,job)
info2 = '''
-------------info of {_name}----------
name:{_name}
age:{_age}
job:{_job}
'''.format(_name=name,_age=age,_job=job)
info3 = '''
-------------info of {0} ----------
name:{0}
age:{1}
job:{2}
'''.format(name,age,job)
print(info,info2,info3)
%s s代表文本类型 d代表数字类型 f 浮点小数
7:打印一个变量类型
Age = input(“age:”)
Print(type(age))
8:强制转换类型
age = int(input("age:"))#integer 强制转成整形
print(type(age))
9:密码密文显示
import getpass
name = input("name:")
passwd = getpass.getpass("password:")
print(name,passwd)
10:添加判断
_name = '123456'
_password = '12345'
name = input("name:")
passwd = input("password:")
if _name == name and _password ==
passwd:
print("登陆成功,欢迎{name1}".format(name1=name))
else:
print("登录失败")
11:多层判断
age_of_lodboy = 55
guess_age = int(input("请输入年龄:"))
if guess_age == age_of_lodboy:
print("恭喜你猜对啦!")
elif guess_age > age_of_lodboy:
print("太大啦!")
else:
print("太小啦!")
12:循环
无限循环
count = 0
while True:
#当 条件成立
print("变大:",count)
count = count +1 #count +=1
13:while循环并结束
age_of_lodboy = 55
while True:
guess_age = int(input("请输入年龄:"))
if guess_age == age_of_lodboy:
print("恭喜你猜对啦!")
break
elif guess_age > age_of_lodboy:
print("太大啦!")
else:
print("太小啦!")
14:循环次数限制
一、笨方法
count = 0
age_of_lodboy = 55
while True:
guess_age = int(input("请输入年龄:"))
if guess_age == age_of_lodboy:
print("恭喜你猜对啦!")
break
elif guess_age > age_of_lodboy:
print("太大啦!")
count +=1
if count == 3:
break
else:
print("太小啦!")
count +=1
if count == 3:
break
二、一般方法、
count = 0
age_of_lodboy = 55
while True:
if count == 3:
break
guess_age = int(input("请输入年龄:"))
if guess_age == age_of_lodboy:
print("恭喜你猜对啦!")
break
elif guess_age > age_of_lodboy:
print("太大啦!")
else:
print("太小啦!")
count +=1
三、优化方法
count = 0
age_of_lodboy = 55
while count <3:
guess_age = int(input("请输入年龄:"))
if guess_age == age_of_lodboy:
print("恭喜你猜对啦!")
break
elif guess_age > age_of_lodboy:
print("太大啦!")
else:
print("太小啦!")
count +=1
15:循环次数超限提示警告
16:for循环
循环取值
for i in range(10):
print(“loop ”,i)
正常走完执行else,被破坏就不继续执行。
17:跳着打印 补偿
18:判断用户是否需要继续执行
count = 0
age_of_lodboy = 55
while count <3:
guess_age = int(input("请输入年龄:"))
if guess_age == age_of_lodboy:
print("恭喜你猜对啦!")
break
elif guess_age >
age_of_lodboy:
print("太大啦!")
else:
print("太小啦!")
count +=1
if count == 3:
ceshi = input("输入y继续?输入n退出?")
if ceshi !='n':
count = 0
19:跳出本次循环执行下次循环。
把循环走完。 contiune
for i in range(0,10):
if i <3:
print("loop ",i)
else:
continue
print("呵呵。。。")
20:结束当前循环
break
21:循环套循环
for i in range(10):
print("-------------",i)
for j in range(10):
print(j)
if j >5:
break
- 还没有人评论,欢迎说说您的想法!