pythonday02基础与运算符
今日概要
1.循环
2.字符串格式化
3.运算符
4.编码
if的嵌套
score = input('请输入成绩') score_int = int(score) if score_int >= 90: print('A') elif score_int >=80: print('B') elif score_int >=70: print('C') else: print('D')
10086示例
message = """欢迎致电10086 1.话费查询; 2.流量服务; 3.业务办理; 4.人工服务""" print(message) index = input('请输入你要选择的服务:') index = int(index) if index==1: print('话费查询') elif index == 2: print('流量服务') elif index == 3: content = """业务办理 1. 修改密码; 2. 更改套餐; 3. 停机;""" print(content) value = input('请输入要办理的业务:') value = int(value) if value == 1: print('修改密码') elif value == 2: print('更改套餐') elif value == 3: print('停机') else: print('错误') elif index == 4: print('人工服务') else: print('输入错误')
循环语句
# 1. 循环打印 “人生苦短,我用Python。” """ while True: print('人生苦短,我用Python。') """ # 2. while后加入条件 """ while 1>0 and 2>1: print('人生苦短,我用Python。') """ # 3. 数字相加 """ count = 1 value = count + 1 print(value) """ """ count = 1 count = count + 1 print(count) """ # 4. 请通过循环,让count每次循环都 + 1 . """ count = 1 while True: print(count) count = count + 1 """ # 练习 """ while True: count = 1 print(count) count = count + 1 """ # 5. 请通过循环,1 2 3 .. 10. """ count = 1 while count <= 10: print(count) count = count + 1 print('结束') """ # 6. 请通过循环,1 2 3 4 5 6 8 9 10. # 快速注释 ctrl+? """ # 错误示例 count = 1 while count <= 10 and count != 7 : print(count) count = count + 1 """ """ # 正确 count = 1 while count <= 6: print(count) count = count + 1 count = 8 while count <= 10: print(count) count = count + 1 """ """ # 正确 count = 1 while count <= 10: if count != 7: print(count) count = count + 1 """ """ # 正确 count = 1 while count <= 10: if count == 7: pass else: print(count) count = count + 1 """ # 7. 关键字:break """ while True: print(666) break # 终止当前循环 print('结束') """ # 练习: """ # 通过break实现 1 ~ 10 count = 1 while True: print(count) if count == 10: break count = count + 1 print('结束') """ """ # break是终止当前循环 while True: print('你好') while True: print(666) break break """ # 8. 关键字:continue """ count = 1 while count <=10: print(count) continue # 本次循环如果遇到continue,则不在继续往下走,而是回到while条件位置。 count = count + 1 """ """ 示例:1234568910 count = 1 while count <=10: if count == 7: count = count + 1 continue print(count) count = count + 1 """ # 9. while else """ count = 1 while count < 10: print(count) count = count + 1 else: # 不再满足while后的条件时,触发。 或 条件=False print('ELSE代码块') print('结束') """ """ count = 1 while True: print(count) if count == 10: break count = count + 1 else: # 不再满足while后的条件时,触发。 或 条件=False print('ELSE代码块') print('结束') """
字符串格式化
# 字符串格式化存在的意义 """ name = input('姓名:') do = input('在干什么:') template = "%s在教室,%s。" %(name,do,) print(template) """ # 直接做占位符 # template = "我是%s,年龄%s, 职业%s。" %("alex",73,'讲鸡汤',) # print(template) # template = "我是%s,年龄%d, 职业%s。" %("alex",73,'讲鸡汤',) # print(template) # name = 'alex' # template = "%s现在手机的电量是100%%" %(name,) # print(template) # 练习 name = input('请输入姓名:') age = input('请输入年龄:') job = input('请输入职业:') hobby = input('请输入爱好:') msg = ''' ------------ info of Alex Li ---------- Name : %s Age : %s job : %s Hobbie: %s ------------- end ----------------''' data = msg %(name,age,job,hobby,) print(data)
运算符
# ####################### 算数运算符 ####################### # value = 11 % 3 # print(value) # 练习题:打印 1 ~ 100 之间的奇数。 # count = 1 # while count <= 100: # val = count % 2 # if val == 1: # print(count) # count = count + 1 # val = 2**8 # print(val) # val = 9//2 # print(val) # 练习题: 1 ~ 100 之间所有的数相加。 # total = 0 # count = 1 # while count <=100: # total = total + count # count = count + 1 # print(total) # ####################### 赋值运算 ####################### # count = 1 # while count <=100: # print(count) # count +=1 # count = count + 1 # ####################### 逻辑运算 ####################### # 一般情况下 # if 1 > 0 and 1 > 2: # print('666') # 二般情况下 ## 小知识 # - int # - str # - bool # 数字转字符串 # v1 = 666 # v2 = str(v1) # 字符串转数字 # v1 = "666" # v2 = int(v1) # 数字转布尔值 # v1 = 0 # v2 = bool(v1) # print(v2) # 字符串转布尔值 # v1 = "" # v2 = bool(v1) # print(v2) # 布尔值转换其他 # v1 = True # v2 = str(v1) # print(v2) # 需要掌握的转换知识点 """ - 字符串转数字 - 数字转字符串 - "" / 0 转换布尔之后是False """ # 对于 or,如果有遇到 """ 对于 or,如果有遇到 value= 1 or 9 第一个值如果是转换成布尔值如果是真,则value=第一值。 第一个值如果是转换成布尔值如果是假,则value=第二值。 如果有多个or条件,则从左到右依次进行上述流程。 示例: v1 = 0 or 1 v2 = 8 or 10 v3 = 0 or 9 or 8 """ # 对于and,如果遇到 """ 对于and,如果遇到 value= 1 and 9 这种情况 如果第一个值转换成布尔值是True,则value=第二个值。 如果第一个值转换成布尔值是False,则value=第一个值。 如果有多个and条件,则从左到右依次进行上述流程。 示例: v1 = 1 and 9 v2 = 1 and 0 v3 = 0 and 7 v4 = 0 and "" v5 = 1 and 0 and 9 """ # 综合 # 先看and再看or # v1 = 1 and 9 or 0 and 6 # print(v1)
其他
1)优先级 在没有()的情况下not 优先级高于 and,and优先级高于or,即优先级关系为(
)>not>and>or,同一优先级从左往右计算。
2)数据类型转换
编码
编码扩展 ascii unicode ecs2 ecs4 utf-8,中文用3字节。 utf-16 # 先看and再看or # v1 = 1 and 9 or 0 and 6 # print(v1) # 数字转字符串 # v1 = 666 # v2 = str(v1) # 字符串转数字 # v1 = "666" # v2 = int(v1) # 数字转布尔值 # v1 = 0 # v2 = bool(v1) # print(v2) # 字符串转布尔值 # v1 = "" # v2 = bool(v1) # print(v2) # 布尔值转换其他 # v1 = True # v2 = str(v1) # print(v2)