一、变量、运算符与数据类型1. 注释
2. 运算符1. 位运算符
运算符的优先级
2. 数据类型与转换
通过 print() 可看出 a 的值,以及类 (class) 是int a = 1031 print(a, type(a)) # 1031 <class 'int'> type可以帮助我们去获取到它的类型,再通过print()打印出来。
Python 里面有很多用途广泛的包 (package),用什么你就引进 【例子】使 1/3 保留 5 位,用 import decimal from decimal import Decimal decimal.getcontext().prec = 5 c = Decimal(1) / Decimal(3) print(c) 布尔 (boolean) 型变量只能取两个值,True 和 False。当把布尔型变量用在数字运算中,用 1 和 0 代表 True 和 False 类型转换
3. print() 函数
没有参数时,每次输出后都会换行。 shoplist = ['apple', 'mango', 'carrot', 'banana'] print("This is printed without 'end'and 'sep'.") for item in shoplist: print(item)
# This is printed without 'end'and 'sep'. # apple # mango # carrot # banana
shoplist = ['apple', 'mango', 'carrot', 'banana'] print("This is printed with 'sep='&''.") for item in shoplist: print(item, 'another string', sep='&')
# This is printed with 'sep='&''. # apple&another string # mango&another string # carrot&another string # banana&another string 二、条件语句1. if 语句
temp = input("猜一猜小哥哥帅不帅?") guess = int(temp) # input 函数将接收的任何数据类型都默认为 str。 if guess == 帅: print("你太了解小姐姐的心思了!") print("哼,猜对也没有奖励!") else: print("猜错了,小姐姐现在心里想的是666!") print("游戏结束,不玩儿啦!")
temp = input('请输入成绩:') source = int(temp) if 100 >= source >= 90: print('A') elif 90 > source >= 80: print('B') elif 80 > source >= 60: print('C') elif 60 > source >= 0: print('D') else: print('输入错误!')
my_list = ['lsgogroup'] my_list.pop(0) assert len(my_list) > 0
# AssertionError 三、循环语句1.while语句while语句最基本的形式包括一个位于顶部的布尔表达式,一个或多个属于while代码块的缩进语句,代码块会一直循环执行,直到布尔表达式的值为布尔假。。 string = 'abcd' while string: print(string) string = string[1:]
# abcd # bcd # cd # d 如果布尔表达式不带有<、>、==、!=、in、not in等运算符,仅仅给出数值之类的条件,也是可以的。当while后写入一个非零整数时,视为真值,执行循环体;写入0时,视为假值,不执行循环体。也可以写入str、list或任何序列,长度非零则视为真值,执行循环体;否则视为假值,不执行循环体。 当while循环正常执行完的情况下,执行else输出,如果while循环中执行了跳出循环的语句,比如 break,将不执行else代码块的内容。 count = 0 while count < 5: print("%d is less than 5" % count) count = count + 1 else: print("%d is not less than 5" % count)
# 0 is less than 5 # 1 is less than 5 # 2 is less than 5 # 3 is less than 5 # 4 is less than 5 # 5 is not less than 5 2.for 语句for循环是迭代循环,在Python中相当于一个通用的序列迭代器,可以遍历任何有序序列,如str、list、tuple等,也可以遍历任何可迭代对象,如dict。 member = ['张三', '李四', '刘德华', '刘六', '周润发'] for each in member: print(each)
# 张三 # 李四 # 刘德华 # 刘六 # 周润发 每次循环,迭代变量被设置为可迭代对象的当前元素,提供给代码块使用。 当for循环正常执行完的情况下,执行else输出,如果for循环中执行了跳出循环的语句,比如 break,将不执行else代码块的内容,与while - else语句一样。 for num in range(10, 20): # 迭代 10 到 20 之间的数字 for i in range(2, num): # 根据因子迭代 if num % i == 0: # 确定第一个因子 j = num / i # 计算第二个因子 print('%d 等于 %d * %d' % (num, i, j)) break # 跳出当前循环 else: # 循环的 else 部分 print(num, '是一个质数')
# 10 等于 2 * 5 # 11 是一个质数 # 12 等于 2 * 6 # 13 是一个质数 # 14 等于 2 * 7 # 15 等于 3 * 5 # 16 等于 2 * 8 # 17 是一个质数 # 18 等于 2 * 9 # 19 是一个质数 range 这个BIF的作用是生成一个从start参数的值开始到stop参数的值结束的数字序列,该序列包含start的值但不包含stop的值。 for i in range(1, 10, 2): print(i)
# 1 # 3 # 5 # 7 # 9 3.跳出循环语句break语句可以跳出当前所在层的循环。 import random secret = random.randint(1, 10) #[1,10]之间的随机数
while True: temp = input("猜一猜小姐姐想的是哪个数字?") guess = int(temp) if guess > secret: print("大了,大了") else: if guess == secret: print("你太了解小姐姐的心思了!") print("哼,猜对也没有奖励!") break else: print("小了,小了") print("游戏结束,不玩儿啦!") continue终止本轮循环并开始下一轮循环。 for i in range(10): if i % 2 != 0: print(i) continue i += 2 print(i)
# 2 # 1 # 4 # 3 # 6 # 5 # 8 # 7 # 10 # 9 四、异常处理1. Python 标准异常总结
2.Python标准警告总结
老铁,三连支持一下,好吗?↓↓↓ |
|