本文共 3706 字,大约阅读时间需要 12 分钟。
上周报名了reboot python 课程,终于下决心要把python 搞好了,希望自己能坚持下来,并得到自己想要的成绩
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #coding:utf-8 #呵呵 #print 'hello world' #x='hello world' #print x #x=raw_input('hello world') #print x #int #print 2+3 #print 1+2*3 #print 8/3 #print 8/3.0 #print 8%3 #str 单双引号无却别 print 'hello world' print "hello world" print "I'am pythoner" print 'I\'am pythoner' #三个单引号 起声明作用 print ''' this is a test ''' #字符串拼接 print 'hello ' + 'reboot' print 'hello ' * 2 #字符串格式化 #数字(0, 1, ...)即代表format()里面的元素, 所以可以使用"."调用元素的方法; x = 'fujinzhou' y = 22 h = 'good' print 'Hi %s I am %d years old ,You are very %s' % (x,y,h) print 'Hi ' + x + ',You are very ' + h + '' print ( 'Hi {0} I am {1} years old,You are very {2}!' . format (x,y,h)) #布尔值 True False 与或非 #and #两边都是真,才是真 #print True and True #print 2>3 and 3>2 #or 或 ,两种情况只要有一种情况 #print True or False #not 非 #print not False #print not True #流程控制 #if True or False: # 是True 就执行这段代码 if 2 < 3 or 5 < 4 : print 'condition is True' else : print 'condition is False' name = 'reboot' age = 20 if name = = 'reboot' : if age> 10 : print 'you are %s years old' % (age) print 'condition is reboot' else : print 'condition is False' #while 循环 #while 情况1: #里面的代码会一直执行,直到情况1是false #i=0 #while i<20: #print i #i+=1 #print 'while is end' #用户一直输入数字,当输入0时。终止并输出数字之和 count = 0 while True : i = int ( raw_input ( 'please inut your number:' )) count + = i if i = = 0 : print count break ; count = 0 y = raw_input ( 'please input a number' ) while int (y)! = 0 : count = count + int (y) y = raw_input ( 'please input a number' ) print 'the total is %d' % (count) #用户一直输入数字,当输入为空时。终止并输出平均值 count = 0 total = 0 y = raw_input ( 'please input a number' ) while y: count = count + int (y) y = raw_input ( 'please input a numbers:' ) total + = 1 print 'the total is %s' % (count / total) #for 循环 专门针对list dict等结构 #arr=['1','2','three'] #for i in arr: # print i #找出列表中js出现的次数 #num=0 #list1=['c','python','js','node','java','js','ruby'] #for i in list1: #if i =='js': # num+=1 #print num #numlist=[1,2,3,2,12,3,1,3,21,2,2,3,4111,22,3333,444,111,4,5,777,65555,45,33,45] #max1=0 #max2=0 #for i in numlist: #if max1<i: #max1=i #for i in numlist: #if max1==i: #continue #if max2<i: #max2=i #print max1,max2 #i=0 #while True: #if i >10: #break #print i #i+=1 #arr=['c','python','js','node'] #for i in arr: #if i=='js': #continue #print i #d ={'name':'reboot','age':50} #print d['name'] #根据key 获取 value #d['age']=50 #修改值 #print d #d['newkey']='key1' #增加新值 #print d #total=0 b = {} a = [ 'C' , 'js' , 'python' , 'js' , 'css' , 'js' , 'html' , 'node' , 'js' , 'python' , 'js' , 'css' , 'js' , 'html' , 'node' , 'js' , 'python' , 'js' , 'css' , 'js' ] for i in a: if i in b: b[i] + = b[i] + 1 else : b[i] = 1 print b |
小练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #coding:utf-8 #用户输入名字和分数,并保存到list。如果输入为空,打印并结束循环。并算出平均值 list1 = [] count = 0 num = 0 while True : x = raw_input ( 'please input your name:' ) y = raw_input ( 'please input your grade:' ) if y.isdigit() and x.isalpha(): list1.append( int (y)) list1.append( str (x)) count + = 1 num = int (num) + int (y) elif len (x) = = 0 or len (y) = = 0 : avg = num / count print list1 break else : print 'input is error' print '平均值是 %s' % (avg) |