python basis
Three ways formatted output
Placeholder
For example, I want to print this format
My name is kang, My age is 18.
Using string concatenation too much trouble, then, that we will use placeholders (with lies 3.0) version)
name = 'kang'
age = 18
print("My name is %s, My age is %s" % (name, age))
My name is kang, My age is 18
(Note:% s (for all data types),% d (only for the number types)
format format
Speak true method of formatting is rubbish, if you need to use this encounter multi-parameter time, or need to pass on a lot of parameters crackling behind the sentence. This is better to use with placeholders or below f-String format (format for version 3.4)
name = 'kang'
age = 18
print("My name is {0}, My age is {1}".format(name, age))
My name is kang, My age is 18
f-String formatting
Compared placeholder way, python3.6 version adds f-String formatted way, relatively simple to understand, this is the way I use the most is recommended to use this way
name = 'kang'
age = 18
print(f"My name is {kang}, My age is {age}")
My name is kang, My age is 18.
Basic operators
Arithmetic Operators
Pupils know everything, simple addition and subtraction, multiplication and division, do not do too much introduction.
print(1+2)
3
x = 1
y = 2
res = x+y
print(res)
3
# /有零有整除,得到一个浮点型
print(10/3)
print(10//4)
3
2
# %:取余
print(10 % 3)
1
# **,幂
print(10**3)
1000
Comparison Operators
Direct mapping, assume that the variable a is 10, b is 20 variable.
pwd = '123'
print(pwd != '123')
print(pwd == '123')
False
True
Assignment Operators
Also a direct mapping, simple and crude, assume that the variable a is 10, b is variable 20
age = 19
age = age + 1
print(age)
20
age = 19
age += 1
print(age)
20
Logical Operators
Mapping or direct, simple and crude, assume that the variable a is 10, b is variable 20
# 从左到右的方式找到逻辑运算符,找到逻辑运算符的左边,左边成立,再去找到逻辑运算符的右边
print(3 > 3 and 1 > 2 or 2 > 1) # False
True
Identity operator
and the difference is the ==: is used to determine whether two variables refer to the same objects (whether in the same memory space), a value for determining == reference variable is equal
a = 10
b = a
c = 10
print(f'a is b:{a is b}')
print(f'a == b:{a == b}')
print(f'a is z:{x is z}')
print(f'a == z:{x == z}')
a is b:True
a == b:True
a is c:False
a == c:True
Operator precedence
python operator precedence in mathematics equivalent of the first count and then count addition and subtraction multiplication and division, but this is silly high priority of your brackets on the line ...
# Python中True为1,False为0
print(True > 0) # True
print(True > 2) # Flase