Learn python (1) from scratch - expressions, statements, variables and operators

This article can be regarded as the real first article of learning Python from scratch, officially starting to understand the basic syntax of Python.

expressions and statements

A statement is a unit of code that has an effect, such as creating a new variable

insert image description here

At this time, the value of n is given as 17.

Expressions are combinations of values, variables, and operators. A value itself is considered an expression, as are variables, so the following are all valid expressions:insert image description here

variable name

Usually, the programmer will choose a more meaningful name for the variable, so as to record the purpose of the variable.

Variable names can be composed of letters, numbers and underscores _, but cannot start with a number, and are strictly case-sensitive. The naming principles of variable names have different guidelines for different occasions or companies. For us beginners, we need to ensure that The naming style in the whole program is uniform. Here are a few different naming situations:

insert image description here

  1. We can find that the previous four cases are statements that can be executed normally, and then to prove that they are four different variables, let's take a look at their values:

insert image description here

​ It can also be seen from the running results that the variables with different case are indeed two different variables.

  1. 8money = 11A grammatical error occurred, which also proves that the variable name mentioned above cannot start with a number.
  2. True = 5A syntax error also occurred, why is this? We see true = 5that it can run normally, so we have to talk about another concept keyword .

Keyword : It is a pre-defined identifier in the programming language, also known as a reserved word. Keywords have their own unique functions and cannot be used as variable names. Of course, it is best not to use variable names that are only case-sensitive from keywords. eg true.

Python3 has the following keywords:

‘False’, ‘None’, ‘True’, ‘and’, ‘as’, ‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’

For the above keywords, we don’t need to memorize them deliberately. Usually, most compilers will perform syntax highlighting on keywords, and you will naturally remember some commonly used keywords twice more.

operator

Python provides various operators:

  • arithmetic operator
  • comparison operator
  • assignment operator
  • Logical Operators
  • bitwise operator
  • member operator
  • identity operator

arithmetic operator

Python provides some commonly used arithmetic operators: for the convenience of demonstration, unless otherwise specified, the following two variables appear x = 9, y = 2

operator illustrate example
+ adds two values x + y result: 11
- subtracts two values x - y Result: 7
* multiply two values x * y result: 18
/ divides two values x/y result: 4.5
% Take the modulus to get the remainder of the division of two values x % y Result: 1
** Exponentiation to get x raised to the yth power x ** y Result: 81
// Rounding off to get the integer part of the quotient of dividing two values ​​(downwards) 9 // 2 results in: 4; -9 // 2 results in -5

comparison operator

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

assignment operator

operator illustrate example
= simple assignment operator z = x + y assigns the operation result of x + y to z
+= Additive assignment operator z += x is equivalent to z = z + x
-= subtraction assignment operator z -= a is equivalent to z = z - x
*= multiplication assignment operator z *= x is equivalent to z = z * x
/= division assignment operator z /= x is equivalent to z = z / x
%= modulo assignment operator z %= x is equivalent to z = z % x
**= power assignment operator z * * = a is equivalent to z = z * * x
//= integer division assignment operator z //= x is equivalent to z = z // x

Logical Operators

operator illustrate example
and Boolean AND, x and y, if x is False, x and y returns False, otherwise it returns the computed value of y (x and y) returns 2
or Boolean "or", x or y, if x is True, it returns the value of x, otherwise it returns the computed value of y (x or y) returns 9
not Boolean "not", not x, returns False if x is True. It returns True if x is False not(x and y) returns False

bit operation

Bitwise operation is to calculate the number as a binary number. For the convenience of operation, the following variables a = 5, b = 3; the four-bit binary expression is: a = 0000 0101, b = 0000 0011;

operator illustrate example
& Bitwise AND operator: Two values ​​involved in the operation, if both corresponding bits are 1, the result of the bit is 1, otherwise it is 0 (a & b) output result 1, binary interpretation: 0000 0001
| Bitwise OR operator: as long as one of the corresponding two binary bits is 1, the result bit is 1 (a | b) Output 7, binary interpretation: 0000 0111
^ Bitwise XOR operator: When two corresponding binary bits are different, the result is 1 (a ^ b) output result 6, binary interpretation: 0000 0110
~ 按位取反运算符:对数据的每个二进制位取反,即把1变为0,把0变为1。~x 类似于 -x-1 (~a ) 输出结果-6,二进制解释: 1111 1010, 在一个有符号二进制数的补码形式
<< 左移动运算符:运算数的各二进位全部左移若干位,由<<右边的数指定移动的位数,高位丢弃,低位补0 a << 2 输出结果 20,二进制解释: 0001 0100
>> 右移动运算符:把>>左边的运算数的各二进位全部右移若干位,>>右边的数指定移动的位数 a >> 2 输出结果 1,二进制解释: 0000 0001

成员运算符

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

下面我会通过一个小程序来解释:

x = 8
y = 10
list = [1, 2, 3, 8, 9]
if(x in list):
    print("x-存在")
else:
    printf("x-不存在")
if(y not in list):
    print("y-不存在")
else:
    print("y-存在")

运行结果:

x-存在

y-不存在

身份运算符

运算符 说明 实例
is is 是判断两个标识符是不是引用自一个对象 x is y,如果引用的是同一个对象,则返回True,否则返回False
is not is not 是判断两个标识符是不是引用自不同对象 x is not y,如果引用的不是同一个对象,则返回True,否则返回False

运算符优先级

我们在小学的时候就已经知道了,算术时有先乘除后加减,有括号先算括号里面的,Python自然也遵循了这一原则,并且它也规定了其它运算符的优先级,下面表格列出了从最高到最低优先级的所有运算符,处于同一行的运算符属于同一优先级

运算符 说明
** 指数 (最高优先级)
~ 按位翻转
* / % // 乘,除,取模和取整除
+ - 加法减法
>> << 右移,左移运算符
& 位 ‘AND’
^ | 位运算符
<= < > >= comparison operator
== != equal to operator
= %= /= //= -= += *= **= assignment operator
is is not identity operator
in not in member operator
not or and Logical Operators

Of course, if you are afraid of not being able to remember these priorities, there is a very simple and reliable method: add parentheses to each operation step.

Guess you like

Origin blog.csdn.net/m0_46376148/article/details/108438980