基本运算符
算数运算符
print(10 / 3)
print(10 // 3)
print(10 % 3)
3.3333333333333335
3
1
比较运算
#关于相等性比较所有类型都可以混着用
print("xiaoyunwei" == 10)
print("xiaoyunwei" != 10)
print([111,222] == [222,111])
False
True
False
#> >= < <=主要用于数字类型
#了解:比长度
print("abcdef" > "abz")
print(len("abcd") > len("abz"))
False
True
# 列表比大小,主要是用于同一种类型
l1 = ['abc',123]
l2 = [123,'abc']
print(l1 > l2)
Traceback (most recent call last):
File "/mnt/c/Users/86155/Desktop/ORM/输入输出.py", line 28, in <module>
print(l1 > l2)
TypeError: '>' not supported between instances of 'str' and 'int' #报错
l1 = ['abc','zzz']
l2 = ['abc','abc']
print(l1 > l2)
True
赋值运算符
# 增量赋值
age = 18
#age += 1 # age = age +1
age = age + 1
print(age)
#输出
19
x = 10
x %= 3 # x = x %3
print(x)
#输出
1 # 余数
# 链式赋值
x = y = z =10 #这就是链式赋值
x = 10
y = x
z = y
print(id(x))
print(id(y))
print(id(z))
#输出
94102804542688
94102804542688
94102804542688
# 交叉赋值
x = 10
y = 20
x,y = y,x
# 解压赋值
salaries = [11,22,33,44,55,66]
mon0 = salaries[0]
mon1 = salaries[1]
mon2 = salaries[2]
mon3 = salaries[3]
mon4 = salaries[4]
mon5 = salaries[5]
#强调:变量名的个数与值应该一一对应
# mon0,mon1,mon2,mon3,mon4,mon5=salaries
print(mon0,mon1,mon2,mon3,mon4,mon5)
#输出
11 22 33 44 55 66
#注意:多一个值或者少一个值都不行
salaries = [11,22,33,44,55,66]
mon0,mon1,mon2,mon3,mon4,mon5=salaries
print(mon0,mon1,mon2,mon3,mon4,mon5)
#输出
11 22 33 44 55 66
salaries = [11,22,33,44,55,66]
mon0,mon1,*others=salaries # 针对你不想要的变量用*_
mon0,mon1,*_=salaries
mon0,mon1,*_,mon_last=salaries #获取前两个和最后一个
*_,x,y=salaries #获取最后两个
print(mon0)
print(mon1)
print(others)
print(_)
#输出
11
22
[33, 44, 55, 66]
#课外知识,字典取值
x,y,z = {"k1":111,"k2":222,"k3":333}.values()
x,y,z = {"k1":111,"k2":222,"k3":333}.items()
print(x,y,z)
print(x)
逻辑运算符
# (1) not:对紧跟其后的那个条件的结果取反
print(not True) #False
print(not 10 > 3) #False
print(not 10) #True
# (2) and:用来连接左右两个条件,左右两个条件的结果都为True是,and的最终结果才为True
print(True and 10 > 3) #True
# (3) or:用来连接左右两个条件,左右两个条件的结果但凡有一个True时,or的最终结果才为True
print(10 < 3 or 10 == 10) #True
# ps:偷懒原则==短路运算
# 条件1 and 条件2 and 条件3
# 条件1 or 条件2 or 条件3
# 优先级:not > and > or
res1 = 3>4 and 4>3 or not 1==3 and 'x' == 'x' or 3>3 #True
res2 = (3>4 and 4>3) or (not 1==3 and 'x' == 'x') or 3>3 #True
res3 = 3<4 and (4>3 or not (1==3 and 'x' == 'x') ) #True
# 了解
print(1 and 0) # 0
print(1 and "abc" and 333) # 333
print(False and True or True) # True
print(0 and 2 or 1) # 1
流程控制
if判断
'''
接收用户输入的用户名
接收用户输入的密码
判断 输入的用户名 等于 正确的用户名 并且 输入的密码 等于正确的密码:
告诉用户登录成功
否则:
告诉用户账号名或密码输入错误
if判断的完整的语法
if 条件1:
代码1
代码2
代码3
...
elif 条件2:
代码1
代码2
代码3
...
elif 条件3:
代码1
代码2
代码3
...
...
else:
代码1
代码2
代码3
...
# 语法1
'''
if 条件1:
代码1
代码2
代码3
...
'''
# 语法2
'''
if 条件1:
代码1
代码2
代码3
...
else:
代码1
代码2
代码3
...
'''
# 语法3
'''
if 条件1:
代码1
代码2
代码3
...
elif 条件2:
代码1
代码2
代码3
...
elif 条件3:
代码1
代码2
代码3
...
...
'''
# 语法4(if判断是可以嵌套的)
'''
if 条件1:
if 条件1:
代码1
代码2
代码3
...
'''
'''
# 案例1:
db_name = "xiaoyunwei"
db_pwd = "abc@123"
name = input("用户名:")
pwd = input("密码:")
if name == db_name and pwd == db_pwd:
print("登录成功")
else:
print("登录失败")
# 案例2:
# 如果:成绩>=90,那么:优秀
# 如果成绩>=80且<90,那么:良好
# 如果成绩>=70且<80,那么:普通
# 其他情况:很差
chengji = input("请输入您的成绩:")
chengji = int(chengji)
if chengji >= 90:
print("优秀")
elif chengji >= 80 and chengji < 90:
print("良好")
elif chengji >= 70 and chengji < 80:
print("普通")
else:
print("很差")
#其他方法1
if chengji >= 90:
print("优秀")
elif chengji >= 80:
print("良好")
elif chengji >= 70:
print("普通")
else:
print("很差")
#其他方法2
if chengji >= 90:
print("优秀")
elif 80 <= chengji < 90:
print("良好")
elif 70 <= chengji < 80:
print("普通")
else:
print("很差")
# 案例3:
age = 19
height = 1.7
gender = "female"
is_beautiful = True
if 18 < age < 26 and 1.6 < height < 1.8 and gender == "female" and is_beautiful:
print("开始表白。。。")
else:
print("你是个好人。。。")
print("其他代码。。。")
while循环
"""
while 条件:
代码1
代码2
代码3
...
"""
# 1、基本使用
print('start...')
count = 0
while count < 5:
print(count)
count+=1
print('end...')
#输出
start...
0
1
2
3
4
end...
# 2、死循环
while 1:
print("hello")
# 3、用户案例
db_name = "xiaoyunwei"
db_pwd = "abc@123"
while True:
name = input("用户名:")
pwd = input("密码:")
if name == db_name and pwd == db_pwd:
print("登录成功")
else:
print("登录失败")
# 4、结束while循环
# (1)把条件改成False
db_name = "xiaoyunwei"
db_pwd = "abc@123"
tag = True
while tag:
name = input("用户名:")
pwd = input("密码:")
if name == db_name and pwd == db_pwd:
print("登录成功")
tag = False
else:
print("登录失败")
print('========================')
# (2)break会直接结束本层循环
db_name = "xiaoyunwei"
db_pwd = "abc@123"
tag = True
while tag:
name = input("用户名:")
pwd = input("密码:")
if name == db_name and pwd == db_pwd:
print("登录成功")
# tag = False
break
else:
print("登录失败")
print('========================')
tag = True
while tag:
break
while tag:
break
while tag:
break
tag = True
while tag:
tag = False
while tag:
tag = False
while tag:
tag = False
# 5、while循环的嵌套
db_name = "xiaoyunwei"
db_pwd = "abc@123"
tag = True
while tag:
name = input("用户名:")
pwd = input("密码:")
if name == db_name and pwd == db_pwd:
print("登录成功")
while tag:
print("""
0 退出
1 提现
2 转账
3 查询余额
4 修改密码
""")
choice = input("请输入您的命令编号:")
if choice == "0":
# break
tag = False
elif choice == "1":
print('=============>提现功能<================')
elif choice == "2":
print('=============>转账功能<================')
else:
print('=============>非法指令<================')
break
else:
print("登录失败")
print('========================')
# 6、while+contiune(会众制本次循环直接进入下一次)
conut = 0
while conut < 5:
if conut == 3:
# break
conut += 1
continue #强调:与continue同一级别的后续代码永远不会运行
print(conut)
conut+=1
db_name = "xiaoyunwei"
db_pwd = "abc@123"
while True:
name = input("用户名:")
pwd = input("密码:")
if name == db_name and pwd == db_pwd:
print("登录成功")
else:
print("登录失败")
# continue # 此处不加continue也会进入下一次,不要画蛇添足
# 7、while+else
# 如果while循环不是被break干掉的,那么while的结束都算正常死亡
count = 0
while count < 5:
if count == 3:
# count+=1
# continue
break
print(count)
count+=1
else:
print("else会在while循环正常死亡之后运行")
>练习题<
-
简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型
-
执行 Python 脚本的两种方式是什么(交互式、源文件)
-
Pyhton 单行注释和多行注释分别用什么?(#,’’’’’’)
-
布尔值分别有什么?(True\False)
-
声明变量注意事项有那些?(python不需要声明变量,因为python是动态类型的语言)
-
如何查看变量在内存中的地址?(id())
-
写代码
- 实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!
inp_name = input("your names:") inp_pwd = input("your password:") if inp_name == "seven" and inp_pwd == "123": print("Success!") else: print("ERROR!")
- 实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
count = 0 while count < 3: inp_name = input("your names:") inp_pwd = input("your password:") if inp_name == "seven" and inp_pwd == "123": print("Success!") break else: count+=1 print("ERROR!")
- 实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
count = 0 while count < 3: inp_name = input("your names:") inp_pwd = input("your password:") db_name = ["seven","alex"] db_pwd = '123' if inp_name in db_name and inp_pwd == db_pwd: print("Success!") break else: count+=1 print("ERROR!")
-
写代码
a. 使用while循环实现输出2-3+4-5+6…+100 的和sum = 0 x = 1 while x < 101: sum = sum + x x+=1 print(sum)
b. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12 使用 while 循环实现输出 1-100 内的所有奇数
e. 使用 while 循环实现输出 1-100 内的所有偶数
#偶数 a=100 s=0 while a>0: a=a-1 if a % 2 == 0: print(a,end=' ') #end=''的作用是打印数字时不会换行,可以并排阅读。 s+=a print('\n偶数和是:',s,'\n') #奇数 b=100 u=0 while b>0: b=b-1 if b % 2 != 0: print(b,end=' ') u+=b print('\n奇数和是:',u) #自己写的 a = 100 s = 0 while a > 0: a = a - 1 if a % 2 == 0: print(a) s+=a b = 100 u = 0 while b > 0: b = b - 1 if b % 2 != 0: print(b) u+=b
-
现有如下两个变量,请简述 n1 和 n2 是什么关系?
n1 = 123456
n2 = n1
# n2 是 n1的硬复制,如果n1变换了值,那么n2的值不会发生变化
for循环
# for 循环
# 对比while循环,在循环取值这一场景for循环更具有有事
# 1、更通用
# 2、更简单
name = ['json', 'jack', 'xiaoyuinwei', 'tom']
dic = {'k1':111,'k2':222,'k3':333}
print(len(name))
count = 0
while count < len(name):
print(name[count])
count += 1
for i in dic: # x = 'json'
print(i,dic[i])
# range
for i in range(3):
print("Hello")
# 解压赋值
info = [['name','xiaoyunwei'],['age',18],['gender','woman']]
for x,y in info:
print(x,y)
# for + break
for i in range(10):
if i == 5:
break
print(i)
#输出
0
1
2
3
4
# for + contiune
for i in range(10):
if i == 5:
continue
print(i)
#输出
0
1
2
3
4
6
7
8
9
# for + else
for i in range(10):
if i == 5:
continue
print(i)
else:
print('run...')
#输出
0
1
2
3
4
6
7
8
9
run...
# for循环嵌套
for i in range(3):
print('=========>loop%s'%i)
for j in range(5):
print("inner loop%s" %j)
#输出
=========>loop0
inner loop0
inner loop1
inner loop2
inner loop3
inner loop4
=========>loop1
inner loop0
inner loop1
inner loop2
inner loop3
inner loop4
=========>loop2
inner loop0
inner loop1
inner loop2
inner loop3
inner loop4
# 补充
# 九九乘法表+金字塔
可变不可变类型
可变类型:值变了,但是内存地址不变,证明就是在改变原值,即原值是可变类型
>>> l = ['111','222','333']
>>> print(id(l))
139747460862728
>>> l[0]=4444
>>> print(id(l))
139747460862728
不可变类型:值变了,但是内存地址也变了,证明是产生了新值,即原值是不可变类型
>>> age = 10
>>> print(id(age))
93950901134560
>>> age = 11
>>> print(id(age))
93950901134592