Python基础:运算与流程控制循环

Bertha 。 2022-04-02 07:59 401阅读 0赞

python运算与流程控制循环
布尔值(bool)
“””
True :非0的数字,非空的字符串、列表、元组、字典
False : 0 ‘’() [] {} None

  1. """
  2. num = 10
  3. print(bool(num)) True
  4. strs = 'aa'
  5. print(bool(strs)) True
  6. lst = [2, 3, 4] 列表
  7. print(bool(lst)) True
  8. dic = {'name': '彦成'} 字典
  9. print(bool(dic)) True
  10. tup = (1, 3, 4,) 元组
  11. print(bool(tup)) True
  12. print('-' * 20) 字符串可以和数字相乘,表示连续拼接
  13. num1 = 0
  14. print(bool(num1)) F
  15. str2 = "" 空格也是字符串
  16. print(bool(str2)) False
  17. lst2 = [] 空列表
  18. print(bool(lst2)) False
  19. dic2 = {}
  20. print(bool(dic2)) False
  21. person = None
  22. print(bool(person)) False

逻辑运算符:
True :非0的数字,非空的字符串、列表、元组、字典
False : 0 ‘’() [] {} None
None不能理解为0;0是有意义的,None是一个特殊的空值
布尔值可以用and;or;not;来运算
and运算是与运算,所有条件都符合才为true
or运算是或运算,只要其中有一个为True
not true=false
三种逻辑运算符优先级:not>and>or
算术运算符:
运算符 描述 实例 a=10,b=20

  • 加 两个对象相加 a + b 输出结果 30

  • 减 得到负数或是一个数减去另一个数 a - b 输出结果 -10

  • 乘 两个数相乘或是返回一个被重复若干次的字符串 a * b 输出结果 200
    / 除 x除以y b / a 输出结果 2
    // 取整除 返回商的整数部分 9//2 输出结果 4 , 9.0//2.0 输出结果 4.0
    % 取余 返回除法的余数 b % a 输出结果 0
    ** 幂 返回x的y次幂 a**b 为10的20次方, 输出结果 100000000000000000000

    “””
    算术运算符 + - * / // % ** 幂

    “””
    a = 10
    b = 20

    c = a + b
    print©

    c1 = a - b
    print(c1)

    c2 = a * b
    print(c2)

    c3 = b / a
    print(c3) # 2.0

    print(9 / 4) # 2.25
    print(9 // 4) # 2 取整数部分
    print(9 // 4.0) # 2.0
    print(9 % 4) # 取余数
    print(2 ** 3) # 8

    #判断一个数是否是偶数
    print(4 % 2) # 余数是0 的表示偶数,是1 表示奇数

复合赋值运算(自增运算)
运算符 描述 实例
+= 加法赋值运算符 c += a 等效于 c = c + a
-= 减法赋值运算符 c -= a 等效于 c = c - a
*= 乘法赋值运算符 c = a 等效于 c = c \ a
/= 除法赋值运算符 c /= a 等效于 c = c / a
%= 取模赋值运算符 c %= a 等效于 c = c % a
**= 幂赋值运算符 c **= a 等效于 c = c **“””
复合赋值运算符
+=、=、-=、/= 、//=、 \*= 、%=

  1. """
  2. # +=
  3. a = 2
  4. b = 3
  5. # a += b # a+=b --> a=a+b
  6. # a -= b # a=a-b
  7. a *=b # a=a*b
  8. print(a)
  9. a
  10. //= 取整除赋值运算符 c //= a 等效于 c = c // a
  11. 比较运算符
  12. """
  13. 比较运算符 ==、!= < 、> 、<= 、>=
  14. """
  15. print(10 == 10) # True
  16. print(10 != 32) # True
  17. print(10 < 2) # False
  18. print(10 > 43) # False
  19. print(10 <= 10) # True
  20. print(10 >= 10) # True

逻辑运算符:
True :非0的数字,非空的字符串、列表、元组、字典
False : 0 ‘’() [] {} None
None不能理解为0;0是有意义的,None是一个特殊的空值
布尔值可以用and;or;not;来运算
and运算是与运算,所有条件都符合才为true
or运算是或运算,只要其中有一个为True
not true=false
三种逻辑运算符优先级:not>and>or
“””
and or not
and : 与的关系 一假 则 假
or : 或的关系 一真 则真
not : 非 取反 not True —>False
优先级: not > and > or

  1. """
  2. b = 10 > 4 # True
  3. b1 = 10 > 2 # True
  4. b2 = 2 > 3 # False
  5. b3 = 10 < 5 # False
  6. print(b and b1 and b2) # False
  7. print(b or b1 or b2) # True
  8. print(b1 or b2 or b3) # True
  9. print(b2 or b3) # False
  10. print(b) # True
  11. print(not b) # 取反 False
  12. a = 10
  13. b = 'aa'
  14. print(a and b) # 返回 b 的值
  15. print(a or b) # 返回 a的值
  16. print(not a) # 返回False
  17. # 优先级为是not>and>or。
  18. print(True and False or not False and False)
  19. # 从左往右: false or True and False -->False
  20. # and优先级: False or not false--> True
  21. # True and False or True and False
  22. # --> False or False -->False
  23. print(True and True or not False and False)
  24. # and优先级: True or not False --> True
  25. # 从左往右: True or True and False --> False
  26. # True and True or True and False-->True or False ->True

or的使用:

  1. #去办理个人贷款买房手续,只需要你或者你媳妇代理
  2. you = input("你本人去吗(去或者不去)?")
  3. your_wife = input("媳妇去吗(去或者不去)?")
  4. if you == "去" or your_wife == "去":
  5. print("恭喜,至少一个人办理就行了!")
  6. else:
  7. print("必须有一个人来办理贷款业务!")

and 的使用:

  1. #去办理贷款买房手续,需要你且你媳妇一同来办理
  2. you = input("你本人去吗(去或者不去)?")
  3. your_wife = input("媳妇去吗(去或者不去)?")
  4. if you == "去" and your_wife == "去":
  5. print("恭喜,两个人来了,把事情办成了!")
  6. else:
  7. print("必须有两个人来办理贷款业务!")

“””
格式:
if 判断语句 :
执行的代码1…
执行的代码2…

执行流程: 如果判断语句是True,执行if体中的
内容(执行的代码1…,执行的代码2…)if 体执行
完后,程序继续执行后面的内容,直到程序结束。

如果判断语句是false,则不执行if体中的代码,程序
继续往下执行,直到程序结束。

  1. """
  2. # a = 10
  3. # b = 2
  4. # print('程序开始了....')
  5. #
  6. # if a < b:
  7. # print('这是正确的...')
  8. # print('欢迎新同学哈哈哈')
  9. #
  10. # print('程序结束了...')

需求:从控制台输入年龄,如果年龄小于18就不能来到英雄联盟

  1. # age_str = input('请输入年龄:')
  2. # age = int(age_str) # 注意类型转换
  3. # if age <= 18:
  4. # print('不能玩游戏...')
  5. #
  6. # print('程序结束了...')

“””
not 的使用:
12~17岁 青春期
18~24岁 青年期
25~44岁 壮年期

  1. #青年时期17~24年龄
  2. age = 30
  3. #不是青年时期
  4. if not (age >= 17 and age <= 24):
  5. print("不是青年时期!<17或者大于24")
  6. """
  7. age_str = input("请输入您老的年龄:")
  8. age = int(age_str)
  9. #
  10. # if age >= 18 and age <= 24:
  11. # print('恭喜你,来到了青年期')
  12. # not 18~24 18< >24
  13. if not (age >= 18 and age <= 24):
  14. print('一边去,反正不是青年期')

print(‘程序结束了’)

python流程控制介绍
顺序结构:从上到下
分支结构: if/elif/else
循环结构: for/while

  1. # 女朋友过节
  2. jr = input("请输入节日名称:")
  3. if jr == '生日':
  4. print('买花,买蛋糕...')
  5. elif jr == '情人节':
  6. print('买德芙...')
  7. elif jr == '圣诞节':
  8. print('苹果10x')
  9. else:
  10. print('发红包..')
  11. print('程序结束了...')

if嵌套

  1. # 上火车安检的例子
  2. ticket = '有'
  3. daozi_length = 50
  4. if ticket == '有':
  5. print('火车票通过了...')
  6. if daozi_length <= 16:
  7. print('可以上火车了..')
  8. else:
  9. print('没收,送公安局蹲两天')
  10. else:
  11. print('没票就不能上火车啦')

综合案例:

import random

产生一个int类型的随机数

randint保护开头和结尾

  1. num = random.randint(1, 3)
  2. print(num)

人机猜拳:
“””
猜拳游戏:
思路:
1.玩家从控制台输入内容,石头剪刀 布
2.电脑输出 石头剪刀布
3.判断
(1).玩家赢
(2).电脑赢了
(3).平局

  1. """
  2. # 第一步
  3. player_str = input("请输入你要出的拳头 石头/1, 剪刀/2,布/3 :")
  4. player = int(player_str) # 类型转换
  5. # 第二步
  6. import random 导入模块
  7. 产生一个int类型的随机变量 rand保护开头和结尾
  8. computer = random.randint(1,3) 电脑从1、2、3中随机选取一个数
  9. # 第三步
  10. '''
  11. 玩家 电脑
  12. 石头 剪刀
  13. 剪刀 布
  14. 布 石头
  15. '''
  16. if (player == 1 and computer == 2) or \
  17. (player == 2 and computer == 3) or \
  18. (player == 3 and computer == 1):
  19. print('电脑弱爆了...')
  20. elif player == computer:
  21. print('决战到天亮...')
  22. else:
  23. print('人类弱爆了')

练习1 打印小星星
“””
*
**



“””

  1. # num = 1
  2. # while num <= 5:
  3. # print('*' * num)
  4. # num += 1 # num = num + 1

练习2 : 计算1到100之间的和

  1. a=1
  2. # b=0
  3. # while a<=100:
  4. # b+=a
  5. # print(a,b)
  6. # a+=1

练习3: 计算1到100之间偶数的和

  1. a=1
  2. b=0
  3. while a<=100:
  4. if a%2==0
  5. b+=a
  6. print(a,b)
  7. a+=1

练习4 从控制台入五个值,求和,求平均值

  1. a_str=input('请输入第一个值:')
  2. a=int(a_str)
  3. b_str=input('请输入第二个值:')
  4. b=int(b_str)
  5. c_str=input('请输入第三个值:')
  6. c=int(c_str)
  7. d_str=input('请输入第四个值:')
  8. d=int(d_str)
  9. e_str=input('请输入第五个值:')
  10. e=int(e_str)
  11. total=a+b+c+d+e
  12. print(total)
  13. f=total/5
  14. print(f)

练习5 数字逆序输出,从控制台输入三位数,例如123逆序输出321

  1. a4=input('请输入一个三位数')
  2. a4=int(a4)
  3. gewei=a4%10
  4. baiwei=a4//100
  5. shiwei=((a4-gewei)%100)/10
  6. a4=gewei*100+shiwei*10+baiwei
  7. print(a4)`

发表评论

表情:
评论列表 (有 0 条评论,401人围观)

还没有评论,来说两句吧...

相关阅读