# 1. 输入 print("****** abc *******",end="") print("****** 123 *******") # == > ****** abc ************* 123 ******* 备注: end=""不换行 # 2. 运算符 a = input("Please input the first number:") b = input("Please input the second number:") print("The result %s + %s = %d"%(a,b,(int(a)+int(b)))) # == > The result 10 + 23 = 33 # 3. list arrayList = ["abc","123",[123,"345"],[["se","df"]]] a1 = arrayList[0] a2 = arrayList[2][0] a3 = arrayList[3][0][0] print("a1=%s\na2=%d\na3=%s"%(a1,a2,a3)) # == > a1=abc a2=123 a3=se # 4. 计算体重 a4 = 1.75 a5 = 80.5 BMI = a4 / a5**a5 if BMI > 32 : print("严重肥胖") elif BMI > 28: print("过重") elif BMI < 18.5: print("过轻") else : print("正常") # == > 过轻 # 5. 循环遍历 names = ["a","b","c"] for name in names: print("name=%s"%name) # == > name=a name=b name=c sum = 0 for x in [1,23,3,3,3,3,3,0,5,5,5,5]: sum += x print("sum=%d"%sum) # == > sum=59 # 6. 计算1-100整数的和 listArray = list(range(101)) sum6 = 0 for s6 in listArray: sum6 += s6 print("0~100的整数和:%d",sum6) # == > 0~100的整数和:%d 5050 # 7. while循环计算 sum7 = 0 a7 = 0 while a7 < 101: sum7 = sum7 + a7 a7 += 1 print("while循环:%d",sum7) # == > while循环:%d 5050 # 练习请利用循环依次对list中的每个名字打印出Hello, xxx!: L = ["Python","Objective-C","Swift"] for L1 in L: print("Hello,%s"%L1) # == > Hello,Python Hello,Objective-C Hello,Swift # 8.字典 dictionary = {"WebApp":"Python","iOS":"Objective-C","Android":"java"} dict8 = dictionary["WebApp"] # 拿到字典中key为WebApp对应的Value print("拿到字典中key为WebApp对应的value:%s"%dict8) # == > 拿到字典中key为WebApp对应的value:Python bool = "iOS" in dictionary # 判断key是否在字典中 print("判断key是否在字典中不存在%d"%bool) # == > 判断key是否在字典中不存在1 a8 = dictionary.get("PHP",-1) # 字典中的key不存在的话输出-1 print("字典中的key是否存在不存在输出自己设定的值:%d"%a8) # == > 字典中的key是否存在不存在输出自己设定的值:-1 s8 = dictionary.pop("iOS") print("用pop删除key对应的value为%s"%s8) # == > 用pop删除key对应的value为Objective-C # 9.set:set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key s = set(["1","2","3"]) print("打印一个set",s) # == > 打印一个set {'2', '1', '3'} s = set(["1","2","3","3","2","1"]) print("set可以去重",s) # == > set可以去重 {'2', '1', '3'} # add(key)方法可以添加元素到set中,可以重复添加,但不会有效果 s.add(4) print("输出添加元素后的set",s) # == > 输出添加元素后的set {'2', 4, '1', '3'} # 通过remove(key)方法可以删除元素: s.remove(4) print("输出移除后的代码",s) # == > 输出移除后的代码 {'2', '1', '3'} s1 = set([1,2,3]) s2 = set([2,3,4]) s3 = s1 & s2 print("交集结果",s3) # == > 交集结果 {2, 3} s4 = s1 | s2 print("并集结果",s4) # == > 并集结果 {1, 2, 3, 4} # 10.排序 sortArray = ["3","2","1"] sortArray.sort() print("排序后的数组:",sortArray) # == > 排序后的数组: ['1', '2', '3'] # 练习: 九九乘法表 m = 1 while m <= 9: n = 1 while n <= m: print("%d*%d=%-2d "%(n,m,m*n),end="") n += 1 print("") m += 1 #1*1=1 #1*2=2 2*2=4 #1*3=3 2*3=6 3*3=9 #1*4=4 2*4=8 3*4=12 4*4=16 #1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 #1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 #1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 #1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 #1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81