Home > Article > Backend Development > Definition of Python variables and how to use operators
We can first take a brief look at Python Addition, subtraction, multiplication and division
print(1 + 2 + 5) print(1 + 2 * 5) print(1 + 2 - 5) print(1 + 2 / 5) # 运行结果 8 11 -2 1.4
We found that addition, subtraction, multiplication and other languages are basically different, but in other languages such as C/Java, the result of dividing an integer by an integer is still an integer, that is, the decimal part will be truncated, but in It will not be truncated in Python, which is more in line with people's intuition for daily calculations
print is a Python built-in function
You can use - * / ( ) and other operators perform arithmetic operations. Multiplication and division are calculated first, followed by addition and subtraction
There can be no space or multiple spaces between the operator and the number. However, It is generally customary to write a space (more beautiful)
Variables must be composed of numbers, letters and underscores. It cannot contain other special symbols, and cannot start with a number
Variable names cannot conflict with keywords
In Python , variable names are case-sensitive
It is recommended to use camel case naming method for variable naming (the first letter of other words except the first word is capitalized), or use snake-like naming. Method (use underscores to separate multiple words)
userName = '驼峰命名法' user_name = "蛇形命名法" _test = 10
Unlike C/Java, Python does not need to be specified explicitly when defining variables The type of the variable will be automatically determined when assigning a value
tmp = 10 print(type(tmp)) # 运行结果 <class 'int'>
type is a built-in function in Python. You can use type to check the type of a variable. Note: and C/ Different from Java and other languages, Python's int type variable has no upper limit on the data range that can be represented. As long as the memory is sufficient, it can theoretically represent unlimited size numbers
tmp = 1234567891011121314 print(tmp)
Because Python's int can be represented as needed The data size is automatically expanded, so Python does not have types like long, byte/short
tmp = 3.14 print(type(tmp)) # 运行结果 <class 'float'>
Note: Unlike C/Java language, Python only has float for decimals. There is no double type, but in fact python is equivalent to double in C/Java, which represents a double-precision floating point number (8 bytes)
str1 = 'hello'str2 = "world"print(type(str1))print(str1)print(str2)# 运行结果<class 'str'>helloworldastr1 = 'hello' str2 = "world" print(type(str1)) print(str1) print(str2) # 运行结果 <class 'str'> hello world
In python, strings are enclosed in single quotes or double quotes. There is no difference between the two.
But if single quotes appear in the string, they can be nested
str3 = "hello:'java'" str4 = 'hello: "python"' print(str3) print(str4) # 运行结果 hello:'java' hello: "python"
There is also a triple quote in Python, which can contain single quotes and double quotes
str3 = ''' test "hello:'java'"''' str4 = """ test "hello:'java' """ print(str3) print(str4) # 运行结果 test "hello:'java'" test "hello:'java' str3 = "'''test '''" str4 = '""" test """' print(str3) print(str4) # 运行结果 '''test ''' """ test """
Find the character length. Find the length of a string in Python through the built-in function len
str1 = 'hello' str2 = "world" print(len(str1)) print(len(str2)) str3 = "'''test '''" str4 = '""" test """' print(len(str3)) print(len(str4)) # 运行结果 5 5 11 12
Characters Note on string splicing: In Python, only strings and characters can be spliced. Splicing other types of variables will result in an error
str1 = 'hello' str2 = "world" print(str1 + str2) print(str2 + str1) # 运行结果 helloworld worldhello
The Boolean type is a special type that takes a value There are only two types, True (true) and False (false)
a = True b = False print(type(a)) print(type(b)) # 运行结果 <class 'bool'> <class 'bool'>
Notes If the Boolean type is operated on an integer or floating point number type, True represents 1 and False represents 0.
In Python, the type of a variable can change during the "program running" process. This feature is called "dynamic type"
tmp = 10 print(type(tmp)) tmp = 'test' print(type(tmp)) tmp = False print(type(tmp)) # 运行结果 <class 'int'> <class 'str'> <class 'bool'>
Although you do not need to manually specify the type in Python, you can also display the specified type
a: int = 10 b: str = 'str' c: float = 3.14
In Python, lines starting with # are comments
# 这是第一行注释 # 这是第二行注释
enclosed in triple quotes is called "doc string", which can also be viewed as It is a kind of comment.
can contain multiple lines of content,
is usually placed at the beginning of the file/function/class
""" or ‘’’ can be used (equivalent)
""" 这 是 多行注释 """ ''' 这 也是多行注释 '''
As mentioned before, use the Python built-in function print to output data to the console
number = 10 tmp = False print(number) print(tmp) # 输出 10 False
More often, we hope that the output content is a mixture of strings and variables
Example
name = '张三' age = 18 print(f"name = {name}" f'age = {age}') # 运行结果 name = 张三age = 18
A string using f as the prefix is called f-string
{ } can be used inside Embed another variable/expression
python uses the input function to read user input from the console
tmp = input() print(tmp)
Or input with prompts
name = input('请输入姓名:') age = input('请输入年龄:') print(f'name = {name}') print(f'age = {age}') # 运行结果 请输入姓名:张三 请输入年龄:18 name = 张三 age = 18
The parameter of input is equivalent to a "prompt message", or there may be no input.
input The return value is what the user inputs. It is a string type
Because the input data is of string type by default. If necessary, it must be forced to type zhuangh
num1 = int(input("请输入第一个数字:")) num2 = int(input("请输入第二个数字:")) print(f'num1 + num2 = {num1 + num2}')
in Python are - * / % ** //
Seven kinds of operators
Note 1: 0 cannot be used as a divisor. If used as a divisor, an exception will be thrown
print(5/0)
Note 2: The result of dividing an integer by an integer in Python It may be a decimal because truncation will not occur
print(9/4) # 执行结果 2.25
注意事项3: 在Python中 //
这个符号,等同于C/Java中的除号,就是整数除以整数就会得到整数,会发生截断
print(9//4) 运行结果 2
注意事项4: **
是次方的意思,比如 3**4 就表示的是 34,它也可以表示小数次方,比如 9**0.5 就表示为对9开方
print(3**4) print(9**0.5) 运行结果 81 3.0
注意事项5: 正对负数取余,结果会是正数
关系运算符就是用来比较两个操作数的大小是否相等的,c1743839dbf03bcf8100b6aa1e69ccf2
、e41e9ffb4fa1bfd6844985de5647f5f4=
、==
、!=
关系运算符返回的是布尔值,如果表达式为真就返回True如果表达式为假就返回False
a = 10 b = 15 print(a > b) print(a < b) print(a >= b) print(a <= b) 运行结果 False True False True
关系运算符不但可以针对数字进行比较,还能够比较字符串,可以比较字符相等
a = 'hello' b = 'hello' print(a == b) 运行结果 True
还可以比较字符串大小,比较字符串大小是通过字典序来比较的,首先看首字母在字母表上的顺序,谁小,谁就排在前面,如果首字母相同,就一次比较第二字母、第三个字母…
a = 'abcd' b = 'abce' print(a > b) print(a < b) # 运行结果 False True
注意事项 对于浮点数来说,使用 ==
进行比较相等时存在一定的风险的,因为浮点数在内存中的存储和表示,是可能存在误差的,这样的误差在进行算数运算的时候就可能被放大,从而导致 ==
的判断出现误判
a = 0.1 + 0.2 b = 0.3 print(a == b) print(a) print(b) 运行结果 False 0.30000000000000004 0.3
对于浮点数正确的比较方式:就是不在严格比较相等,而是判定它的差值是否小于允许的误差范围以内
a = 0.1 + 0.2 b = 0.3 print(-0.000001 < a-b < 0.000001) 运行结果 True
在Python中逻辑运算符为and or not
and 并且:两端为True则为True,一端为False则为False
or 或者:两端都为False则为False,否则为True
not 逻辑取反:本身为True,取反为False,本身为False取反为True
a = 10 b = 20 c = 30 print(b > a and b > c) print(b > a or b > c) print(not a > b) 运行结果 False True True
Python一种特殊写法 a 4c0cdbb2680faf2efdb07c2cfa990c00>) 等
The above is the detailed content of Definition of Python variables and how to use operators. For more information, please follow other related articles on the PHP Chinese website!