1、运算符介绍
a.数字运算符
a.算术运算符:+ - * /(浮点除) //整数除 **乘方 %取模(即取余数)
b.比较运算符:< > <= >= == !=
c.赋值运算符:+= -= *= /=
b.逻辑运算符
a.and (两个表达式都必须为真)
print(1==1 and 1<2) 输出结果:True
print(1==2 and 1<2) 输出结果:False
print(1==2 and 3<2) 输出结果:False
b.or(两个表达式至少有一个为真)
print(1==1 or 1<2) 输出结果:True
print(1!=1 or 1>2) 输出结果:False
print(1==1 or 1>2) 输出结果:True
c.not(表达式为真,就是假)
a=1
b=2
print(not a==b) 输出结果:True
c.成员运算符
a.in
a = 10
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
print "a在列表中"
else:
print "a不在列表中" 输出结果:a不在列表中
b.not in
a = 2
list = [1, 2, 3, 4, 5 ];
if ( a not in list ):
print "a不在列表中"
else:
print "a在列表中" 输出结果:a在list列表中
d.身份运算符
a.is
a = 20
b = 20
if ( a is b ):
print "a和 b有相同的标识"
else:
print "a和 b没有相同的标识" 输出结果:a和 b有相同的标识
b.is not
a = 20
b = 30
if ( a is not b ):
print "a和b没有相同的标识"
else:
print "a和b有相同的标识" 输出结果:a和b没有相同的标识
c.经典面试题:is 与 == 区别
is 用于判断两个变量引用对象是否为同一个(同一块内存空间), == 用于判断引用变量的值是否相等
2、if判断
a.if判断(基础版)
a=3
if a==3:
print('允许你中100万')
else:
print('当个穷鬼') 输出结果:允许你中100万
b.if判断(普通版)
age=1
if age>=18:
print('允许你进入网吧')
else:
print('回去睡觉') 输出结果:回去睡觉
c.if判断(升级版)
age=int(input('请输入你的年龄:'))
print(age,type(age))
if age>=18:
print('允许你进入网吧')
else:
print('回去睡觉')
d.经典面试题:判断字符是否在列表中
list1=['1',2,'熊猫',(1,2,'r')]
if (1,2,'r') in list1:
print('a在列表中')
else:
print('a不在列表中') 输出结果:a在列表中
3、多重if和if嵌套
a.多重if:多个判断
判断成绩:如果你的成绩是90分,你是优秀的,如果你是80分,你是良好的,如果你是70分,你是中等的,如果你是60分,加油,否则,你留级
grade=int(input('请输入成绩:'))
if grade>=90:
print('你是优秀的')
elif grade>=80:
print('你是良好的')
elif grade>=70:
print('你是中等的')
elif grade>=60:
print('加油')
else:
print('你留级')
b.if嵌套
判断成绩:如果你的成绩在[80,+∞],吃海鲜,如果你的成绩在[60,80)之间,吃斋饭,如果你的成绩在[-∞,60)之间,饿几天
grade=int(input('请输入成绩:'))
if grade>=60:
if grade>=80:
print('吃海鲜')
else:
print('吃斋饭')
else:
print('饿几天')
c.扩展题:python实现石头剪刀布的游戏
1.从控制台输入要输出的拳--石头(1)、剪刀(2)、布(3)
2.电脑随机出拳--假定电脑只会出石头
3.比较胜负
分析:两个人(控制台 电脑随机数)
比较胜负 玩法有三种(玩家赢 电脑赢 平局)
玩家赢的场景:
剪刀(2)--布(3)
布(3)--石头(1)
石头(1)--剪刀(2)
平局
or只要一个条件满足就是真 and同时为真
import random
player=int(input('控制台输入要输出的拳--石头(1) 剪刀(2)、布(3):'))
computer=random.randint(1,3)
print('玩家输出的是:%s,电脑输出的是%s'%(player,computer))
if (player==2 and computer==3) or (player==3 and computer==1) or (player==1 and computer==2):
print('玩家赢了,电脑输了')
elif player==computer:
print('平局')
else:
print('电脑赢了,玩家输了')
4、while循环
a.循环5次hello world
i=1
while i<=5:
print('hello world')
i+=1 输出结果:hello world hello world hello world hello world hello world
b.题目:计算0+1+2+3+4+5+6+7.......+100相加的和等于多少
a=1
sum=0
while(a<101):
sum=sum+a
a=a+1
print(sum) 输出结果:5050
5、break和continue
a.break:跳出循环,不再执行
a = 0
while a < 5:
a = a +1
if a == 3:
break
print(a)
print("Good bye!") 输出结果:3 Good bye!
b.continue:跳出本次循环,执行下一次
a = 0
while a < 5:
a = a +1
if a == 3:
continue
print(a)
print("Good bye!") 输出结果:5 Good bye!
6、嵌套循环
a.打印直角三角形
row=1
while row<=5:
cols=1
while cols<row:
print('*',end='')
cols += 1
print('*')
row+=1
等同于:
for i in range(1,6):
for j in range(1,i+1):
print('*',end=' ')
print('')
输出结果:
*
**
***
****
*****
7、while循环+else
a.若循环无中止情况,即执行else的代码
i=1
while i<=3:
print('hello world')
i=i+1
else:
print('循环完了')
输出结果:
hello world
hello world
hello world
循环完了
b.若循环期间停止了,不执行else的代码
i=1
while i<=3:
print('hello world')
if i==2:
break
i=i+1
else:
print('循环完了')
输出结果:
hello world
hello world
8、for循环
a.循环3次 hello world
for i in range(3):
print('hello world') 输出结果:hello world hello world hello world
b.循环列表
list=['小米','xiaoming','小翠']
for i in list:
print(i) 输出结果:小米 xiaoming 小翠
c.循环字典
dict={'a':'1','b':'2','c':'4'}
for i in dict:
print(i+':'+dict[i]) 输出结果:a:1 b:2 c:4
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现