# -*- coding:utf8 -*- # 3-1 Python中数据类型 #Enter a code print(45678+int('0x12fd2',16)) print('Learn Python in imooc') print(100 < 99) print(int('0xff',16)==255) # 3-2 Python之print语句 #input code print('hello,python') print('hello','python') # 3-3 Python的注释 #print ('hello') # 3-4 Python中什么是变量 #等差数列 x1 = 1 d = 3 n = 100 x100 = x1+(n-1)*d s=n*x1+n*d*(n-1)/2 print (s) # 3-5 Python中定义字符串 s = ('Python was started in 1989 by \"Guido\".\nPython is free and easy to learn.') print s # 3-6 Python中raw字符串与多行字符串 print (r'''"To be, or not to be": that is the question....Whether it's nobler in the mind to suffer.''') # 3-7 Python中Unicode字符串 # -*- coding: utf-8 -*- print ('''静夜思 床前明月光, 疑是地上霜。 举头望明月, 低头思故乡。''') # 3-8 Python中整数和浮点数 11.0 / 4 # ==> 2.75 11 / 4 # ==> 2 print (2.5 + 10.0 / 4) print (2.5 + 10 / 4) # 3-9 Python中布尔类型 a = 'python' print ('hello,', a or 'world') b = '' print ('hello,', b or 'world') #因为当运行到a时为true,计算机就不再计算后面的代码而是直接输出 # 4-1 Python创建list L = ['Adam',95.5,'Lisa',85,'Bart',59] print (L) # 4-2 4-2 Python按照索引访问list L = [95.5,85,59] print( L[0]) print( L[1]) print( L[2]) # 4-3 Python之倒序访问list L = [95.5, 85, 59] print (L[-1]) print (L[-2]) print (L[-3]) # 4-4 Python之添加新元素 #append()总是把新的元素添加到 list 的尾部。 L = ['Adam', 'Lisa', 'Bart'] L.insert(2, 'Paul') print (L) # 4-5 Python从list删除元素 L = ['Adam', 'Lisa', 'Paul', 'Bart'] L.pop(2) L.pop(2) print (L) # 4-6 Python中替换元素 L = ['Adam', 'Lisa', 'Bart'] L[-1] = 'Adam' L[0] = 'Bart' L[2]=L[-1] print (L) # 4-7 Python之创建tuple t = (0,1,2,3,4,5,6,7,8,9) print (t) # 4-8 Python之创建单元素tuple t = ('Adam',) print (t) # 4-9 Python之“可变”的tuple #令t = ('a', 'b', ['A', 'B'])不可变 t = ('a', 'b', ('A', 'B')) print t # 5-1 Python之if语句 score = 75 if score >= 60: print ('passed') # 5-2 Python之 if-else score = 55 if score >= 60: print ('passed') else: print ('failed') # 5-3 Python之 if-elif-else score = 85 if score >= 90: print ('excellent') elif score >= 80: print ('good') elif score >= 60: print ('passed') else: print ('failed') # 5-4 Python之 for循环 L = [75, 92, 59, 68] sum = 0.0 for x in L: sum += x print (sum / 4) # 5-5 Python之 while循环 sum = 0 x = 1 while x <=100: sum += x x += 2 print (sum) # 5-6 Python之 break退出循环 sum = 0 x = 1 n = 1 while True: sum += x x *= 2 n += 1 if n > 20: break print (sum) # 5-7 Python之 continue继续循环 sum = 0 x = 0 while True: x = x + 1 if x > 100: break if x%2 == 0: continue sum += x print (sum) # 5-8 Python之 多重循环 for x in ['1','2','3','4','5','6','7','8','9']: for y in ['1','2','3','4','5','6','7','8','9']: if x < y: print (x+y) # 6-1 Python之什么是dict d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul':75 } # 6-2 Python之访问dict d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 } print ('Adam: ', d.get('Adam')) print ('Lisa: ', d.get('Lisa')) print ('Bart: ', d.get('Bart')) # 6-3 Python中dict的特点 # -*- coding: utf-8 -*- d = { 95:'Adam', 85:'Lisa', 59:'Bart' } # 6-4 Python更新dict d = { 95: 'Adam', 85: 'Lisa', 59: 'Bart' } d[72] = 'Paul' # 6-5 Python之 遍历dict d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 } for key in d: print (key, ':', d[key]) # 6-6 Python中什么是set #set会自动去掉重复的元素,原来的list有4个元素,但set只有3个元素。 #创建 set 的方式是调用 set() 并传入一个 list,list的元素将作为set的元素 s = set(['Adam', 'Lisa', 'Bart', 'Paul']) # 6-7 Python之 访问set #可以用 in 操作符判断,返回true或者false s = set([name.lower() for name in ['Adam', 'Lisa', 'Bart', 'Paul']]) print 'adam' in s print 'bart' in s # 6-8 Python之 set的特点 months = set(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']) x1 = 'Feb' x2 = 'Sun' if x1 in months: print 'x1: ok' else: print 'x1: error' if x2 in months: print 'x2: ok' else: print 'x2: error' # 6-9 Python之 遍历set s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)]) for x in s: print x[0], ':', x[1] # 6-10 Python之 更新set #set的add();set的remove() s = set(['Adam', 'Lisa', 'Paul']) L = ['Adam', 'Lisa', 'Bart', 'Paul'] for x in L: if x in s: s.remove(x) else: s.add(x) print (s) # 7-2 Python之调用函数 L = range(1,101) #[1,2,...100] sum = 0 for i in L : sum = sum + i*i print sum # 7-3 Python之编写函数 def square_of_sum(L): return sum([i * i for i in L]) print square_of_sum([1, 2, 3, 4, 5]) print square_of_sum([-5, 0, 5, 15, 25]) # 7-4 Python函数之返回多值 import math def quadratic_equation(a, b, c): x1 = (-b + math.sqrt(b * b - 4 * a * c))/(2 * a) x2 = (-b - math.sqrt(b * b - 4 * a * c))/(2 * a) return x1,x2 print quadratic_equation(2, 3, 0) print quadratic_equation(1, -6, 5) # 7-5 Python之递归函数 def move(n, a, b, c): if n == 1: print a, '-->', c return move(n-1, a, c, b) print a, '-->', c move(n-1, b, a, c) move(4, 'A', 'B', 'C') # 7-6 Python之定义默认参数 def greet(x = 'world'): print ('Hello,%s.'%x) greet() greet('Bart') # 7-8 Python之定义可变参数 def average(*args): l = len(args) if l == 0:return 0.0 else: return sum(args,0.0)/l print average() print average(1, 2) print average(1, 2, 2, 3, 4) # 8-1 对list进行切片 L = range(1, 101) print L[:10] #前十个数 print L[2::3] #3的倍数 print L[4:50:5] #50以内5的倍数 # 8-2 倒序切片 L = range(1, 101) print L[-10:] #最后十个数 print L[-46::5] #最后10个5的倍数 # 8-3 对字符串切片 def firstCharUpper(s): return s.title() print firstCharUpper('hello') print firstCharUpper('sunday') print firstCharUpper('september') # 9-1 什么是迭代 for i in range(1, 101): if i%7 == 0: print (i) # 9-2 索引迭代 #zip()函数可以把两个 list 变成一个 list:zip([10, 20, 30], ['A', 'B', 'C'])->>>[(10, 'A'), (20, 'B'), (30, 'C')] L = ['Adam', 'Lisa', 'Bart', 'Paul'] for index, name in enumerate(L): print index+1, '-', name # 9-3 迭代dict的value #1. values() 方法实际上把一个 dict 转换成了包含 value 的list。 #2.但是 itervalues() 方法不会转换,它会在迭代过程中依次从 dict 中取出 value,所以 itervalues() 方法比 values() 方法节省了生成 list 所需的内存。 d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 } print sum(d.values())*1.0/len(d) d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 } sum = 0.0 for score in d.itervalues(): sum = sum + score print sum/len(d) # 9-4 迭代dict的key和value #items() 方法把dict对象转换成了包含tuple的list # items() 也有一个对应的 iteritems(),iteritems() 不把dict转换成list,而是在迭代过程中不断给出 tuple d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 } sum = 0.0 for k, v in d.items(): sum = sum + v print k, ':', v print 'average', ':', sum/len(d) # 10-1 生成列表 print [x*(x+1) for x in range(1,100,2)] #[1x2, 3x4, 5x6, 7x8, ..., 99x100] [x * x for x in range(1, 11)] #[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] # 10-2 复杂表达式 #打印表格 d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 } def generate_tr(name, score): if score < 60: return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score) return '<tr><td>%s</td><td>%s</td></tr>' % (name, score) tds = [generate_tr(name, score) for name, score in d.iteritems()] print '<table border="1">' print '<tr><th>Name</th><th>Score</th><tr>' print '\n'.join(tds) print '</table>' # 10-3 条件过滤 #isinstance(x, str) 可以判断变量 x 是否是字符串 def toUppers(L): return [x.upper() for x in L if isinstance(x, str) ] print toUppers(['Hello', 'world', 101]) # 10-4 多层表达式 #利用 3 层for循环的列表生成式,找出对称的 3 位数。例如,121 就是对称数,因为从右到左倒过来还是 121。 print [a*100+b*10+c for a in range(1,10) for b in range(0,10) for c in range(0,10) if a == c]
[慕课网 Python入门 廖雪峰]
猜你喜欢
转载自blog.csdn.net/daisy_fight/article/details/80579620
今日推荐
周排行