1.while循环
- 基本机构:
while 条件:
循环体
while True:
print("aloha")
print("this is python")
print("this is while")
-
基本原理:
- 先判断条件是否为True
- 如果是True,进入循环体
- 执行到循环体的底部
- 继续判断条件,若条件成立,再次进入循环体
-
循环如何终止
-
改变条件
flag = True while flag: print("aloha") print("this is python") flag = False print("this is while")
练习1:使用while循环输出1~100所有数字。
count = 1 while count <= 100: print(count) count += 1
练习2:计算1+2+3+4+……+100的值
result = 0 count = 1 while count <= 100: result += count count += 1 print(result)
-
break
循环中遇到break,直接退出循环
while True: print("aloha") print("this is python") break print("this is while") print("outside")
练习:打印出1~100所有的偶数
count = 2 while True: print(count) count += 2 if count > 100: break
-
系统命令
-
-
coutinue的用法
continue就相当于直接到循环体的底部,然后继续循环
flag = True while flag: print("aloha") print("this is python") flag = False continue print("this is while")
-
while else 的用法
count = 1 while count < 5: print(count) count += 1 else: print("aloha")
如果被break打断,则不执行else语句
count = 1 while count < 5: print(count) if count == 3: break count += 1 else: print("aloha")
-
练习: 建立用户登录系统,允许用户输入错误三次
count = 1 while count <= 3: username = input("please enter your name:") password = input("please enter your password:") enter_code = input("please enter your code:") code = "abcd" chance_times = str(3 - count) if enter_code == code: if username == "yangsen": if password == "1234": print("welcome python world!") break else: print("password error!") else: print("user does not exist!") else: print("你还剩%s次机会" % chance_times) count += 1 else: print("错误3次,禁止登陆")
2.格式化输出
字符串中想让某些位置变成动态可传入的,首先要考虑到格式化输出
name = input("请输入你的姓名:")
age = input("请输入你的年龄:")
job = input("请输入你的工作:")
hobbies = input("请输入你的爱好:")
# %-->占位符 s-->str
info = """
-----------%s的个人信息------------
姓名:%s
年龄:%s
工作:%s
爱好:%s
""" % (name,name,age,job,hobbies)
print(info)
注意点:格式化输出时,如果要输出的内容有%,而不是作为占位符使用,需要用%% 来表示一个%
如:
msg ="我叫%s, 今年%s, 学习进度%s%%" % ('杨森',18,2)
print(msg)
3.运算符
算数运算符(+ - * /)、比较运算符(> =)、赋值运算符(= +=)、逻辑运算符(and or)、成员运算符
i1 = 2
i2 = 3
print(2 ** 3)
print(10 // 3)
print(10 % 3)
print(3 != 4)
count = 1
count = count + 1
count += 1
print(count)
# and or not
# 1 在没有()的情况下,优先级:not > and > or,同一优先级从左至右依次计算
# 情况1:两边都是比较运算
# print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
# print(True or False)
# 情况2:两边都是整数
'''
x or y , x为真,值就是x,x为假,值是y
'''
# print(1 or 2)
# print(3 or 2)
# print(4 or 2)
# print(-1 or 2)
# print(0 or 2)
# print(1 and 2)
数据类型之间的转换
# str ---> int : 只能是纯数字组成的字符串
s1 = '00100'
print(int(s1))
# int ----> str
i1 = 100
print(str(i1),type(str(i1)))
# int ---> bool : 非零即True ,0为False。
i = 0
print(bool(i))
# bool ---> int
print(int(True)) # 1
print(int(False)) # 0
4.编码初识
ASCII:只包含英文字母,数字,特殊字符。均占8位(1字节)
gbk:英文字母,数字,特殊字符和中文。英文字母占8位(1字节),中文占16位(2字节)
Unicode:万国码,包括世界上所有的文字,不论中文、英文或符号,每个字符均占32位(4字节)。浪费空间,浪费资源。
utf-8:万国码升级版,英文占8位(1字节)、欧洲字符占16位(2字节)、中文字符站24位(3字节)
今日练习
练习1
猜数字,三次机会
answer = 66
count = 1
while count <= 3:
guss_num = int(input("请猜测数字:"))
if guss_num < answer:
print("你猜小了")
elif guss_num > answer:
print("你猜大了")
else:
print("你猜对了")
break
count += 1
else:
print("你太笨了")
练习2
使用while循环打印1 2 3 4 5 6 8 9 10
# 方法一
count = 1
while count <= 10:
if count == 7:
count += 1
print(count)
count += 1
# 方法二
count = 1
while count <= 10:
if count == 7:
pass
else:
print(count)
count += 1
# 方法三
count = 0
while count < 10:
count += 1
if count == 7:
continue
print(count)
练习3
求1-2+3-4+5……+99-100的结果
count = 1
result = 0
while count <= 100:
if count%2 == 0:
result -= count
else:
result += count
count += 1
print(result)