学习Python第二天_数据类型&运算符&if分支

仅记录个人学习Python所学,学识浅薄,若有错误欢迎指出。文章可能会不太完善,后续可能会继续更新。

一.数据类型

Number【数字:整型int,浮点型[小数]float,复数类型complex】

String【字符串】

Boolean【布尔类型】 True真(1), Flase假(0)

None【空值】

list【列表】

tuple【元组】

dict【字典】

set【集合】

bytes【字节】b’hello’ (不常用)

# int
a = 100
print(a, type(a))

# float
b = 10.3
print(b, type(b))

# 小数精度
c = 32
s = c / 2    # round()四舍五入
print(round(s))

# str
c = "liang"
print(c, type(c))

# bool
d = True
print(d, type(d))
print(int(True), int(False))

# None
e = None
print(e, type(e))

# list
f = [1, 2, 3]
print(f, type(f))  # [1, 2, 3] <class 'list'>

# tuple
g = (1, 2, 3)
print(g, type(g))  # (1, 2, 3) <class 'tuple'>

# dict
h = {"name": "yi", "age": 99}
print(h, h["name"], type(h))  # {'name': 'yi', 'age': 99} yi <class 'dict'>

# set
i = {1, 1, 3, 4}
print(i, type(i))  # {1, 3, 4} <class 'set'>

# bytes
j = b'hello'
print(j, type(j))  # b'hello' <class 'bytes'>

二.运算符

1.算术运算符

“+” “-” “*”【乘法】 /【除法】 %【求余,取模】 **【求幂,次方】 //【取整】

num1 = 5
num2 = 3
print(num1 / num2)  #浮点型:1.6666666666666667    默认精度16位
print(num1 % num2)  #2
print(num1 ** num2) #5的3次方
print(num1 // num2) #获取浮点数的整数部分

#除了+和-之外,其他的算术运算符都是相同的优先级
#出现优先级,解决办法使用括号
print((2 ** 5) * 3)  # 96
# 秒转化时分秒
t = 5446
hour = t // 3600  # 小时
minute = t // 60 % 60  # 分
second = t % 60  # 秒
print("%2d" % hour, ":", "%2d" % minute, ":", "%2d" % second)
2.赋值运算符

简单赋值运算符:= 给一个变量进行赋值

复合赋值运算符:+= -= %= /= … 给一个变量进行赋值,同时给变量进行相应的运算

# 赋值运算符
# =, +=, -=, %=, *=, //=, /=, **=
# = : 一定会先运算=右边的表达式
a = 30
print(a)
a += 3  # a = a + 3
print(a)
a -= 3  # a = a - 3
print(a)
a *= 3  # a = a * 3
print(a)
a **= 3  # a = a ** 3
print(a)
a //= 3  # a = a // 3
print(a)
a /= 3  # a = a / 3
print(a)
a %= 7  # a = a % 7
print(a)
3.关系【条件,比较】运算符

作用:比较大小,得到结果为布尔值【如果表达式成立,则返回True,如果不成立,则返回False】

# 关系运算符
# >, <, >=, <=, == ,!=
print(10 > 4)      # True
print(10 < 4)    # False
print(10 >= 4)      # True
print(10 <= 4)    # False
print(10 == 4)    # False
print(10 != 4)      # True
# 字符串比较
# 比较字符串大小 比较前几位 ASCII码值大小 前几位比较出大小 不再进行后续比较
# ASCII     A~Z 65-90
#           a~z (65-90)+32
#           0~9 48-57
print('a' > 'c')  # False
print('abc' > 'bdc')    # False
print('ed' > 'ac')      # True
4.逻辑运算符
# and 并且 , or 或者, not 取反

# and : 两边都为真则为真, 一边为加 则为假
print(True and True)  # True
print(False and True)  # False
print(False and False)  # False
print(True and False)  # False
print(10 % 2 == 0 and 3 > 4)  # False
# or : 一个为真则为真,两边都假则为假
print(True or True)  # True
print(False or True)  # True
print(False or False)  # False
print(True or False)  # True
print(10 % 2 == 0 or 3 > 4)  # True
# not : 一定会得到一个bool值
""" 以下均取反 """
print(not True)  # False
print(not False)    # True
print("*****" * 10)
print(not 0)    # True
print(not "")  # True
print(not "   ")  # False
print(not None)  # True
print(not [])  # True
print(not {})  # True
print()
# bool
# 数值类型: 0 为False  其他都为真
# 字符串: ""空字符串是假的,其他都为真
# None : None 是假的
# list 类型: [] 空列表为假,其他为真
# dict 类型: {} 空字典为假,其他为真

# 短路操作
# and
x = 10 and 0 and 4  # 前面不满足 后面不会进行判断
x = 10 and print(2)   # print 输出后 变为 None
print(x)

# or
y = 0 or 10 or 4
y = 0 or 3 or print(2)
print(y)

# 练习
x = 5 and True  # True
y = 4 or False  # 4
s = x * 3 + y   # True = 1   1 * 3 + 4
print(s)

# 判断闰年
year = 2020
s = year % 4 == 0 and year % 100 != 0 or year % 400 == 0
print(s)
5.成员运算符和身份运算符

成员运算符: in, not in

身份运算符: is, is not

# 成员运算符
# in,not in
print("123" in "123456")
print(12 in [1, 12, 3, 5])
print(1 not in [1, 2])
print("***" * 10)
# 判断某个月份 是否是31天
m = 1
mouths = [1, 3, 5, 7, 8, 10, 12]
print(m in mouths)
print("***" * 10)

# 身份运算符
# is,is not     判断内存地址是否相同
a = 100
b = 100
print(id(a))    # id() 查看内存地址
print(id(b))
print(a is b)   # True
print("abc" is "abc")      # True
print("abc" is not "abcd")  # True
print("abc" is "abcd")  # False
6.位运算符
# 位运算符      二进制展开
print(6 & 3)    # 位与 110 011   010  结果为 2
print(6 | 3)    # 位或 110 011   111  结果为 7
print(6 ^ 3)    # 位异或   110 011 101 结果为 5
print(~6)
# 按位取反  结果为-7
# 00000000 0000000 0000000 00000110 =>6
# ~6
# 11111111 1111111 1111111 11111001     补码
# 11111111 1111111 1111111 11111000     反码  第一位为符号位 1为负 0为正 补码-1
# 10000000 0000000 0000000 00000111     原码  反码+1
# 正数 原码,反码,补码 一样
print(6 << 2)   # 左移    0000 0110 11左移 两位 0001 1000 结果为24
print(6 >> 2)   # 右移   11右移两位 0000 0001 结果为1

三、if分支

顺序结构:代码从上往下依次执行

分支结构:根据不同的条件,执行不同的语句

循环结构: 根据指定的条件,重复执行某段代码

1.if单分支

if 表达式:

​ 执行语句

说明;要么执行,要么不执行,当表达式成立的之后,则执行语句;如果表达式不成立,则直接跳过整个if语句继续执行后面的代码

# 1.if语句
num1 = 30
num2 = 50
if num1 != num2:
    num1 = 100
print(num1)
2.if else 语句

语法:

if 表达式:

​ 执行语句1

else:

​ 执行语句2

说明:如果表达式成立,则执行语句1;如果不成立,则执行语句2

# 2.if else语句
a = int(input("请输入一个数:"))
if a % 2 == 0:
    print(a, "是偶数")
else:
    print(a, "不是偶数")
print()
3.if-elif-else分支语句

语法:

if 表达式1:

​ 执行语句1

elif 表达式2:

​ 执行语句2

elif 表达式3:

​ 执行语句3

。。。。。

else:

​ 执行语句n

说明:实现了多选一的操作,会根据不同的条件从上往下来进行匹配,如果匹配上了,则执行对应的语句,然后直接结束整个if-elif语句,但是,如果所有的条件都不成立的话,则执行else后面的语句

注意:不管if-elif-else有多少个分支,都只会执行其中的一个分支

# 3. if-elif-else分支语句
age = int(input("请输入年龄:"))
if age < 3:
    print("婴儿")
elif age < 6:
    print("儿童")
elif age < 12:
    print("青少年")
elif age < 18:
    print("青年")
else:
    print("hello")
print()

score = int(input("请输入成绩:"))

if score < 60:
    print("不及格")
elif score < 70:
    print("及格")
elif score < 80:
    print("中等")
elif score < 90:
    print("良好")
elif score <= 100:
    print("优秀")
else:
    print("成绩不合法")
print()
4.if嵌套

语法:

if 表达式1:

​ 执行语句1

​ if 表达式2:

​ 执行语句2

说明:if语句的嵌套,可以在单分支,双分支,多分支之间进行任意组合

# 4. if嵌套
if score < 0 or score > 100:
    print("成绩不合法,请重新输入")
else:
    if score < 60:
        print("不及格")
    elif score < 70:
        print("及格")
    elif score < 80:
        print("中等")
    elif score < 90:
        print("良好")
    elif score <= 100:
        print("优秀")
    else:
        print("成绩不合法")
print()

练习题

# 从控制台输入圆的半径,计算周长和面积, π=3.14
# 1.
a = float(input("请输入圆的半径:"))
print("圆的周长为:", a * 3.14 * 2)
print("圆的面积为:", a * a * 3.14)
print()

# 2.一辆汽车以40km/h的速度行驶,行驶了45678.9km,求所用的时间
b = 45678.9 / 40
print("汽车所用时间了:", b, "小时")
print()

# 3.华氏温度转摄氏温度
# 【提示:将华氏温度转换为摄氏温度(F是华氏温度)  F = 1.8C + 32】
c = float(input("请输入当前华氏温度:"))
d = (c - 32) / 1.8
print("当前摄氏度为:", round(d))
print()

# 4.入职薪水10K,每年涨幅入职薪水的5%,50年后工资多少?
salary = 10
e = salary + salary * 0.05 * 50
print("50年后工资是:", round(e), "K")
print()

# 5.为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时?
f = 89 // 24
g = 89 % 24
print("共", f, "天", g, "个小时")
print()

# 6.给定一个5位数,分别把这个数字的万位, 千位,百位、十位、个位算出来并显示。如: 34567
h = 34567
t = 0
j = 5
for i in range(j):
    t = h % 10 ^ (j-i) // 10 ^ (j-i-1)
    print(t, end=' ')

# if
# 1.x 为 0-99 取一个数,y 为 0-199 取一个数,
#   如果 x>y 则输出 x的值,
#   如果 x 等于 y 则输出 x+y的值,
#   否则输出y的值
from random import randint
x = randint(0, 99)
y = randint(0, 199)
if x > y:
    print("x的值为:", x)
elif x == y:
    print("x+y的值为:", x+y)
else:
    print("y的值为:", y)
print()

# 2.从控制台输入一个三位数,如果是水仙花数就打印“是水仙花数”,否则打印“不是水仙花数”
# 该数的每一位的立方和等于自身的值,比如:153=1^3+5^3+3^3
# 	例如:153=1^3+5^3+3^3
# 	n = 153:
# 	个位:n%10
# 	十位:(n//10)%10
# 	百位:n//100
a = int(input("请输入一个三位数:"))
b = (a // 100) ** 3 + ((a // 10) % 10) ** 3 + (a % 10) ** 3
if a == b:
    print("是水仙花数")
else:
    print("不是水仙花数")
print()

# 3.从控制台输入一个五位数,如果是回文数就打印“是回文数”,否则打印“不是回文数”
#  回文数: 对称的5位数
# 	例如:11111   12321   12221
num1 = int(input("请输入一个五位数:"))
q = num1 // 10000
w = num1 % 10
e = num1 // 1000 % 10
r = num1 // 10 % 10
if q == w and e == r:
    print("是回文数")
else:
    print("不是回文数")
print()

# 4.从控制台输入两个数,输出较大的值
t1 = int(input("请输入第一个数:"))
t2 = int(input("请输入第二个数:"))
s = t1 if t1 > t2 else t2
print(s)
print()

# 5,成绩判定
# 	大于85 优秀
# 	大于等于75小于等于85 良好
# 	大于等于60小于75 及格
# 	小于60  不及格
score = int(input("请输入成绩:"))
if score < 0 or score > 100:
    print("成绩不合法")
else:
    if score < 60:
        print("不及格")
    elif score < 75:
        print("及格")
    elif score <= 85:
        print("良好")
    else:
        print("优秀")
print()

# 进阶
# 1,判断一个年份是闰年还是平年;
#  (1.能被4整除而不能被100整除.(如2004年就是闰年,1800年不是.)
#   2.能被400整除.(如2000年是闰年))
year = int(input("请输入年份:"))
if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
    print(year, "是闰年")
else:
    print(year, "是平年")
print()

# 2,输入一个月份,然后输出对应月份有多少天,将天数输出(不考虑闰年)
# 比如:
# 输入 6 输出为30
# 输入 2 输出为28
mouth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
n = int(input("请输入月份:"))
if 0 < n < 13:
    print(mouth[n])
else:
    print("月份错误")
print()

# 3. 开发一款软件,根据公式(身高-108)*2=标准体重,可以有10斤左右的浮动。
# 来观察测试者体重是否合适, 输入真实身高(cm),真实体重(斤)
height = int(input("请输入真实身高(cm):"))
weight = int(input("请输入真实体重(斤):"))
bweight = (height - 108) * 2
if weight - 10 > bweight:
    print("体重过重")
elif weight + 10 < bweight:
    print("体重过轻")
else:
    print("体重正常")
print()

# 4.模拟玩骰子游戏,根据骰子点数决定什么惩罚【例如:1.跳舞,2.唱歌....】
# 随机取1~6中的某一个整数
from random import randint
n = randint(1, 6)
print(n)
if n == 1:
    print("跳舞")
elif n == 2:
    print("唱歌")
elif n == 3:
    print("真心话")
elif n == 4:
    print("大冒险")
elif n == 5:
    print("没啥事")
else:
    print("再来一次")
print("****" * 10)


# 1.分别输入某年某月某日,判断这一天是这一年的第几天?(考虑闰年) (*****)
#     year, month, day
#     提示: 使用多个if单分支
mouths = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
year = int(input("请输入年份:"))
if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
    mouths[2] = 29
else:
    mouths[2] = 28
mouth = int(input("请输入月份:"))
day = int(input("请输入日期:"))
count = day    # 计算总天数
if mouth > 1:
    count += 31
if mouth > 2:
    count += mouths[2]
if mouth > 3:
    count += 31
if mouth > 4:
    count += 30
if mouth > 5:
    count += 31
if mouth > 6:
    count += 30
if mouth > 7:
    count += 31
if mouth > 8:
    count += 31
if mouth > 9:
    count += 30
if mouth > 10:
    count += 31
if mouth > 11:
    count += 30
print("这是第", count, "天")
print("****" * 10)

# 2,输入一个时间,输出该时间的下一秒: (*****)
# 	如:23:59:59,
# 	输入:hour, min, sec
# 	输出 0: 0: 0
hour = int(input("请输入小时:"))
minute = int(input("请输入分钟:"))
sec = int(input("请输入秒:"))
sec += 1
if sec == 60:
    sec = 0
    minute += 1

    if minute == 60:
        minute = 0
        hour += 1

        if hour == 24:
            hour = 0
print("%2d:" % hour, "%2d:" % minute, "%2d" % sec)

# 秒转变 天 时 分 秒
t = 456789
day = t // (60 * 60 * 24)
hour = t // (60 * 60) % 24
minute = t // 60 % 60
sec = t % 60
print("%d天%d时%d分%d秒" % (day, hour, minute, sec))
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值