Table of Contents
Constants and expressions
1. Simple introduction
2. Naming rules for variables
3. Variable type
1) Integer
2) Floating point number
3) String
4) Boolean type
4. Dynamic type feature
5. Display the specified type
Comments
1. Line comments
2. Document string
Input and output
1. Through the console Output
2. Input through the console
Operator
1. Arithmetic operators
2. 关系运算符
3. 逻辑运算符
4. 赋值运算符
Home Backend Development Python Tutorial Definition of Python variables and how to use operators

Definition of Python variables and how to use operators

May 19, 2023 am 08:04 AM
python

<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 id="Constants-and-expressions">Constants and expressions</h3> <h4 id="Simple-introduction">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:php;toolbar:false;'>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 id="Naming-rules-for-variables">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:php;toolbar:false;'>userName = &#39;驼峰命名法&#39; user_name = "蛇形命名法" _test = 10</pre><div class="contentsignin">Copy after login</div></div><h4 id="Variable-type">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 id="Integer">1) Integer </h5><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>tmp = 10 print(type(tmp)) # 运行结果 <class &#39;int&#39;></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:php;toolbar:false;'>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 id="Floating-point-number">2) Floating point number</h5><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>tmp = 3.14 print(type(tmp)) # 运行结果 <class &#39;float&#39;></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 id="String">3) String</h5><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>str1 = &#39;hello&#39;str2 = "world"print(type(str1))print(str1)print(str2)# 运行结果<class &#39;str&#39;>helloworldastr1 = &#39;hello&#39; str2 = "world" print(type(str1)) print(str1) print(str2) # 运行结果 <class &#39;str&#39;> 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:php;toolbar:false;'>str3 = "hello:&#39;java&#39;" str4 = &#39;hello: "python"&#39; print(str3) print(str4) # 运行结果 hello:&#39;java&#39; 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:php;toolbar:false;'>str3 = &#39;&#39;&#39; test "hello:&#39;java&#39;"&#39;&#39;&#39; str4 = """ test "hello:&#39;java&#39; """ print(str3) print(str4) # 运行结果 test "hello:&#39;java&#39;" test "hello:&#39;java&#39; str3 = "&#39;&#39;&#39;test &#39;&#39;&#39;" str4 = &#39;""" test """&#39; print(str3) print(str4) # 运行结果 &#39;&#39;&#39;test &#39;&#39;&#39; """ 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:php;toolbar:false;'>str1 = &#39;hello&#39; str2 = "world" print(len(str1)) print(len(str2)) str3 = "&#39;&#39;&#39;test &#39;&#39;&#39;" str4 = &#39;""" test """&#39; 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:php;toolbar:false;'>str1 = &#39;hello&#39; str2 = "world" print(str1 + str2) print(str2 + str1) # 运行结果 helloworld worldhello</pre><div class="contentsignin">Copy after login</div></div><h5 id="Boolean-type">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:php;toolbar:false;'>a = True b = False print(type(a)) print(type(b)) # 运行结果 <class &#39;bool&#39;> <class &#39;bool&#39;></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 id="Dynamic-type-feature">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:php;toolbar:false;'>tmp = 10 print(type(tmp)) tmp = &#39;test&#39; print(type(tmp)) tmp = False print(type(tmp)) # 运行结果 <class &#39;int&#39;> <class &#39;str&#39;> <class &#39;bool&#39;></pre><div class="contentsignin">Copy after login</div></div><h4 id="Display-the-specified-type">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:php;toolbar:false;'>a: int = 10 b: str = &#39;str&#39; c: float = 3.14</pre><div class="contentsignin">Copy after login</div></div><h3 id="Comments">Comments</h3><h4 id="Line-comments">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 id="Document-string">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">""" 这 是 多行注释 """ &#39;&#39;&#39; 这 也是多行注释 &#39;&#39;&#39;</pre><div class="contentsignin">Copy after login</div></div><h3 id="Input-and-output">Input and output</h3><h4 id="Through-the-console-Output">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:php;toolbar:false;'>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:php;toolbar:false;'>name = &#39;张三&#39; age = 18 print(f"name = {name}" f&#39;age = {age}&#39;) # 运行结果 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 id="Input-through-the-console">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:php;toolbar:false;'>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:php;toolbar:false;'>name = input(&#39;请输入姓名:&#39;) age = input(&#39;请输入年龄:&#39;) print(f&#39;name = {name}&#39;) print(f&#39;age = {age}&#39;) # 运行结果 请输入姓名:张三 请输入年龄: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:php;toolbar:false;'>num1 = int(input("请输入第一个数字:")) num2 = int(input("请输入第二个数字:")) print(f&#39;num1 + num2 = {num1 + num2}&#39;)</pre><div class="contentsignin">Copy after login</div></div><h3 id="Operator">Operator</h3><h4 id="Arithmetic-operators">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:php;toolbar:false;'>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:php;toolbar:false;'>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:php;toolbar:false;'>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:php;toolbar:false;'>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 id="关系运算符">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:php;toolbar:false;'>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:php;toolbar:false;'>a = &#39;hello&#39; b = &#39;hello&#39; 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:php;toolbar:false;'>a = &#39;abcd&#39; b = &#39;abce&#39; 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:php;toolbar:false;'>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:php;toolbar:false;'>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 id="逻辑运算符">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:php;toolbar:false;'>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:php;toolbar:false;'>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 id="赋值运算符">4. 赋值运算符</h4><p><strong>链式赋值</strong></p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>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:php;toolbar:false;'>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:php;toolbar:false;'>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:php;toolbar:false;'>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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

See all articles