摘要:什么是變量假設兩個做數學運算先把乘以,然后加上,最后再加上列表變量名的命名規則變量名命名只能使用字母數字下劃線變量名的首字母不能是數字系統關鍵字,不能用在變量名中保留關鍵字不是系統保留關鍵字,但是不建議作為變量名,否則極易出錯動態語言的特性
什么是變量
>>> [1,2,3,4,5,6] [1,2,3] Traceback (most recent call last): File "變量名的命名規則", line 1, in [1,2,3,4,5,6] [1,2,3] TypeError: list indices must be integers or slices, not tuple //A B,先把A乘以3,然后加上B,最后再加上列表A >>> [1,2,3,4,5,6]*3+[1,2,3]+[1,2,3,4,5,6] [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 2, 3, 4, 5, 6] >>> A = [1,2,3,4,5,6] >>> print(A) [1, 2, 3, 4, 5, 6] >>> B = [1,2,3] >>> A*3 + B + A [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 2, 3, 4, 5, 6]
>>> 1a = 2 //變量名的首字母不能是數字 SyntaxError: invalid syntax >>> A2 = "1" >>> _2 = "1" >>> A*B="1" SyntaxError: can"t assign to operator
>>> and = 1 SyntaxError: invalid syntax >>> if = 2 SyntaxError: invalid syntax >>> import = 3 SyntaxError: invalid syntax >>> type = 3 //type不是系統保留關鍵字,但是不建議作為變量名,否則極易出錯 >>> print(type) 3 >>> type = 1 >>> type(1) Traceback (most recent call last): File "", line 1, in type(1) TypeError: "int" object is not callable >>> 1(1) Traceback (most recent call last): File " ", line 1, in 1(1) TypeError: "int" object is not callable
>>> a = "1" >>> a = 1 >>> a = (1,2,3) >>> a = {1,2,3}值類型與引用類型
1.int
>>> a = 1 >>> b = a >>> a = 3 >>> print(b) 1
2.list
>>> a = [1,2,3,4,5] >>> b = a >>> a[0] = "1" >>> print(a) ["1", 2, 3, 4, 5] >>> print(b) ["1", 2, 3, 4, 5] >>> a = [1,2,3] >>> id(a) 4405825224 >>> hex(id(a)) "0x1069b8ec8" >>> a[0]="1" >>> id(a) 4405825224 >>>
3.str
>>> a = "hello" >>> a = a + "python" //a加上一個新的字符串,不再是原來的字符串了 >>> print(a) hellopython >>> b = "hello" >>> id(b) 4405534032 >>> b = b + "python" //加上新的字符串后,id改變 >>> id(b) 4355329456 >>> "python"[0] "p" >>> "python"[0]="o" Traceback (most recent call last): File "", line 1, in "python"[0]="o" TypeError: "str" object does not support item assignment
4.tuple
>>> a = (1,2,3) >>> a[0] = "1" Traceback (most recent call last): File "運算符", line 1, in a[0] = "1" TypeError: "tuple" object does not support item assignment >>> b = [1,2,3] >>> b.append(4) >>> print(b) [1, 2, 3, 4] >>> c = (1,2,3) >>> c.append(4) Traceback (most recent call last): File " ", line 1, in c.append(4) AttributeError: "tuple" object has no attribute "append" >>> a = (1,2,3,[1,2,4]) >>> a[2] 3 >>> a[3] [1, 2, 4] >>> a[3][2] 4 >>> a = (1,2,3,[1,2,["a","b","c"]]) >>> a[3][2][1] "b" >>> a = (1,2,3,[1,2,4]) >>> a[2] = "3" Traceback (most recent call last): File " ", line 1, in a[2] = "3" TypeError: "tuple" object does not support item assignment >>> a[3][2] = "4" >>> print(a) //元組內的列表可變 (1, 2, 3, [1, 2, "4"])
1.算數運算符:+,-,* ,/,//,%,**
>>> "hello"+"world" "helloworld" >>> [1,2,3]*3 [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> 3-1 2 >>> 3/2 1.5 >>> 3//2 //整除 1 >>> 5%2 //求余 1 >>> 2**2 //求N次方 4 >>> 2**5 32
2.賦值運算符:=,+=,-=,*=,/=,%=,**=,//=
>>> c = 1 >>> c = c+1 >>> print(c) 2 >>> c+=1 >>> print(c) 3 >>> c-=1 >>> print(c) 2 >>> c++ //python中沒有自增和自減運算符 SyntaxError: invalid syntax >>> c-- SyntaxError: invalid syntax >>> b=2 >>> a=3 >>> b+=a >>> print(b) 5 >>> b-=a >>> print(b) 2 >>> b*=a >>> print(b) 6
3.比較(關系)運算符:==,!=,>,<,>=,<=
>>> 1==1 True >>> 1>1 False >>> 1>=1 True >>> a>=b Traceback (most recent call last): File "", line 1, in a>=b NameError: name "a" is not defined >>> a=1 >>> b=2 >>> a!=b True >>> b=1 >>> b+=b>=1 //b=b+True >>> print(b) 2 >>> print(b>=1) True >>> 1>1 False >>> 2>3 False >>> "a">"b" False >>> ord("a") 97 >>> ord("b") 98 >>> "abc"<"abd" //實際上是a和a比,b和b比,c和d比 True >>> ord("abc") Traceback (most recent call last): File " ", line 1, in ord("abc") TypeError: ord() expected a character, but string of length 3 found >>> ord("c") 99 >>> ord("d") 100 >>> [1,2,3]<[2,3,4] True >>> (1,2,3)<(1,3,2) True
4.邏輯運算符:and,or,not
>>> True and True True >>> True and False False >>> True or False True >>> False or False False >>> not False True >>> not True False >>> not not True True
>>> 1 and 1 1 >>> "a" and "b" "b" >>> "a" or "b" "a" >>> not "a" False >>> a = True >>> b = False >>> a or b True >>> b and a False
>>> not 0.1 False >>> not "" True >>> not "0" False
>>> not [] True >>> not [1,2] False >>> [1] or [] [1] >>> [] or [1] [1] >>> "a" and "b" "b" >>> "" and "b" "" >>> 1 and 0 0 >>> 0 and 1 0 >>> 1 and 2 2 >>> 2 and 1 1 >>> 0 or 1 1 >>> 1 or 0 1 >>> 1 or 2 1
5.成員運算符:in,not in
>>> a = 1 >>> a in [1,2,3,4,5] True >>> b = 6 >>> b in [1,2,3,4,5] False >>> b not in [1,2,3,4,5] True >>> b = "h" >>> b in "hello" True >>> b not in (1,2,3,4,5) True >>> b not in {1,2,3,4,5} True >>> b = "a" >>> b in {"c":1} False >>> b = 1 >>> b in {"c":1} False >>> b = "c" >>> b in {"c":1} //字典里面根據key返回 True
6.身份運算符:is,is not
>>> a = 1 >>> b = 1 >>> a is b True >>> a="hello" >>> b="world" >>> a is b False >>> c="hello" >>> a is c True >>> a=1 >>> b=2 >>> a==b False >>> a=1 >>> b=1 >>> a is b True >>> a==b True >>> a=1 >>> b=1.0 >>> a==b True >>> a is b //is不是比較值相等,比較的是兩個變量的身份(內存地址)是否相等 False >>> id(a) 4374928384 >>> id(b) 4376239272 >>> a={1,2,3} >>> b={2,1,3} >>> a==b //集合是無序的 True >>> a is b False >>> id(a) 4433997384 >>> id(b) 4433996488 >>> c=(1,2,3) >>> d=(2,1,3) >>> c==d //元組是序列,是有序的 False >>> c is d False >>> a=1 >>> b=2 >>> a==b False >>> a is b False >>> a = "hello" >>> type(a) == int False >>> type(a) == str True >>> isinstance(a,str) //isinstance是判斷變量類型的函數 True >>> isinstance(a,int) False >>> isinstance(a,(int,str,float)) True >>> isinstance(a,(int,float)) False
7.位運算符:(==把數字當作二進制數進行運算==)
&按位與
|按位或
^按位異或
~按位取反
<<左移動
>>右移動
>>> a = 2 >>> b = 3 >>> a & b 2
變量 | 轉換為十進制 | |||
---|---|---|---|---|
a | 1 | 0 | 2 | |
b | 1 | 1 | 3 | |
按位與 | 1 | 0 | 2 |
>>> a = 2 >>> b = 3 >>> a | b 3
變量 | 轉換為十進制 | |||
---|---|---|---|---|
a | 1 | 0 | 2 | |
b | 1 | 1 | 3 | |
按位或 | 1 | 1 | 3 |
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/44884.html
摘要:解釋器的系統上,一般默認的版本為,我們可以將安裝在目錄中。中的按位運算法則如下下表中變量為,為,二進制格式如下邏輯運算符圖片邏輯運算符測試實例中包含了一系列的成員,包括字符串,列表或元組。 3.Python3解釋器 Linux/Unix的系統上,一般默認的 python 版本為 2.x,我們可以將 python3.x 安裝在 /usr/local/python3 目錄中。 安裝完成后,...
摘要:的基本數據類型中的變量不需要聲明。在里,只有一種整數類型,表示為長整型,沒有中的。字符串的截取的語法格式如下變量頭下標尾下標索引值以為開始值,為從末尾的開始位置。列表列表是中使用最頻繁的數據類型。注意構造包含或個元素的元組的特殊語法規則。 1、python3的基本數據類型 Python 中的變量不需要聲明。每個變量在使用前都必須賦值,變量賦值以后該變量才會被創建。在 Python 中,...
摘要:一的基本語法縮進統一個或者個空格。中的數據類型中有個標準類型數字字符串列表元組集合字典數字復數在中,只有一種整數類型,表示長整型。如則會顯示,并不是換行。空行與代碼縮進不同,空行并不是語法的一部分。我們將首行及后面的代碼組稱為一個子句。 一、python3的基本語法 1、縮進統一(1個tab或者4個空格)。 for i in range(10): print (i) ...
摘要:表達式表達式是運算符和操作數所構成的序列運算符優先級同級的運算符的優先級還是有區別的比如邏輯運算符里的的優先級大于兩個括號同級,左結合出現賦值符號時,右結合優先級在文本文件中編寫代碼腳本是后綴名為的文件,通過命令行執行推薦的,大型工程適合用 表達式 表達式(Expression)是運算符(operator)和操作數(operand)所構成的序列 >>> 1 + 1 2 >>> a ...
摘要:常量的值近似為。在后傳入一個整數可以保證該域至少有這么多的寬度表示浮點數保留位小數常量的值近似為。 1. 數字 類型 int, float, bool, complex type() 查看變量類型 isinstance(a, int) 查看變量類型 showImg(https://segmentfault.com/img/remote/1460000016789047); 運算符 ...
閱讀 842·2019-08-30 15:55
閱讀 1417·2019-08-30 13:55
閱讀 1993·2019-08-29 17:13
閱讀 2847·2019-08-29 15:42
閱讀 1337·2019-08-26 14:04
閱讀 1025·2019-08-26 13:31
閱讀 3277·2019-08-26 11:34
閱讀 837·2019-08-23 18:25