Python 10分钟入门 发表于 2017-02-06 | BB两句遇见一门新语言,你会发现其实和以前的还是有许多共同之处的,但不同的语言之间还是有些细微的差别的,如果不清楚用起来就很恼火。 不多BB了直接看总结123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143# -*- coding: utf-8 -*-#数据类型print 123 #整数 intprint 1.23e3 #浮点数 floatprint 0xff00 #16进制print 'MFS'+"mfs" #字符串print 'Bob said \"I\'m OK\".'print 1==2 #布尔值 False#字符串# print 字符之间可以用 ',' 或 '+' 连接,但是 ',' 表示一个空格print 'Bob said \"I\'m OK\".' #字符串中的反斜杠 '\' 转义特殊字符print r'''"To be, or not to be": that is the question.Whether it's nobler in the mind to suffer.''' #字符串中 Raw 字符串 中的特殊字符不用转义 '''...''' 用来打印多行字符串print u'中文' #打印中文要在字符串前加'u'#布尔值 not and or # 'and' , 'or' 采取短路计算,按顺序运算,由谁决定 T or F 输出谁>>> print 1 or 21>>> print 0 and 20>>> print 0 or 22 #变量定义x1 = 1 #因为是动态编程,变量的类型是可以随意转化的,不必声明变量的类型d = 3n = 100x100 = x1+d*(n-1)s = (x100+x1)*n/2print s#list>>> L = ['Adam',95.5,'Lisa',85,'Bart',59,[1,2,3]] #因为是动态编程,L 中每一个值是随意的>>> print L['Adam', 95.5, 'Lisa', 85, 'Bart', 59, [1, 2, 3]]>>> L=[1,3,5] #索引有负值,代表从后向前>>> print L[0],L[1],L[2]1 3 5>>> print L[-1],L[-2],L[-3]5 3 1L.append(7) #将 7 添加在list尾部L.insert(2,7) #将 7 添加在索引为 2 的位置,其余元素后移L.pop(2) #弹出索引为 2 的元素,并返回这个元素#tuplet = ('Adam',) #一个元素要多写 ','t = ('a', 'b', ['A', 'B']) #tuple 是不可改变的,但是如果tuple中放了 list,则其中的 list 可变#条件判断和循环#ifscore = 85if score>=90: print 'excellent'elif score>=80: print 'good'elif score>=60: print 'passed'else: print 'failed'#forL = [75, 92, 59, 68]sum = 0.0for s in L: sum = sum+sprint sum / 4#while breaksum = 0x = 1n = 1while True: if n==21: break sum+=x x*=2 n+=1print sum#dict (类似 C 的 map)#遍历dictd = { 'Adam': 95, 'Lisa': 85, 'Bart': 59}for k in d: print k,':',d[k]#set (去重,无序,内部元素不可变)weekdays = set(['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN'])x = 'MFS'if x in weekdays: print 'input ok'else: print 'input error' #更新weekdays.add('MFS')weekdays.remove('MFS')#1. 有序集合:list,tuple,str和unicode;#2. 无序集合:set#3. 无序集合并且具有 key-value 对:dict#函数def average(*args): #可变参数的名字前面有个 * 号,可以传入0个、1个或多个参数给可变参数 if not args: return 0.0 else: return sum(args)*1.0/len(args)print average()print average(1, 2)print average(1, 2, 2, 3, 4)#切片>>> L = range(1,100)>>> print L[1:10] #从索引 1 - 9[2, 3, 4, 5, 6, 7, 8, 9, 10]>>> print L[2:10:3] #从索引 2 - 10 ,没隔三个取一个[3, 6, 9]# enumerate() 函数L = ['Adam', 'Lisa', 'Bart', 'Paul']enumerate(L) = [(0, 'Adam'), (1, 'Lisa'), (2, 'Bart'), (3, 'Paul')]#values 和 itervaluesd = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }d.values() = [85, 95, 59] #itervalues 与 values 作用相同但是节省内存#items 和 iteritems>>> d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }>>> print d.items()[('Lisa', 85), ('Adam', 95), ('Bart', 59)]#zipprint [x*y for x,y in zip(range(1,100,2),range(2,101,2))] #生成列表 [1x2, 3x4, 5x6, 7x8, ..., 99x100]print [x*100+y*10+z for x in range(1,10) for y in range(0,10) for z in range (1,10) if x==z] #利用 3 层for循环的列表生成式,找出对称的 3 位数对称数。