Python基础的语句判断,循环

#python运算与流程控制循环
一、布尔值bool运算
True :非0数字,非空的字符串,列表,元组,字典 。
False :0、 “” 、() 、{}、[]、 None。
1、num = 10 # 非零的数字
print(bool(num))#true
2、strs = “aa”
print(bool(strs))#true
3、lest = [2, 3, 4] # 列表
print(bool(lest))#true
4、dic = {“name”: “大侠”} # 字典
print(bool(dic))#true
5、tup = (1, 2, 3,) # 元组
print(bool(tup))
6、print("-" * 20) # 字符串可以和数字相乘,表示连续拼接--------------------.
7、num1 = 0
print(bool(num1)) # false
8、str2 = “”
print(bool(str2)) # false空格也是字符串 ,有空格的话显示true
9、lst2 = []
print(bool(str2)) # false空列表
10、dic2 = {}
print(bool(dic2))# false
11、person = None
print(bool(person))# false
二、运算符
1、算术运算符+,-,,/,//,%,**
print(3%2)#余数1,可用来判断奇数偶数
print(11//2)#取整数5
print(2**3)#2的3次方8
2、赋值运算
a=10
b=20
print(a+b)#结果30
3、复合赋值运算+=,-+,
=。。。
a=6
b=20
a+=b
print(a)#结果26
a=6
b=18
a*=b
print(a)#结果108
4、比较关系运算符==,!=,<,>,<=,>=
print(10<=17)#True
print(10!=17)#True
print(10>=19)#False
5、逻辑运算符
and 或,一假则假;or与,一真则真 ; not非,取相反值
#例:
b=10>2#true
b1=10>4#true
b2=10<20#ture
b3=10<4#flase
b4=10<3#flase
print(b and b1 and b2)#true
print(b and b3 and b4)#false一假则假
print(b or b3 or b4)#true一真则真
print(b)#true
print(not b)#false取相反值
#例2
a=10
b=“aa”
print(a and b) #返回b的值
print(a or b) #返回a的值,a真则不再向下进行
print(not a) #返回false
6、优先级
#not>and>or
#例:print(True and False or not False and False)---->Turt and False or True and False---->False or False---->False
7、if else结构
if 判断语句。。。代码1.。。。
。。。代码2.。。。
else:
。。。代码3.。。。
。。。代码4.。。。
执行流程:
如果 判断语句是true,执行if体中的内容(执行代码1.。。。执行代码2.。。。)if体执行完后,程序继续执行后面的内容,直到程序结束。
如果判断语句是folse,则不执行if体中的代码,程序继续往下执行,直到程序结束"""
例如:
a=17
b=15
if a<b:
print(“这是正确的。。。”)
else:
print(“这是错误的。。。”)
print(“程序结束了。。。”)
8、if elif结构
格式 if
判断语句。。。执行代码1。。。
判断语句。。。执行代码2。。。
else:
。。。执行代码。。。
。。。执行代码。。。
执行流程从上到下,如果判断语句是true,则执行if,folse则执行else.
例如:

jr=input(“请输入节日名称:”)
if jr==“生日”:
print(“买生日蛋糕,买花。。。”)
elif jr==“圣诞节”:
print(“买苹果10x”)
elif jr==“情人节”:
print(“买德芙”)
elif jr==“中秋节”:
print(“买月饼”)
else:
print(“发红包”)

print(“程序结束了。。。”)
9、if嵌套式结构
格式 if 判断语句 执行代码1。。。
elif 判断语句 执行代码2。。。
elif 判断语句 执行代码3.。。。
else:
执行代码4.流程从上到下
例如:
ticket=“有”
dao_lengh=50
if ticket==“有”:
print(“火车票通过啦!”)
if dao_lengh<=15:
print(“可以上车”)
else :
print(“不许上车,没收刀子,派出所拘留”)
else :
print(“没票不准上火车。。。”)
10、猜拳游戏
play_er=input(“请玩家输入要出的拳头 石头/1 剪刀/2 布/3”)
player=int(play_er)
import random
computer=random.randint(1,3)
print(computer)
if player=1 and computer=2 or
player=2 and computer=3 or
player=3 and computer=1:
print(“电脑弱爆了”)
elif player==computer:
print(“决战到天亮”)
else:
print(“人类弱爆了”)
11、while循环格式:
while 条件语句 :
…代码1…
…代码2…

流程:
首先判断条件语句是否成立,如果成立 执行循环体中的内容,
当循环体执行完后,继续判断条件语句是否成立,如果成立
继续执行循环体,直到条件语句不成立后,循环结束,
程序继续往下执行。为了避免死循环要加范围。
a = 1
while a < 5:
print(‘哈哈哈’)#哈哈哈*4
print(‘程序结束了…’)
例:想向控制台 输出10句hello world
a = 1
while a <= 10:
print(a, ‘hello world’)
a += 1 # a=a+1 a=9+1 --> a=10
print(‘程序结束了…’)

#练习1
“”"*
**


***""“控制台中出现此图形。
a=1
while a<5:
print(”
"*a)
a+=1
print(“程序结束了”)
#练习2
1-100的和
num = 1
total = 0
while num <= 100:
total += num
num += 1
print(total)#结果为5050
#练习3
(1)1-100中偶数的和
num=0
osh=0
while num<=100
osh+=num
num+=num+2
print(osh)
(2)
num=0
osh=0
while num<=100:
if num%2=0
osh+=num
num+=1
print(osh)

练习4
从控制台输入5个值求和。
a=1
total=0
while a<=5:
num_str1=input(“请输入要求的数”)
num1=int(num_str1)
a+=1
total+=num1
print(total)
从控制台输入5个值求和的平均值。
a=1
total=0
while a<=5:
num_str=input(“请输入数字”)
num=int(num_str)
a+=1
total+=num
print(total/5)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值