이 기사는 Python의 변수 및 연산자에 대한 코드 예제를 제공합니다. 특정 참조 값이 있으므로 도움이 될 수 있습니다.
변수란 무엇입니까
두 개의 목록이 수학 연산을 한다고 가정합니다
>>> [1,2,3,4,5,6] [1,2,3] Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> [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 "<pyshell#1>", line 1, in <module> type(1) TypeError: 'int' object is not callable >>> 1(1) Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> 1(1) TypeError: 'int' object is not callable
파이썬 동적 언어의 특징, 선언 시 변수 유형을 지정할 필요 없음
>>> a = '1' >>> a = 1 >>> a = (1,2,3) >>> a = {1,2,3}
값 유형 및 참조 유형
int, str, tuple은 값 유형(불변), list, set, dict는 참조 유형(변수)입니다.
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 "<pyshell#8>", line 1, in <module> '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 "<pyshell#21>", line 1, in <module> 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 "<pyshell#26>", line 1, in <module> 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 "<pyshell#7>", line 1, in <module> a[2] = '3' TypeError: 'tuple' object does not support item assignment >>> a[3][2] = '4' >>> print(a) //元组内的列表可变 (1, 2, 3, [1, 2, '4'])
Operators
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 "<pyshell#3>", line 1, in <module> 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 "<pyshell#12>", line 1, in <module> 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</p> <p>0은 False로 간주되며, 0이 아닌 경우 True</p> <pre class="brush:php;toolbar:false">>>> 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
빈 문자열 False
>>> not 0.1 False >>> not '' True >>> not '0' False
를 의미합니다. 빈 목록 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
객체의 세 가지 특성: id, value, type, "is"로 ID 판단, 사용 값을 판단하려면 "==", 유형을 판단하려면 "isinstance"
>>> 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. 비트 연산자: (==숫자를 이진수로 연산==)
&bitwise AND
|비트 OR
^비트별 XOR
~비트별 부정
<<왼쪽으로 이동
>>오른쪽으로 이동
비트별 AND 연산, 각 이진수를 비교하여 둘 다 1이면 1을 얻고, 하나가 0이면 0
>>> a = 2 >>> b = 3 >>> a & b 2
Variable | 10진수로 변환 |
|
||
---|---|---|---|---|
a | 을 얻습니다.1 | 0 | 2 | |
b | 1 | 1 | 3 |
|
비트 AND | 1 | 0 | 2 십진수로 변환 b1 |
1 |
1 |
1 |
3 |
---|
위 내용은 Python의 변수 및 연산자의 코드 예의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!