1 在3版本中print需要加上括號
2 多行語句:用\連接
1 item_one=1 2 item_two=2 3 item_three=3 4 total = item_one + \ 5 item_two + \ 6 item_three 7 print (total)
?
3 引號
? 字符串通常在引號中 不管是單引號 雙引號還是三引號
?必須保證前后一致
1 #引號 2 word = 'word' 3 sentence = "這是一個句子。" 4 paragraph = """這是一個段落。 5 包含了多個語句""" 6 print (word) 7 print (sentence) 8 print (paragraph)
?
4注釋:
?(1)#開頭 也可以在結尾用#進行注釋
?(2)多行注釋 用三引號括起來
1 #注釋 2 # 第一個注釋 3 print ("Hello, Python!"); # 第二個注釋 4 name = "Madisetti" # 這是一個注釋 5 6 ''' 7 這是多行注釋,使用單引號。 8 這是多行注釋,使用單引號。 9 這是多行注釋,使用單引號。 10 ''' 11 12 """ 13 這是多行注釋,使用雙引號。 14 這是多行注釋,使用雙引號。 15 這是多行注釋,使用雙引號。 16 """
5碼組:
1 #碼組 2 ''' 3 if expression : 4 suite 5 elif expression : 6 suite 7 else : 8 suite 9 '''
6幫助
1 help(sys.stdout.write)
?
7變量賦值
1 #變量賦值 2 counter = 100 # 賦值整型變量 3 miles = 1000.0 # 浮點型 4 name = "John" # 字符串 5 6 print (counter) 7 print (miles) 8 print (name) 9 10 a = b = c = 1 11 print (a,b,c) 12 a, b, c = 1, 2, "john" 13 print (a,b,c) 14 15 #數字 16 var1 = 1 17 var2 = 10 18 19 #del var1[,var2[,var3[....,varN]]]] 20 var=5896419821 21 var_a=0.22 22 var_b=3e2 23 del var 24 del var_a, var_b
8數據類型
?Numbers
??(1)不可以改變的數據類型
??(2)用于存儲數值
??(3)int long float complex
?string
??(1)str*2代表輸出2次
??(2)可以用+進行字符串的連接
1 #字符串 2 #s="a1a2???an"(n>=0) 3 s = 'ilovepython' 4 print (s[1:5])#從0開始 5 print (s[5:-1])#去不到末尾的n 6 7 str = 'Hello World!' 8 print (str) # 輸出完整字符串 9 print (str[0]) # 輸出字符串中的第一個字符 10 print (str[2:5]) # 輸出字符串中第三個至第五個之間的字符串 11 print (str[2:]) # 輸出從第三個字符開始的字符串 12 print (str * 2) # 輸出字符串兩次 13 print (str + "TEST") # 輸出連接的字符串
?List
??(1)用中括號[]創建 用,分開
??(2)*和+的用法和string類似
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tinylist = [123, 'john']print (list) # 輸出完整列表 print (list[0]) # 輸出列表的第一個元素 print (list[1:3]) # 輸出第二個至第三個的元素 print (list[2:]) # 輸出從第三個開始至列表末尾的所有元素 print (tinylist * 2) # 輸出列表兩次 print (list + tinylist) # 打印組合的列表
?Tuple
??(1)用小括號()
??(2)元素是固定的 相當于可讀
??(3)用法和上面的string list類似
??(4)元祖中不能直接賦值給某個元素 tuple[2]=1000//wrong
1 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) 2 tinytuple = (123, 'john') 3 4 print (tuple) # 輸出完整元組 5 print (tuple[0]) # 輸出元組的第一個元素 6 print (tuple[1:3]) # 輸出第二個至第三個的元素 7 print (tuple[2:]) # 輸出從第三個開始至列表末尾的所有元素 8 print (tinytuple * 2) # 輸出元組兩次 9 print (tuple + tinytuple) # 打印組合的元組 10 11 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) 12 list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] 13 tuple[2] = 1000 # 元組中是非法應用 14 list[2] = 1000 # 列表中是合法應用
?Dictionary
??(1)用{},無序集合
??(2)采用鍵值的方式 例如'name':'join'
??(3)可以輸出所有的鍵和值?
??
1 #元字典 2 dict = {} 3 dict['one'] = "This is one" 4 dict[2] = "This is two" 5 6 tinydict = {'name': 'john','code':6734, 'dept': 'sales'} 7 8 9 print (dict['one']) # 輸出鍵為'one' 的值 10 print (dict[2]) # 輸出鍵為 2 的值 11 print (tinydict) # 輸出完整的字典 12 print (tinydict.keys()) # 輸出所有鍵 13 print (tinydict.values()) # 輸出所有值
9運算符
?9.1算數運算符
?(1)冪 **
?(2)取整除 //
1 a = 21 2 b = 10 3 c = 0 4 5 c = a + b 6 print ("Line 1 - Value of c is ", c) 7 8 c = a - b 9 print ("Line 2 - Value of c is ", c) 10 11 c = a * b 12 print ("Line 3 - Value of c is ", c) 13 14 c = a / b 15 print ("Line 4 - Value of c is ", c ) 16 17 c = a % b 18 print ("Line 5 - Value of c is ", c) 19 20 a = 2 21 b = 3 22 c = a**b 23 print ("Line 6 - Value of c is ", c) 24 25 a = 10 26 b = 5 27 c = a//b 28 print ("Line 7 - Value of c is ", c)
?9.2比較運算符
??(1)== 比較對象是否相等
?9.3位運算符
??(1)&:位與:兩個相應位都為1 結果為1
??(2)|:位或:其中一個為1則為1
??(2)^:位異或:相異的時候為1
??(4)~:取反:0-》1 1——》0
??(5)<<:左移:
??(6)>>:右移
1 #位運算符 2 a = 60 # 60 = 0011 1100 3 b = 13 # 13 = 0000 1101 4 c = 0 5 6 c = a & b; # 12 = 0000 1100 7 print "Line 1 - Value of c is ", c 8 9 c = a | b; # 61 = 0011 1101 10 print "Line 2 - Value of c is ", c 11 12 c = a ^ b; # 49 = 0011 0001 13 print "Line 3 - Value of c is ", c 14 15 c = ~a; # -61 = 1100 0011 16 print "Line 4 - Value of c is ", c 17 18 c = a << 2; # 240 = 1111 0000 19 print "Line 5 - Value of c is ", c 20 21 c = a >> 2; # 15 = 0000 1111 22 print "Line 6 - Value of c is ", c
?9.4成員運算符
??(1)in:在指定序列中找到值返回true
??(2)not in
1 a = 10 2 b = 20 3 list = [1, 2, 3, 4, 5 ]; 4 5 if ( a in list ): 6 print ("Line 1 - a is available in the given list") 7 else: 8 print ("Line 1 - a is not available in the given list") 9 10 if ( b not in list ): 11 print ("Line 2 - b is not available in the given list") 12 else: 13 print ("Line 2 - b is available in the given list") 14 15 a = 2 16 if ( a in list ): 17 print ("Line 3 - a is available in the given list") 18 else: 19 print ("Line 3 - a is not available in the given lis)t"
?9.5身份運算符
??(1)is:判斷是不是引用的同一個對象
??(2)is not:判斷兩個標識符是不是引用不同對象
1 #身份運算符 2 a = 20 3 b = 20 4 5 if ( a is b ): 6 print ("Line 1 - a and b have same identity") 7 else: 8 print ("Line 1 - a and b do not have same identity") 9 10 if ( id(a) == id(b) ): 11 print ("Line 2 - a and b have same identity") 12 else: 13 print ("Line 2 - a and b do not have same identity") 14 15 b = 30 16 if ( a is b ): 17 print ("Line 3 - a and b have same identity") 18 else: 19 print ("Line 3 - a and b do not have same identity") 20 21 if ( a is not b ): 22 print ("Line 4 - a and b do not have same identity") 23 else: 24 print ("Line 4 - a and b have same identity")
?