<ul class="first_class_ul list-paddingleft-2"><ul class="second_class_ul list-paddingleft-2"><ul class="third_class_ul list-paddingleft-2"></ul></ul></ul>
<h3>Constants and expressions</h3>
<h4>1. Simple introduction</h4>
<p>We can first take a brief look at Python Addition, subtraction, multiplication and division</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">print(1 + 2 + 5)
print(1 + 2 * 5)
print(1 + 2 - 5)
print(1 + 2 / 5)
# 运行结果
8
11
-2
1.4</pre><div class="contentsignin">Copy after login</div></div><p>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</p><ul class=" list-paddingleft-2"><li><p>print is a Python built-in function</p></li><li><p>You can use - * / ( ) and other operators perform arithmetic operations. Multiplication and division are calculated first, followed by addition and subtraction </p></li><li><p> 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)</p></li></ul><h4>2. Naming rules for variables</h4><ul class=" list-paddingleft-2"><li><p>Variables must be composed of numbers, letters and underscores. It cannot contain other special symbols, <strong> and cannot start with a number</strong></p></li><li><p>Variable names cannot conflict with keywords</p></li><li><p>In Python , variable names are case-sensitive</p></li><li><p>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) </p></li></ul><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">userName = '驼峰命名法'
user_name = "蛇形命名法"
_test = 10</pre><div class="contentsignin">Copy after login</div></div><h4>3. Variable type</h4><p> 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 </p><h5>1) Integer </h5><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">tmp = 10
print(type(tmp))
# 运行结果
<class 'int'></pre><div class="contentsignin">Copy after login</div></div><p>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</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">tmp = 1234567891011121314
print(tmp)</pre><div class="contentsignin">Copy after login</div></div><p>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</p><h5>2) Floating point number</h5><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">tmp = 3.14
print(type(tmp))
# 运行结果
<class 'float'></pre><div class="contentsignin">Copy after login</div></div><p>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) </p><h5>3) String</h5><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">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</pre><div class="contentsignin">Copy after login</div></div><p>In python, strings are enclosed in single quotes or double quotes. There is no difference between the two. </p><p>But if single quotes appear in the string, they can be nested </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">str3 = "hello:'java'"
str4 = 'hello: "python"'
print(str3)
print(str4)
# 运行结果
hello:'java'
hello: "python"</pre><div class="contentsignin">Copy after login</div></div> <p>There is also a triple quote in Python, which can contain single quotes and double quotes </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">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 """</pre><div class="contentsignin">Copy after login</div></div><p> Find the character length. Find the length of a string in Python through the built-in function len </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">str1 = 'hello'
str2 = "world"
print(len(str1))
print(len(str2))
str3 = "'''test '''"
str4 = '""" test """'
print(len(str3))
print(len(str4))
# 运行结果
5
5
11
12</pre><div class="contentsignin">Copy after login</div></div><p>Characters Note on string splicing: In Python, only strings and characters can be spliced. Splicing other types of variables will result in an error </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">str1 = 'hello'
str2 = "world"
print(str1 + str2)
print(str2 + str1)
# 运行结果
helloworld
worldhello</pre><div class="contentsignin">Copy after login</div></div><h5>4) Boolean type</h5><p>The Boolean type is a special type that takes a value There are only two types, True (true) and False (false)</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">a = True
b = False
print(type(a))
print(type(b))
# 运行结果
<class 'bool'>
<class 'bool'></pre><div class="contentsignin">Copy after login</div></div><p><strong>Notes</strong> If the Boolean type is operated on an integer or floating point number type, True represents 1 and False represents 0. </p><h4>4. Dynamic type feature</h4><p>In Python, the type of a variable can change during the "program running" process. This feature is called "dynamic type"</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">tmp = 10
print(type(tmp))
tmp = 'test'
print(type(tmp))
tmp = False
print(type(tmp))
# 运行结果
<class 'int'>
<class 'str'>
<class 'bool'></pre><div class="contentsignin">Copy after login</div></div><h4>5. Display the specified type</h4><p>Although you do not need to manually specify the type in Python, you can also display the specified type</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">a: int = 10
b: str = 'str'
c: float = 3.14</pre><div class="contentsignin">Copy after login</div></div><h3>Comments</h3><h4>1. Line comments</h4><p>In Python, lines starting with # are comments </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"># 这是第一行注释
# 这是第二行注释</pre><div class="contentsignin">Copy after login</div></div><h4>2. Document string </h4><p> enclosed in triple quotes is called "doc string", which can also be viewed as It is a kind of comment. </p><ul class=" list-paddingleft-2"><li><p> can contain multiple lines of content, </p></li><li><p> is usually placed at the beginning of the file/function/class</p></li><li><p>""" or ‘’’ can be used (equivalent) </p></li></ul><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">"""
这
是
多行注释
"""
'''
这
也是多行注释
'''</pre><div class="contentsignin">Copy after login</div></div><h3>Input and output</h3><h4>1. Through the console Output</h4><p>As mentioned before, use the Python built-in function print to output data to the console</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">number = 10
tmp = False
print(number)
print(tmp)
# 输出
10
False</pre><div class="contentsignin">Copy after login</div></div><p>More often, we hope that the output content is a mixture of strings and variables</p><p>Example</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">name = '张三'
age = 18
print(f"name = {name}" f'age = {age}')
# 运行结果
name = 张三age = 18</pre><div class="contentsignin">Copy after login</div></div><ul class=" list-paddingleft-2"><li><p>A string using f as the prefix is called f-string</p></li><li><p>{ } can be used inside Embed another variable/expression</p></li></ul><h4>2. Input through the console</h4><p>python uses the input function to read user input from the console</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">tmp = input()
print(tmp)</pre><div class="contentsignin">Copy after login</div></div> <p>Or input with prompts</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">name = input('请输入姓名:')
age = input('请输入年龄:')
print(f'name = {name}')
print(f'age = {age}')
# 运行结果
请输入姓名:张三
请输入年龄:18
name = 张三
age = 18</pre><div class="contentsignin">Copy after login</div></div><ul class=" list-paddingleft-2"><li><p>The parameter of input is equivalent to a "prompt message", or there may be no input.</p></li><li><p>input The return value is what the user inputs. It is a string type</p></li></ul><p>Because the input data is of string type by default. If necessary, it must be forced to type zhuangh</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">num1 = int(input("请输入第一个数字:"))
num2 = int(input("请输入第二个数字:"))
print(f'num1 + num2 = {num1 + num2}')</pre><div class="contentsignin">Copy after login</div></div><h3>Operator</h3><h4>1. Arithmetic operators</h4><p>in Python are<code> - * / % ** //</code> Seven kinds of operators</p><p><strong>Note 1</strong>: 0 cannot be used as a divisor. If used as a divisor, an exception will be thrown </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">print(5/0)</pre><div class="contentsignin">Copy after login</div></div><p><strong>Note 2: </strong> The result of dividing an integer by an integer in Python It may be a decimal because truncation will not occur</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">print(9/4)
# 执行结果
2.25</pre><div class="contentsignin">Copy after login</div></div><p><strong>注意事项3:</strong> 在Python中 <code>//</code> 这个符号,等同于C/Java中的除号,就是整数除以整数就会得到整数,会发生截断</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">print(9//4)
运行结果
2</pre><div class="contentsignin">Copy after login</div></div><p><strong>注意事项4:</strong> <code>**</code>是次方的意思,比如 3**4 就表示的是 34,它也可以表示小数次方,比如 9**0.5 就表示为对9开方</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">print(3**4)
print(9**0.5)
运行结果
81
3.0</pre><div class="contentsignin">Copy after login</div></div><p><strong>注意事项5:</strong> 正对负数取余,结果会是正数</p><h4>2. 关系运算符</h4><p>关系运算符就是用来比较两个操作数的大小是否相等的,<code><</code> 、<code>></code>、<code><=</code>、<code>>=</code>、<code>==</code>、<code>!=</code></p><p>关系运算符返回的是布尔值,如果表达式为真就返回True如果表达式为假就返回False</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">a = 10
b = 15
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
运行结果
False
True
False
True</pre><div class="contentsignin">Copy after login</div></div><p>关系运算符不但可以针对数字进行比较,还能够比较字符串,可以比较字符相等</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">a = 'hello'
b = 'hello'
print(a == b)
运行结果
True</pre><div class="contentsignin">Copy after login</div></div><p>还可以比较字符串大小,比较字符串大小是通过字典序来比较的,首先看首字母在字母表上的顺序,谁小,谁就排在前面,如果首字母相同,就一次比较第二字母、第三个字母…</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">a = 'abcd'
b = 'abce'
print(a > b)
print(a < b)
# 运行结果
False
True</pre><div class="contentsignin">Copy after login</div></div><p><strong>注意事项</strong> 对于浮点数来说,使用 <code>==</code>进行比较相等时存在一定的风险的,因为浮点数在内存中的存储和表示,是可能存在误差的,这样的误差在进行算数运算的时候就可能被放大,从而导致 <code>==</code>的判断出现误判</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">a = 0.1 + 0.2
b = 0.3
print(a == b)
print(a)
print(b)
运行结果
False
0.30000000000000004
0.3</pre><div class="contentsignin">Copy after login</div></div><p>对于浮点数正确的比较方式:就是不在严格比较相等,而是判定它的差值是否小于允许的误差范围以内</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">a = 0.1 + 0.2
b = 0.3
print(-0.000001 < a-b < 0.000001)
运行结果
True</pre><div class="contentsignin">Copy after login</div></div><h4>3. 逻辑运算符</h4><p>在Python中逻辑运算符为<code>and or not</code></p><ul class=" list-paddingleft-2"><li><p><strong>and</strong> 并且:两端为True则为True,一端为False则为False</p></li><li><p><strong>or</strong> 或者:两端都为False则为False,否则为True</p></li><li><p>not 逻辑取反:本身为True,取反为False,本身为False取反为True</p></li></ul><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">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</pre><div class="contentsignin">Copy after login</div></div><p>Python一种特殊写法 <code>a < b and b < c</code> 这个等价于<code>a < b < c</code></p><p><strong>短路操作</strong> <code>or</code>和<code>and</code>都有短路</p><ul class=" list-paddingleft-2"><li><p><strong>and</strong>:如果前面为假后面的就不会再执行了</p></li><li><p><strong>or</strong>:如果前面为真后面就不会再执行了</p></li></ul><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">a = 10
b = 20
c = 30
print(b < a and 10/0)
print(b > a or 10/0)</pre><div class="contentsignin">Copy after login</div></div><h4>4. 赋值运算符</h4><p><strong>链式赋值</strong></p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">a = b = c = 10</pre><div class="contentsignin">Copy after login</div></div><p><strong>多元赋值</strong></p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">a, b, c = 10, 20, 30</pre><div class="contentsignin">Copy after login</div></div><p>示例:交换两个变量的值 传统写法</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">a = 10
b = 20
tmp = a
a = b
b = tmp</pre><div class="contentsignin">Copy after login</div></div><p>使用多远赋值</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:py;">a = 10
b = 20
a, b = b, a</pre><div class="contentsignin">Copy after login</div></div><p>注意:Python中不存在像 C/Java的++、–操作</p>
<p>除了上述之外, Python 中还有一些运算符, 比如 身份运算符 (is, is not), 成员运算符 (in, not in), 位运算符 ( & | ~ ^ << >>) 等</p>
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!