python_basic syntax

1. Operator

arithmetic operator

+ add - add two objects a + b output result 31
- Subtract - get a negative number or subtract one number from another a - b output result -11
* multiply - multiply two numbers or return a string repeated several times a * b output result 210
/ Divide - x divided by y b / a output result 2.1
% Modulo - returns the remainder of the division b % a output result 1
** power - returns x raised to the power of y a**b is 10 to the 21st power
// Round and divide - Round to the smallest
>>> 9//2
4

comparison operator  

== equals - compares objects for equality
!= not equal - compares if two objects are not equal
> greater than - returns whether x is greater than y
< 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.
>= greater than or equal to - Returns whether x is greater than or equal to y.
<= Less than or equal to - Returns whether x is less than or equal to y.

assignment operator

= simple assignment operator c = a + b assign the operation result of a + b to c
+= Additive 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
**= power assignment operator c **= a is equivalent to c = c ** a
//= integer division assignment operator c //= a is equivalent to c = c // a

bitwise operator

& Bitwise AND: 1 if both are 1, otherwise 0
|       Bitwise OR: 1 if there is only one 1
~ Inversion: when 0 is 1, when 1 is 0
^ Bitwise XOR: 1 and 0 are 1, otherwise 0
<< Left shift: how much you shift to the left, you will delete the previous number, and then add the corresponding number at the end
>> Move right: How much is moved to the right, how much will be deleted at the end, and then the head will be added accordingly

Logical Operators

and 1 and 1   == 1
or 1 or 0      == 1
not not 1        ==0

 member operator

in x exists in the specified sequence, then it is 1
not in

identity operator 

is is is to judge whether two identifiers refer to an object x is y , like  id(x) == id(y)  , returns True if they refer to the same object, otherwise returns False
is not is not is to judge whether two identifiers refer to different objects x is not y  , like  id(x) != id(y) . Returns True if the reference is not the same object, False otherwise.

number

int integer

floatfloat type

complex plural:

a + bj,或者complex(a,b)

convert

int(var) : Convert var to an integer

float(var) : convert var to float

complex(var) or complex(x, y): convert var to complex

num1 = "12"
num2 = int(num1)

num3 = "1.23"
num4 = float(num3) 

Common Mathematical Functions

abs(x) take the absolute value
sqrt(x) prescription
pow(x, y) x**y

constant 

pi           The mathematical constant pi (pi, generally expressed in π)
e                                         The mathematical constant e, e is the natural constant (natural constant).

2. string 

Via ' or ' '  . Generally, you can get the value of the corresponding element with [] . (Starting from 0, -1 takes the last element)

(Quoted from rookie tutorial python)
 

escape character

\n : Newline

\t :tab

\" :"

\' :'

format character (similar to c language)

Emphasis on the str.format() function

Python format formatting function | rookie tutorial (runoob.com)

f-string

triple quotes

三引号允许字符串跨多行字符串中可以包含换行符、制表符以及其他特殊字符

常见的字符串相关函数

3.列表

 列表类似于数组,下表从0开始索引,可以从-1(末尾)开始往前遍历

同时,可以通过list[num]获取对应下标的值

常见的与列表相关的函数

len(list)  == 得到列表的长度

max(list) == 得到列表最大元素

min(list) == 得到列表最小元素

方法:

list.append(num) == 末尾添加新元素

list.count(num) == 统计num在列表中出现的次数

list.extend(seq) == 在末尾追加另一个列表

list.index(obj) == 找到obj的下标

list.insert(index, obj) == 插入

list.pop([index = -1]) == 弹出一个元素,默认为最后一个

list.remove(obj) == 移除obj,没有返回值,只删除第一个obj

list.reverse() == 反转

list.sort()  == 排序

list.clear() == 清空

list.copy() == 复制

4.元组

 元组与列表类似,但是元组中的元素不能修改,但可通过del 删除真个元组

元组使用 () , 而列表使用 []

下标索引从 0 开始,可以进行截取

5.字典

6.集合

 

Guess you like

Origin blog.csdn.net/weixin_43537097/article/details/130019738