程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Getting started with Python - operators

編輯:Python

Numeric operators (+, -, *, /, %, **, //)

Python There are the following operators .

+a # Integers
-a # negative
a + b # Add
a - b # Subtraction
a * b # Multiplication
a / b # division
a % b # remainder
a ** b # Exponentiation
a // b # Divide and round

 

An operator (~, &, |, ^, <<, >>)

The following operators are defined

~a # Bit reversal
a & b # AND: And (a And b All are 1 When is 1)
a | b # OR: Logic or (a or b by 1 The result is 1)
a ^ b # XOR: Logical XOR
a << b # Move left
a >> b # Move right

 

Substitution operator (=, +=, -=, *=, /=, %=, **=, //=, &=, |=, ^=, <<=, >>=)

There are the following substitution operators . I won't support it ++  and  -- , Replace it with  a += 1  and  a -= 1  Express .

a = b # towards a Plug in b
a += b # a = a + b
a -= b # a = a - b
a *= b # a = a * b
a /= b # a = a / b
a %= b # a = a % b
a **= b # a = a ** b
a //= b # a = a // b
a &= b # a = a & b
a |= b # a = a | b
a ^= b # a = a ^ b
a <<= b # a = a << b
a >>= b # a = a >> b

 

Comparison operator (==, !=, <, >, <=, >=, <=>, ===)

There are the following comparison operators

a == b # a and b equal
a != b # a and b Unequal
a < b # a Than b Small
a > b # a Than b Big
a <= b # a Less than or equal to b
a >= b # a Greater than or equal to b
a <> b # a It's not equal to b
a is b # a And b equal
a is not b # a And b Unequal
a in b # a Included in b
a not in b # a Not included in b

 

Boolean operation (and, or, not)

There are the following operators .

a and b # a And b
a or b # a or b
not a # Not a

 

Conditional operator (if else)

The following formula ,c  When true, the result is  x , Otherwise, the result is y..

x if c else y

 

String operators (+, *, [n:m])

The string contains the following operators .n  and  m  Specify a negative number , Count from the end of the text .

a + b # String splicing
a * n # a Duplicate string n Time
a[n] # Take out No n Characters
a[n:m] # From n To the first m Substring of
a[n:] # The first n Substrings from beginning to end
a[:m] # From the beginning to the m Substrings of
a[n:m:s] # From n From the beginning to m until , jumping s individual 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved