a、字符串的定義方法
使用單引號(')
你可以用單引號指示字符串,就如同'Quote me on this'這樣。所有的空白,即空格和制表符都照原樣保留。
使用雙引號(")
在雙引號中的字符串與單引號中的字符串的使用完全相同,例如"What's your name?"。
使用三引號('''或""")
利用三引號,你可以指示一個多行的字符串。你可以在三引號中自由的使用單引號和雙引號。例如:
'''This is a multi-line string. This is the first line.This is the second line."What's your name?," I asked.He said "Bond, James Bond."'''
轉義符
用/'來指示單引號——注意這個反斜杠?,F在你可以把字符串表示為'What/'s your name?'。
表示這個特別的字符串的方法是"What's your name?",即用雙引號或三引號。
在一個字符串中,行末的單獨一個反斜杠表示字符串在下一行繼續,而不是開始一個新的行。例如:
"This is the first sentence./This is the second sentence."
b、變量
定義變量的方法與其它語言類似,變量的類型是通過賦值來確定的
Int型 Number = 0字符串型 String = ‘'Dict型 dict = {}c、縮進
同一層次的語句必須有相同的縮進。每一組這樣的語句稱為一個塊
i = 5 print 'Value is', iprint 'I repeat, the value is', i
d、運算符
| 運算符 | 名稱 | 說明 | 例子 |
| + | 加 | 兩個對象相加 | 3 + 5得到8。'a' + 'b'得到'ab'。 |
| - | 減 | 得到負數或是一個數減去另一個數 | -5.2得到一個負數。50 - 24得到26。 |
| * | 乘 | 兩個數相乘或是返回一個被重復若干次的字符串 | 2 * 3得到6。'la' * 3得到'lalala'。 |
| / | 除 | x除以y | 4/3得到1(整數的除法得到整數結果)。4.0/3或4/3.0得到1.3333333333333333 |
| // | 取整除 | 返回商的整數部分 | 4 // 3.0得到1.0 |
| % | 取模 | 返回除法的余數 | 8%3得到2。-25.5%2.25得到1.5 |
| < | 小于 | 返回x是否小于y。所有比較運算符返回1表示真,返回0表示假。這分別與特殊的變量True和False等價。注意,這些變量名的大寫。 | 5 < 3返回0(即False)而3 < 5返回1(即True)。比較可以被任意連接:3 < 5 < 7返回True。 |
| > | 大于 | 返回x是否大于y | 5 > 3返回True。如果兩個操作數都是數字,它們首先被轉換為一個共同的類型。否則,它總是返回False。 |
| <= | 小于等于 | 返回x是否小于等于y | x = 3; y = 6; x <= y返回True。 |
| >= | 大于等于 | 返回x是否大于等于y | x = 4; y = 3; x >= y返回True。 |
| == | 等于 | 比較對象是否相等 | x = 2; y = 2; x == y返回True。x = 'str'; y = 'stR'; x == y返回False。x = 'str'; y = 'str'; x == y返回True。 |
| != | 不等于 | 比較兩個對象是否不相等 | x = 2; y = 3; x != y返回True。 |
| not | 布爾“非” | 如果x為True,返回False。如果x為False,它返回True。 | x = True; not y返回False。 |
| and | 布爾“與” | 如果x為False,x and y返回False,否則它返回y的計算值。 | x = False; y = True; x and y,由于x是False,返回False。在這里,Python不會計算y,因為它知道這個表達式的值肯定是False(因為x是False)。這個現象稱為短路計算。 |
| or | 布爾“或” | 如果x是True,它返回True,否則它返回y的計算值。 | x = True; y = False; x or y返回True。短路計算在這里也適用。 |
最常用的是?。ǖ龋┯?、大(等)于、(不)等于、not、and、or
e、控制流
number = 23guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' # New block starts here print "(but you do not win any prizes!)" # New block ends hereelif guess < number: print 'No, it is a little higher than that' # Another block # You can do whatever you want in a block ...else: print 'No, it is a little lower than that' # you must have guess > number to reach here print 'Done'# This last statement is always executed, after the if statement is executed if 'a' in name: print 'Yes, it contains the string "a"'elif和else部分是可選的
number = 23running = Truewhile running: guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' running = False # this causes the while loop to stop elif guess < number: print 'No, it is a little higher than that' else: print 'No, it is a little lower than that'else: print 'The while loop is over.' # Do anything else you want to do hereprint 'Done'for i in range(1, 5):print i等價于for i in range(5):print i
for循環在這個范圍內遞歸——for i in range(1,5)等價于for i in [1, 2, 3, 4],這就如同把序列中的每個數(或對象)賦值給i,一次一個,然后以每個i的值執行這個程序塊
while True: s = raw_input('Enter something : ') if s == 'quit': break print 'Length of the string is', len(s)print 'Done'while True: s = raw_input('Enter something : ') if s == 'quit': break if len(s) < 3: continueprint 'Input is of sufficient length'新聞熱點
疑難解答