Operators and Conditional Loop Statements in Python

operator

1. Arithmetic operators

The following assumes that variable a is 9 and variable b is 7:

operator describe example
+ add - add two objects a + b outputs the result 16
- Subtract - get a negative number or subtract one number from another a - b output result -2
* multiply - multiply two numbers or return a string repeated several times a * b output result 63
/ divide - x divided by y a / b output result 1.2857142857142858
% modulo - returns the remainder of the division a % b output result 2
** power - returns x raised to the y power 2**3, the output result is 8
// Divide - Returns the integer part of the quotient 9//2 outputs result 4 , 9.0//2.0 outputs result 4.0

2. Comparison operators

The following assumes that variable a is 10 and variable b is 20:

operator describe example
== equals - compares objects for equality (a == b) returns False.
!= not equal - compares whether two objects are not equal (a != b) returns true.
<> not equal - compares whether two objects are not equal (a <> b) returns true. This operator is similar to != .
> Greater than - Returns whether x is greater than y (a > b) returns False.
< less than - Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. This is equivalent to the special variables True and False, respectively. Note the capitalization of these variable names. (a < b) returns true.
>= Greater than or equal to - Returns whether x is greater than or equal to y. (a >= b) returns False.
<= Less than or equal to - Returns whether x is less than or equal to y. (a <= b) returns true.

3. Assignment operator

The following assumes that variable a is 10 and variable b is 20:

operator describe example
= simple assignment operator c = a + b assigns the result of the operation of a + b to c
+= addition assignment operator c += a is equivalent to c = c + a
-= Subtraction assignment operator c -= a is equivalent to c = c - a
*= multiplication assignment operator c = a is equivalent to c = c a
/= division assignment operator c /= a is equivalent to c = c / a
%= Modulo assignment operator c %= a is equivalent to c = c % a
**= exponentiation assignment operator c = a is equivalent to c = c a
//= Integer division assignment operator c //= a is equivalent to c = c // a

The above three types of operators are basically the same as those in other languages, and there is no place to hit the blackboard. The following Pythonare the more special operators in zombies.

4. Bitwise operators

Bitwise operators treat numbers as binary. PythonThe bitwise operations in are as follows:

operator describe example
& bitwise AND operator (a & b) output result 12, binary interpretation: 0000 1100
^ bitwise exclusive or operator (a ^ b) output result 49, binary interpretation: 0011 0001
~ bitwise negation operator (~a) outputs the result -61, binary interpretation: 1100 0011, in one's complement form of a signed binary number.
<< left shift operator a << 2 output result 240, binary interpretation: 1111 0000
>> right shift operator a >> 2 output result 15, binary interpretation: 0000 1111

The operation method of this operator is: first convert the number into binary, then perform the above operator operation on each digit of the binary, and finally convert the result into decimal output, the test is as follows

# 5.位运算符
a = 60            # 60 = 0011 1100
b = 13            # 13 = 0000 1101
c = 0

# 运算和输出结果
c = a & b        # 12 = 0000 1100
c = a | b        # 61 = 0011 1101
c = a ^ b        # 49 = 0011 0001
c = ~a           # -61 = 1100 0011
c = a << 2       # 240 = 1111 0000
c = a >> 2       # 15 = 0000 1111

5. Logical Operators

PythonThe language supports logical operators, but it is different Swiftfrom the logical operators ( ) in other languages ​​( ) &, |, !, pay attention to the difference between bit operators, do not use them wrongly when using them, the following assumes that the variable a is 10 and b is 20:

operator describe example
and x and y boolean "and" - if x is False, x and y returns False, otherwise it returns the computed value of y (a and b) returns 20.
or x or y 布尔”或” - 如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值 (a or b) 返回 10。
not not x 布尔”非” - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True not(a and b) 返回 False

6. 成员运算符

Python支持成员运算符, 支持字符串,列表或元组

运算符 描述 实例
in 如果在指定的序列中找到值返回True,否则返回False x 在 y序列中 , 如果x在y序列中返回True。
not in 如果在指定的序列中没有找到值返回True,否则返回False x 不在 y序列中 , 如果x不在y序列中返回True。

测试示例:

# 成员运算符
list1 = [1, 2, 3, 4, 5, 6]

print(3 in list1)         # True
print(10 in list1)        # False
print(10 not in list1)    # True


str1 = 'titanjun'
print('jun' in str1)       # True

7. 身份运算符

身份运算符用于比较两个对象的存储单元

运算符 描述 实例
is is是判断两个标识符是不是引用自一个对象 x is y, 如果 id(x) 等于 id(y) , is 返回结果 1
is not is not是判断两个标识符是不是引用自不同对象 x is not y, 如果 id(x) 不等于 id(y). is not 返回结果 1

8. Python运算符优先级

以下表格列出了从最高到最低优先级的所有运算符:

运算符 描述
** 指数 (最高优先级)
~, +, - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
*, /, %, // 乘,除,取模和取整除
+, - 加法减法
>>, << 右移,左移运算符
& 位 ‘AND’
`^, `
<=, <, >, >= 比较运算符
<>, ==, != 等于运算符
=, %=, /=, //=, -=, +=, *=, **= 赋值运算符
is, is not 身份运算符
in, not in 成员运算符
not, or, and 逻辑运算符

二. 条件和循环语句

  • Python程序语言指定任何非0和非空null值为true,0 或者 nullfalse
  • 由于 python 并不支持 switch 语句,所以多个条件判断,只能用 elif 来实现

1. if语句

  • if语句的使用和其他语言没什么区别, 主要是格式不一样, 没有大括号, 主要时注意缩进
if 判断条件:
    执行语句……
elif 判断条件:
    执行语句……
else
    执行语句……

使用示例:

# if-else语句
if 2 > 3:
    print('真的')
else:
    print("假的")

# if-elif语句
a = 3
if (a == 1):
    print(1)
elif (a == 2):
    print(2)
else:
    print(3)

2. While循环语句

与C语言中用法一样, 这里就不在赘述了

3. for 循环语句

Pythonfor循环可以遍历任何序列的项目,如一个列表或者一个字符串。

# for循环
# 输出每一个字符
for s in "char":
    print(s)

# 使用索引
list2 = [5, 2, 4]
for index in range(len(list2)):
    print(list2[index])
  • 其中, 函数 len() 返回列表的长度,即元素的个数。 range返回一个序列的数, 这个后面会说到
  • python 中,for … else 表示for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。

  • 对于Python语言, 我也是小白, 正在努力学习中, 文中如有不足之处, 还望多多指教

  • 测试代码详见 GitHub地址
  • 后期会持续更新相关文章

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906351&siteId=291194637