1.程序交互input
语法格式:
#input出来的都是字符串类型 username = input('请输入用户名') password = input('请输入密码') print(username) print(password) #让用户输入姓名,年龄,性别然后打印一句话'我叫: , 今年: , 性别:, ' name = input('请输入姓名:') age = input('请输入年龄:') sex = input('请输入性别:') msg = '我叫:' + name + ',今年:' + age + ',性别:' + sex print(msg)
2.流程控制语句if
2.1单独if
if 条件: 结果 ###################################################################### if 3>2: print('hello world')
2.2if else
if 条件; 结果 else: 结果 ###################################################################### age = input('请输入您的年龄:') if int(age) < 18: print('你还未成年') else: print('恭喜你,你成年了')
2.3多条件if elif else(跟据业务需求,可加可不加)多选一
if 条件: 结果 elif 条件: 结果 ###################################################################### num = int(input('请输入你要猜的点数:')) if num == 1: print('恭喜你,猜对了') elif num == 2: print('还要加油哦!') elif num == 3: print('继续猜哦!') else : print('你太笨了。')
2.4嵌套if
if 条件: if 条件: if 条件: ###################################################################### username = input('请输入您的用户名:') password = input('请输入您的密码:') your_cord = input('请输入验证码:') code = 'qwer' if your_cord == code: if username == 'shuai' and password == '123': print('登录成功') else: print('账号密码错误') else: print('验证码输入有误')
2.5if小游戏练习
###猜年龄 age = 90 insert = int(input('请输入你要猜的年龄:')) if insert > age: print('太大了!') elif insert < age: print('猜小了') else : print('恭喜你,猜对了!') ###猜成绩 num = int(input('请输入你的成绩:')) if num == 100 : print('你太棒了!') elif num >= 90: print('优秀') elif num >= 70: print('一般') elif num >= 60: print('刚刚及格') else: print('你不及格')
3.while循环
语法格式 while 条件: 循环体 ###################################################################### while True : print('最炫民族风') print('我们不一样') print('庐州月') print('人间')
练习题: 列出1-100所有的数字
conut = 1 while conut < 100: conut = conut + 1 print(conut) ####################################################################### conut = 1 flog = True while flog: conut = conut + 1 print(conut) if conut == 100: flog = False
练习题:1+2+3+4+.......+100的总和
s = 0 conut = 1 while conut < 101: s =conut + s conut = conut + 1 print(s)
1.break:遇到break就直接退出循环体
while True : print('最炫民族风') print('我们不一样') break print('庐州月') print('人间')
练习题:将1-100的所有偶数列举出来
conut = 1 while conut < 101: if conut % 2 == 0: print(conut) conut = conut +1
2.continue:退出本次循环,继续下一次循环
说明:continue后面代码不执行,每次都会判断条件
flog = True while flog: print(111) print(222) flog = False continue print(333)
3.while else
说明:while循环如果被break打断,则不执行else语句
conut = 1 while conut < 5: print(conut) if conut == 2: break conut = conut + 1 else: print(666)
小练习:用户名密码最多登录三次
count = 1 while count <=3: count = count + 1 username = input('请输入您的用户名:') password = input('请输入您的密码:') your_cord = input('请输入验证码:') code = 'qwer' if your_cord == code: if username == 'shuai' and password == '123': print('登录成功') else: print('账号密码错误') else: print('验证码输入有误')
4.for循环
有限循环,可以和break和continue,else结合和while用法一样,遇到break直接结束循环,else则不继续执行。
for 变量 in iterable: pass ################### for i in s: print(i) #################### s = '加油干,怒芦柑' for i in s: print(i) if i == '干': break ################### for i in range(3): print(i) else: print("for循环没有提前挑出") ______________________________________________ for i in range(2): print(i) continue else: print("for循环提前跳出")
5.格式化输出
说明:%为占位符,s--->str表明数据类型
name = input('请输入您的姓名:') age = input('请输入您的年龄:') job = input('请输入您的工作:') hobby = input('请输入您的爱好:') msg = ''' --------- info of %s ------------ Name : %s Age : %s Job : %s Hobbie : %s --------- end --------------------- '''%(name,name,age,job,hobby) print(msg)
问题:先在有这么一行代码
这样会报错的,因为在格式化输出里,你出现%默认为就是占位符的%,但是我想在上面一条语句中最后的80%就是表示80%而不是占位符,怎么办?
msg = '我叫%s,今年%s,学习进度1%%' %('啊帅',18) print(msg)、
6.运算符
计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算、比较运算、逻辑运算、赋值运算、成员运算、身份运算、位运算,今天我们暂只学习算数运算、比较运算、逻辑运算、赋值运算、成员运算
算数运算符 | + - * / |
---|---|
比较运算符 | > > == |
赋值运算符 | = + |
逻辑运算符 | and or not 优先级not > and >or |
成员运算符 |
6.1算数运算
以下假设变量:a=10,b=20
6.2比较运算
以下假设变量:a=10,b=20
6.3赋值运算
以下假设变量:a=10,b=20
6.4逻辑运算
以下假设变量:a=10,b=20
根据逻辑运算的进一步研究:
1. 在没有()的情况下not,优先级高于and,and优先级高于or,即优先级关系为()>not>and>or,同一级优先级从左往右计算。
练习题:判断下列逻辑语句的True或False
判断or左边为True或False就行。
3>4 or 4<3 and 1==1 False 1 < 2 and 3 < 4 or 1>2 True 2 > 1 and 3 < 4 or 4 > 5 and 2 < 1 True 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 False 1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 False not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 False
2.x or y , x为真,值就是x,x为假,值是y
x and y, x为真,值是y,x为假,值是x
练习题:求出下列逻辑语句的值。
8 or 4 8 0 and 3 0 0 or 4 and 3 or 7 or 9 and 6 3 解:0 or 3 or 7 or 6 3 or 7 3
5.5成员运算
除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。
练习题:判断子元素是否在原字符串(字典,列表,集合)中
print('喜欢' in 'dkfljadklf喜欢hfjdkas') True print('a' in 'bcvd') False print('y' not in 'ofkjdslaf') True
6.6Python运算符优先级
运算符 | 描述 |
---|---|
** | 指数 (最高优先级) |
~ + - | 按位翻转, 一元加号和减号 (最后两个的方法名为 [email protected] 和 [email protected]) |
* / % // | 乘,除,取模和取整除 |
+ - | 加法减法 |
>> << | 右移,左移运算符 |
& | 位 'AND' |
^ | | 位运算符 |
<= < > >= | 比较运算符 |
<> == != | 等于运算符 |
= %= /= //= -= += *= **= | 赋值运算符 |
is is not | 身份运算符 |
in not in | 成员运算符 |
not and or | 逻辑运算符 |
7.编码初识
常见编码集:
-
ascii:
-
不支持中文
-
一个字符占用 8 位
-
-
gbk(包含 ascii)国标码:
-
一个英文字符占用 8 位(1 字节)
-
一个中文字符占用 16 位(2 字节)
-
-
Unicode:
-
英文:4 个字节,32 位
-
中文:4 个字节,32 位
-
-
utf-8(最流行的编码集):
-
英文:1 个字节,8 位(1 字节)
-
欧洲:2 个字节,16 位(2字节)
-
亚洲:3 个字节,24 位(3字节)
-
单位转换:
-
1 字节 = 8 位
-
1 Byte = 8 bits
-
1024 bytes = 1 KB
-
1024 KB = 1 MB
-
1024 MB = 1 GB
-
1024 GB = 1 TB # 够用了
-