1: >>> greeting = 'Hello' 2: >>> salution = 'Mr.' 3: >>> name = 'Bill' 4: #以逗号分隔输出项 5: >>> print(greeting, salution, name) 6: Hello Mr. Bill 7: #在逗号前增加了一个空格符 8: >>> print(greeting, ',', salution, name) 9: Hello , Mr. Bill 10: #为了显示成'Hello, Mr. Bill'这个样式,可以使用连接符‘+’ 11: >>> print(greeting + ',', salution, name) 12: Hello, Mr. Bill 13: >>> 14:
1: #第一种导入方法 2: #导入模块 3: import module1 4: import module2 5: #调用同名函数的方法 6: module1.open() 7: module2.open() 8: 9: #第二种导入方法 10: #导入函数,并给函数取相应的别名 11: from module1 import open as open1 12: from module2 import open as open2 13:
1: #从math中导入sqrt 2: >>> from math import sqrt as msqrt 3: #从cmath中导入sqrt 4: >>> from cmath import sqrt as csqrt 5: >>> msqrt(100) 6: 10.0 7: >>> csqrt(-1) 8: 1j 9: >>> 10:
1: #一般的同时赋值操作 2: >>> x, y, z = (1,2,3) 3: >>> x 4: 1 5: >>> y 6: 2 7: >>> z 8: 3 9: >>> 10: 11: #从字典中弹出任意一对儿键值对儿,并赋值给两个变量 12: >>> people = {'first': 'Andy', 'second':'Bill'} 13: >>> key, value = people.popitem() 14: >>> key 15: 'second' 16: >>> value 17: 'Bill' 18: >>> 19:
1: #链式赋值 2: >>> x=y=z=1 3: >>> x 4: 1 5: >>> y 6: 1 7: >>> z 8: 1 9: >>> 10:
1: #针对数字的各种操作 2: >>> x = 123 3: >>> x += 1 4: >>> x 5: 124 6: >>> x -= 4 7: >>> x 8: 120 9: >>> x *=2 10: >>> x 11: 240 12: >>> x /=3 13: >>> x 14: 80.0 15: >>> x %=9 16: >>> x 17: 8.0 18: >>> 19: 20: #字符串的增量赋值 21: >>> y = 'Test string' 22: >>> y += ', haha!' 23: >>> y 24: 'Test string, haha!' 25: >>> y *= 2 26: >>> y 27: 'Test string, haha!Test string, haha!' 28: >>> 29:
1: >>> True 2: True 3: >>> False 4: False 5: >>> [] 6: [] 7: >>> bool ([]) 8: False 9: >>> bool ([1,]) 10: True 11: >>> bool (0) 12: False 13: >>> bool (0.0) 14: False 15: >>> bool (0.1) 16: True 17: #不同的假值之间也是不相同的 18: >>> [] == {} 19: False 20: >>> [] == None 21: False 22: >>> 23:
1: #if, elif, else应用 2: num = input("Please enter a number:") 3: num = int(num) 4: if num > 0: 5: print ('You input a positive number!') 6: elif num < 0: 7: print ('You input a negative number!') 8: else: 9: print ('You input a zero!') 10:
1: key = input("Please select type, color(c) or number(n):") 2: if key == 'c': 3: color = input ("Please select a color, Red(r), Green(g), Blue(b):") 4: if color == 'r': 5: print('You selected red') 6: elif color == 'g': 7: print('You selected green') 8: elif color == 'b': 9: print('You selected blue') 10: else: 11: print("Illegal color type!") 12: else: 13: print ("You select number!") 14:
1: if '123' is '123':
1: #or的特殊用法,如果没有输入,则会返回or后面的值 2: >>> name = input("Please enter a name:") or '<unknown>' 3: Please enter a name: 4: >>> name 5: '<unknown>' 6: 7: >>> a = 'a' 8: >>> c = 'c' 9: #如果if后面的判断语句为真,返回a 10: >>> a if True else c 11: 'a' 12: #如果if后面的判断语句为假,返回c 13: >>> a if False else c 14: 'c' 15: >>> 16:
1: >>> age = 10 2: >>> assert 1<age<120, "Age must be realistic" 3: >>> age = -1 4: >>> assert 1<age<120, "Age must be realistic" 5: Traceback (most recent call last): 6: File "<pyshell#26>", line 1, in <module> 7: assert 1<age<120, "Age must be realistic" 8: AssertionError: Age must be realistic 9: >>> 10:
1: name = '' 2: while not name.strip(): 3: name = input("Please input your name:") 4: print("Hello,", name)
1: #遍历列表中的各个元素 2: >>> x = [1,2,3,4,5] 3: >>> for number in x: 4: print (number) 5: 1 6: 2 7: 3 8: 4 9: 5 10: >>> 11: 12: #使用内建函数range 13: >>> x = range(10) 14: >>> x 15: range(0, 10) 16: >>> for number in x: 17: print(number) 18: 0 19: 1 20: 2 21: 3 22: 4 23: 5 24: 6 25: 7 26: 8 27: 9 28: >>> 29:
1: x = {'a':'1', 'b':'2', 'c':'3'} 2: for key in x.keys(): 3: print (key, x[key]) 4: 5: for val in x.values(): 6: print(val) 7:
1: >>> x = list(range(0,5)) 2: >>> y = list(range(5,10)) 3: >>> z = list(range(10, 15)) 4: >>> z 5: [10, 11, 12, 13, 14] 6: >>> y 7: [5, 6, 7, 8, 9] 8: >>> x 9: [0, 1, 2, 3, 4] 10: 11: >>> zipped = zip(x, y, z) 12: >>> list(zipped) 13: [(0, 5, 10), (1, 6, 11), (2, 7, 12), (3, 8, 13), (4, 9, 14)] 14: >>> 15:
1: >>> mylist = ['12312', '12ab', '123sa', '1231s'] 2: >>> for index, string in enumerate(mylist): 3: print(index, string) 4: 5: 6: 0 12312 7: 1 12ab 8: 2 123sa 9: 3 1231s 10: >>> 11:
1: >>> data = [1,67,1,13,14,61,2] 2: >>> sorted(data) 3: [1, 1, 2, 13, 14, 61, 67] 4: >>> list(reversed(data)) 5: [2, 61, 14, 13, 1, 67, 1] 6: >>> 7:
1: >>> import math 2: >>>for x in range(99, 0, -1): 3: >>> root = math.sqrt(x) 4: >>> if root == int(root): 5: >>> print ('Max number is:', x) 6: >>> break 7: 8: Max number is 81 9:
1: #一个打印偶数的例子,不加else 语句,程序也能正确执行 2: >>> for x in range(10): 3: if x%2 == 0: 4: print(x) 5: else: 6: continue 7: 8: 9: 0 10: 2 11: 4 12: 6 13: 8 14: >>> 15:
1: while True: 2: word = input("Please enter a word:") 3: if not word: 4: break 5: print("You input:" , word) 6: 7: Please enter a word:TEst 8: You input: TEst 9: Please enter a word:ls 10: You input: ls 11: Please enter a word: 12: >>> 13:
1: #设置一个奇数序列,判断里面是不是有偶数(一个蛋疼的程序,哈哈) 2: x = list(range(1,100,2)) 3: for val in x: 4: if val%2 == 0: 5: print (x) 6: break; 7: else: 8: print("Did not break!") 9: #执行结果 10: Did not break! 11:
1: #得到10以内数字的平方的列表 2: import math 3: mylist = [math.pow(x, 2) for x in list(range(0,10))] 4: print (mylist) 5: 6: #得到10以内偶数的平方的列表 7: mylist = [math.pow(x, 2) for x in list(range(0,10)) if x % 2 == 0] 8: print (mylist) 9:
1: >>> a = 10 2: #if的语句块中并没有其他语句需要执行,先用pass占位,执行的时候,如果if判断为真直接跳过。 3: >>> if a>0: 4: pass 5: >>>
1: >>> x = {'a':'1', 'b':'2', 'c':'3'} 2: >>> y = x 3: >>> y 4: {'a': '1', 'c': '3', 'b': '2'} 5: #删除变量x,再调用会报“未定义”的错误 6: >>> del x 7: >>> x 8: Traceback (most recent call last): 9: File "<pyshell#15>", line 1, in <module> 10: x 11: NameError: name 'x' is not defined 12: >>> y 13: {'a': '1', 'c': '3', 'b': '2'} 14: #删除字典中的项 15: >>> del y['a'] 16: >>> y 17: {'c': '3', 'b': '2'} 18: >>> 19:
1: #exec直接执行语句 2: >>> exec('print("Hello, world!")') 3: Hello, world! 4: #exec执行后不返回执行结果 5: >>> exec("2*2") 6: >>> 7: #exec在命名空间中执行语句 8: >>> exec(""" 9: x=2 10: y=3 11: z=4 12: """, scope) 13: >>> scope.keys() 14: dict_keys(['__builtins__', 'x', 'z', 'y']) 15: >>> scope['x'] 16: 2 17: >>> 18: 19: #eval直接执行语句 20: >>> eval('print("Hello, world!")') 21: Hello, world! 22: #eval在执行后将执行结果返回 23: >>> eval('2*2') 24: 4 25: >>> 26: #eval操作字典中的数据 27: >>> scope.keys() 28: dict_keys(['__builtins__', 'x', 'z', 'y']) 29: >>> eval('x+y+z', scope) 30: 9 31: >>> 32:
Date: 2011-11-23 20:40:47
Org version 7.7 with Emacs version 23
Validate XHTML 1.0