(1)格式化输出(%% 第一个% 转译)
1 # 格式化 输入 输出 2 name = input("Name:") 3 age = input("Age:") 4 job = input("Job:") 5 hobbies = input("Hobbies:") 6 7 info = ''' 8 ---------index %s ------------- 9 Name : %s 10 Age : %s 11 job : %s 12 Hobbies: %s 13 ''' % (name, name, age, job, hobbies)14 15 print(info)16 17 # 如果输入的时候必须需要输入% 则输入%% 可以实现转义功能18 msg = "我是%s,年龄%d,目前学习进度为80%%" % ('shine_rainbow', 18)19 # result 我是shine_rainbow,年龄18,目前学习进度为80%20 print(msg)
(2)基本数据类型1
1 # int 2 a = 2 ** 3 3 print(type(a)) 4 b = 2 ** 60 5 print(b) 6 # str 7 name = "my name is alex" 8 age = "2" 9 msg = name+"\n"+age+"岁了\n"+"学习python 非常开心 开 心 开 心 心 "10 print(msg)11 # 对于input 从键盘上获取的内容,均为字符型12 # name = input("请输入贵姓")13 # print(name)14 # print(type(name))15 # 注意 在字符串拼接中str + str 为字符串拼接 str * int 为将这个内容复制多少次,在拼接在一起16 print("hello world\n" * 10) # 打印10次hello world17 # 字符串的拼接只能是双方都是字符串, 不可以跟数字和其他类型拼接18 # print("hello"+2)19 # 布尔类型20 21 print(3 > 4)22 23 # 格式化输出24 """25 需求:询问用户的姓名、年龄、工作、爱好,然后打印成如下格式:26 ----------------index of Alex Li----------------27 Name : Alex Li28 Age : 2229 job : Teacher30 Hobbies : girl31 ---------------end-------------------32 """33 # name = input("please input name")34 # age = int(input("please input age")) # 将str--->int35 # job = input("please input job")36 # hobbies = input("please input hobbies")37 # msg =38 # """39 # ----------------index of %s----------------40 # Name : %s41 # Age : %s42 # job : %s43 # Hobbies : %s44 # ---------------end-------------------45 # """ % (name, name, age, job, hobbies)46 # print(type(age))47 # print(msg)
(3)基本逻辑语句
1 # 逻辑运算 1.在没有()的情况下,not 优先级高于and and 高于or 即优先级()>not > and >or 同一优先级从左到右计算 2 print(3 > 4 or 4 < 3 and 1 == 1) 3 # f or f print (false) 4 print(1 < 2 and 3 < 4 or 1 > 2) 5 # t or f print(true) 6 print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) 7 # t or f print(true) 8 # 求出下列逻辑语句的值 9 # x or y if x is false then y else x10 print(8 or 4)11 12 print(0 and 3)13 # if x is false then true else false14 # x and y ,x true then y ,x false x15 print(0 or 4 and 3 or 7 or 9 and 6)16 # 0 or 3 or 7 or 617 # 3 or 7 or 618 # 3 or 619 # 620 # 判断子元素是否在原字符串(字典、列表、集合中)21 print('喜欢' in 'jdljs;k;as喜欢')22 print('a' in 'asdfds')23 print('y' not in 'avd')24 # true true true
(4)流程控制
1 # if : 2 if 3 > 4: 3 print("1111") 4 else: 5 print("222") 6 # if 多分支测试 7 score = int(input('请输入您的分数')) 8 if score > 100: 9 print("神童")10 elif score >= 90:11 print("A")12 elif score >= 80:13 print("B")14 elif score >= 60:15 print("C")16 elif score > 10:17 print("D")18 else:19 print("成绩低于10 太难受了。")
(5)循环控制
1 # while循环 2 # 1-100的求和 3 # count = 0 4 # result = 0 5 # while count < 100: 6 # result += count 7 # count += 1 8 # print(result) 9 # 只要求出5-95之前的数字 注意使用continue10 count = 011 result = 012 while count < 10:13 count += 114 if count > 3 and count < 6:15 continue16 result += count17 18 print(result)
第一天测试题:
1. 使用while 循环输出 1 2 3 4 5 6 8 9 10
2.求1-100的所有数的和
3.输出1-100 内的所有奇数
4.1-2+3-4+5-...+99
5.三次登录
1 # 使用while 循环输出 1 2 3 4 5 6 8 9 10 2 # count = 0 3 # while count < 10: 4 # count += 1 5 # if count == 7: 6 # continue 7 # print(count) 8 # print("----end----") 9 # 求1-100的所有数的和10 # result = 011 # while count < 100:12 # count += 113 # result += count14 # print(result)15 # 输出1-100 内的所有奇数16 # result = 017 # while count < 100:18 # count += 119 # if count % 2 == 0:20 # continue21 # result += count22 # print(result)23 # 或24 # count = 125 # while count < 100:26 # result += count27 # count += 228 # print(result)29 # 1-2+3-4+5-...+9930 # count = 131 # sum1 = 032 # flag = 133 # while count < 100:34 # if count % 2 == 0:35 # flag = -136 # else:37 # flag = 138 # sum1 = sum1 + count * flag39 # count += 140 # print(sum1)41 # 三次登录42 username = "root"43 password = "123456"44 count = 345 while count > 0:46 count -= 147 input_username = input("请输入用户名:")48 input_password = input("请输入密码:")49 if input_username.__eq__(username):50 if input_password.__eq__(password):51 print("登录成功,当前登录用户%s"%(username))52 break53 else:54 print("登录失败,你还有%d次机会" %(count))55 else:56 print("登录失败,你还有%d次机会" %(count))