Python 3.4.4 学习笔记(004)python manuals/the python tutorial -- 3. An Informal Introduction to Python...
可以看到python版本號變了,找到一本學習python的書,Mark Lutz所著的Python學習手冊,建議我直接學習最新版本的Python,到網站上看了一下,是3.5.1,但是不支持XP,能支持XP的是3.4.4,都是2015年12月發布的,果斷更新。等漫游指南看完了,就轉向那本學習手冊。
學習運算符
/ ? ?除法,17/3 = 5.66...
// ? ? ? ? ? 17 // 3 = 5 ?整數除法
% ? ? ? ? ? 17 % 3 = 2 ?取余數
** ? ? ? ? ? 5 ** 2 = 25 乘方 這個和fortran 一樣
交互模式下,最后一個表達式的值儲存在變量"_"(下劃線)里面
如:
>>> a=5 >>> b=4 >>> a*b 20 >>> _ 20>>> b+_
24
>>> _
24
>>>
數值方面,除了int float還支持 Decimal 、Fraction,(小數分數) 復數
支持字符串,Strings, 單雙引號都行
\' \" 分別表示 ’ “ ?轉義字符的作用 ?\n 交互模式下原樣輸出,print函數作用下成為換行符
>>> print('C:\some\name') # here \n means newline! C:\some ame >>> print(r'C:\some\name') # note the r before the quote C:\some\name三個雙引號可以原樣輸出多行文本:
print("""\ Usage: thingy [OPTIONS]-h Display this usage message-H hostname Hostname to connect to """)作用于字符串,n*"..." 或者 "。。。"*n表示將字符串重復n次,+則連接字符串。并排放置的字串自動連在一起,先連后乘(所乘數字置于最后,否則不合語法),先乘后加
>>> a= "abc" "def" "kk"*3 >>> a 'abcdefkkabcdefkkabcdefkk' >>> a="abc"+"def"+"kk"*3 >>> a 'abcdefkkkkkk' >>> >>> word = 'Python' >>> word[0] # character in position 0 'P' >>> word[5] # character in position 5 'n' >>> word[-1] # last character 'n' >>> word[-2] # second-last character 'o' >>> word[-6] 'P' >>> word[0:2] # characters from position 0 (included) to 2 (excluded) 'Py' >>> word[2:5] # characters from position 2 (included) to 5 (excluded) 'tho' >>> word[:2] + word[2:] 'Python' >>> word[:4] + word[4:] 'Python' >>> word[:2] # character from the beginning to position 2 (excluded) 'Py' >>> word[4:] # characters from position 4 (included) to the end 'on' >>> word[-2:] # characters from the second-last (included) to the end 'on'+---+---+---+---+---+---+| P | y | t | h | o | n |+---+---+---+---+---+---+0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1看到以上這些,我只想說,太強大了
然后是列表,列表好像是數組,可是太靈活了
>>> squares = [1, 4, 9, 16, 25] >>> squares [1, 4, 9, 16, 25] >>> squares[0] # indexing returns the item 1 >>> squares[-1] 25 >>> squares[-3:] # slicing returns a new list [9, 16, 25] >>> squares[:] [1, 4, 9, 16, 25] >>> squares + [36, 49, 64, 81, 100] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] >>> cubes = [1, 8, 27, 65, 125] # something's wrong here >>> 4 ** 3 # the cube of 4 is 64, not 65! 64 >>> cubes[3] = 64 # replace the wrong value >>> cubes [1, 8, 27, 64, 125] >>> cubes.append(216) # add the cube of 6 >>> cubes.append(7 ** 3) # and the cube of 7 >>> cubes [1, 8, 27, 64, 125, 216, 343] >>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> letters ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> # replace some values >>> letters[2:5] = ['C', 'D', 'E'] >>> letters ['a', 'b', 'C', 'D', 'E', 'f', 'g'] >>> # now remove them >>> letters[2:5] = [] >>> letters ['a', 'b', 'f', 'g'] >>> # clear the list by replacing all the elements with an empty list >>> letters[:] = [] >>> letters [] >>> letters = ['a', 'b', 'c', 'd'] >>> len(letters) 4 >>> a = ['a', 'b', 'c'] >>> n = [1, 2, 3] >>> x = [a, n] >>> x [['a', 'b', 'c'], [1, 2, 3]] >>> x[0] ['a', 'b', 'c'] >>> x[0][1] 'b' >>> a="abcde" >>> a=[1,2,3,a,"b"] >>> a [1, 2, 3, 'abcde', 'b'] >>>列表可以取元素,可以使用”+“,可以使用函數len(),可以追加元素,可以多維列表,可以不同變量類型放在一組
甚至這樣也可以:
>>> a, b = 0, 1 >>> while b < 1000: ... print(b, end=',') ... a, b = b, a+b ... 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,感覺python就是一門外語有木有?
轉載于:https://www.cnblogs.com/argent/p/5240698.html
總結
以上是生活随笔為你收集整理的Python 3.4.4 学习笔记(004)python manuals/the python tutorial -- 3. An Informal Introduction to Python...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#开发之问题汇总-vs运行正常,服务器
- 下一篇: 《从零开始学Swift》学习笔记(Day