Table of Contents
1. Python data types
Integer (int)
Long integer (long)
Float type (float)
Complex (complex)
Boolean value (True, False)
None value (None)
String (str)
List (list)
Tuple (tuple)
字典(dict)
二、Python数据运算
算数运算
赋值运算
比较运算
逻辑运算
身份运算
位运算
Python运算符优先级(从高到底依次排列)
Home Backend Development Python Tutorial Detailed summary of Python data types and operators (code examples)

Detailed summary of Python data types and operators (code examples)

Jan 25, 2019 am 10:22 AM
python

This article brings you a detailed summary (code example) of Python data types and operators. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The previous article talked about Python’s input, output and variables. This section will explore Python’s data types and the calculation methods between data!

1. Python data types

The previous section clarified variables. In fact, the values ​​pointed to by variables have their own unique data types. These data types may represent different data. In In Python, there are mainly the following data types:

Integer (int)

In computers, the number of digits in integers actually has a range, and is not infinite as we imagine. Moreover, the number of digits in the integer may be different on different machines. For example:

32-bit system: the number of digits in the integer is 32 bits, and the addressing range is: -231 ~ 231-1, that is -2147483648 ~ 2147483647

64-bit system: the number of digits in the integer is 64 bits, and the addressing range is: -263 ~ 263-1, that is -9223372036854775808 ~ 9223372036854775807

Long integer (long)

In Python, long integer does not specify the number of digits, that is to say , the long integer can be infinitely large, but due to the limitations of the machine itself, it often cannot be infinitely large, and it will not work within a certain range.

Float type (float)

The above two data types are all integers, but in reality they cannot all be integers, there are also decimals, so the floating point type came into being. , to put it bluntly, floating point type is a decimal, and scientific notation can be used. In computers, multiples of 10 in scientific notation are replaced by e. For example: 5.21x105 is written as 5.21e9, or 521e7

Complex (complex)

The above three data types combined are real numbers. In fact, complex numbers are often used in scientific calculations. In Python, there are complex data types, the general form is: x yj, In the formula, x and y are both real numbers, for example: 5 6j

Boolean value (True, False)

There are only two Boolean values: true and false. In Python, they are represented by True and False, which must be Pay attention to capitalizing the first letter. Python is case-sensitive, so be sure to pay attention.

In [77]: 5==6
Out[77]: False
In [78]: 3>2
Out[78]: True
In [79]: True == True
Out[79]: True
In [80]: True == False
Out[80]: False
Copy after login

None value (None)

There is only one null value: None. This null value is very interesting. None cannot be understood as 0, because 0 is not a null value, just like the temperature is 0 degrees Celsius, 0 degrees Celsius has a temperature ( ̄▽ ̄)"

String (str)

The string may be written The most commonly used data type in Python is a string as long as it is enclosed in quotes. Python does not distinguish between single quotes, double quotes and triple quotes. They are the same:

In [81]: 'Hello,world!'
Out[81]: 'Hello,world!'
In [82]: "Hello,world!"
Out[82]: 'Hello,world!'
In [83]: '''Hello,world'''
Out[83]: 'Hello,world'
Copy after login

There will definitely be newbies who will ask, what are you doing with so many things? If you can’t just use one, I’ll just smile silently and say nothing:

In [84]: 'i'm MinuteSheep'
  File "<ipython-input-84-a2a810ee38cb>", line 1
    &#39;i&#39;m MinuteSheep&#39;
       ^
SyntaxError: invalid syntax
Copy after login

Look! An error is reported. Why? Because Python will automatically match the nearest symbol and close it, so the above situation will occur. Make the following modifications:

In [85]: "i&#39;m MinuteSheep"
Out[85]: "i&#39;m MinuteSheep"
Copy after login

(Mengxin: You bad old man is very bad ╰(‵□′)╯)

Similarly, three quotation marks are used for multiple lines, or when the content symbols are confusing:

In [87]: &#39;&#39;&#39;i&#39;m MinuteSheep,i said:"i&#39;m the best man in the world!"&#39;&#39;&#39;
Out[87]: &#39;i\&#39;m MinuteSheep,i said:"i\&#39;m the best man in the world!"&#39;
In [86]: &#39;&#39;&#39;
    ...: i&#39;m MinuteSheep,
    ...: i said:
    ...: "i&#39;m the best man in the world!"
    ...: &#39;&#39;&#39;
Out[86]: &#39;\ni\&#39;m MinuteSheep,\ni said:\n"i\&#39;m the best man in the world!"\n&#39;
Copy after login

Perfect solution, do you remember what \n is? It means line break. Similarly, you will find that i'm becomes i\'m. This is actually It is the display of escape. I will talk about escaping later.

There is a newbie coming out again. Didn’t you say in the previous section that three quotes are multi-line comments? How did it become this section? String?

This is a good question! Look at my explanation, there are pictures and the truth:

The content in direct quotation marks is a comment, as long as Assign the content in triple quotes to a variable, which is a string.

There are many methods for strings. There will be a special section to explain the methods of strings in detail later.

List (list)

Newbies may be unfamiliar with lists. You can temporarily understand them as one-dimensional arrays. Lists are used quite a lot in Python and are a data type that must be mastered in addition to strings. Let’s take a look at what the list looks like:

In [88]: [&#39;MinuteSheep&#39;, &#39;LiMing&#39;, &#39;123&#39;, 123]
Out[88]: [&#39;MinuteSheep&#39;, &#39;LiMing&#39;, &#39;123&#39;, 123]
Copy after login

As you can see, the data enclosed by a pair of square brackets is a list, and there can be other data types in the Liu table. The above list includes: strings and integers. Of course, lists can contain lists, which is called nesting of lists:

In [89]: [&#39;MinuteSheep&#39;, [&#39;LiMing&#39;, 123]]
Out[89]: [&#39;MinuteSheep&#39;, [&#39;LiMing&#39;, 123]]
Copy after login

There are many more about lists The specific methods will not be introduced one by one here. There will be a special section explaining the list method later.

Tuple (tuple)

Tuples may be even more unfamiliar. In fact, tuples It is an immutable list. The list is enclosed by a set of square brackets, and the tuple is enclosed by a pair of round brackets. The list can be manipulated (such as adding, deleting, modifying, and searching), but the tuple cannot, and the tuple cannot. has been changed, let’s see what the tuple looks like:

In [90]: (&#39;MinuteSheep&#39;,&#39;LiMing&#39;,123)
Out[90]: (&#39;MinuteSheep&#39;, &#39;LiMing&#39;, 123)
Copy after login

字典(dict)

字典是Python的一种非常强大的数据类型,通过键值对的形式将数据保存下来,提高了数据增、删、改、查的速度,通常作为数据存储的格式,也来看看字典长啥样哇:

In [91]: {&#39;name&#39;: &#39;MinuteSheep&#39;, &#39;gender&#39; : &#39;male&#39;, &#39;age&#39;: 99}
Out[91]: {&#39;age&#39;: 99, &#39;gender&#39;: &#39;male&#39;, &#39;name&#39;: &#39;MinuteSheep&#39;}
Copy after login

可以看到,字典是用一对花括号括起来的,并且以 'key' : 'value' 的形式存储,同样,字典里面可以包含其他数据类型,上面的字典包括:字符串、整型。当然,字典也可以嵌套:

In [92]: {&#39;name&#39; : &#39;MinuteSheep&#39;, &#39;age&#39;: {&#39;young&#39; : 15, &#39;old&#39; : 99}}
Out[92]: {&#39;age&#39;: {&#39;old&#39;: 99, &#39;young&#39;: 15}, &#39;name&#39;: &#39;MinuteSheep&#39;}
Copy after login

字典也会有专门的一节去讲解它的方法。

二、Python数据运算

说完了Python的数据类型,就该数据运算了,养兵千日,用在一时嘛

算数运算

加 +

In [93]: 1+2
Out[93]: 3
Copy after login

减 -

In [95]: 1-2O
ut[95]: -1
Copy after login

乘 *

In [96]: 1*2
Out[96]: 2
Copy after login

除 /

In [97]: 5/2Out[97]: 2.5
Copy after login

取模 % (就是取余数)

In [98]: 5%2
Out[98]: 1
Copy after login

取整 //

In [99]: 5//2
Out[99]: 2
Copy after login

幂 **

In [100]: 5**2
Out[100]: 25
Copy after login

赋值运算

简单赋值 =

In [102]: a=5
In [103]: b=6
In [104]: c=a+b
In [105]: c
Out[105]: 11
Copy after login

加法赋值 += (b+=a,相当于b=b+a)

In [106]: a=5
In [107]: b=6
In [108]: b+=a
In [109]: b
Out[109]: 11
Copy after login

减法赋值 -= (b-=a,相当于b=b-a)

In [111]: a=5
In [112]: b=6
In [113]: b-=a
In [114]: b
Out[114]: 1
Copy after login

乘法赋值 *= (b*=a,相当于b=b*a)

In [115]: a=5
In [116]: b=6
In [117]: b*=a
In [118]: b
Out[118]: 30
Copy after login

除法赋值 /= (b/=a,相当于b=b/a)

In [119]: a=5
In [120]: b=6
In [121]: b/=a
In [122]: b
Out[122]: 1.2
Copy after login

取模赋值 %= (b%=a,相当于b=b%a)

In [123]: a=5
In [124]: b=6
In [125]: b%=a
In [126]: b
Out[126]: 1
Copy after login

取整赋值 //= (b//=a,相当于b=b//a)

In [127]: a=5
In [128]: b=6
In [129]: b//=a
In [130]: b
Out[130]: 1
Copy after login

幂赋值 **= (b**=a,相当于b=b**a)

In [131]: a=5
In [132]: b=6
In [133]: b**=a
In [134]: b
Out[134]: 7776
Copy after login

比较运算

测试相等 ==

In [136]: 1==1
Out[136]: True
In [137]: 1==2
Out[137]: False
Copy after login

不等于 !=

In [144]: 1!=1
Out[144]: False
In [145]: 1!=2
Out[145]: True
Copy after login

大于 >

In [146]: 1>1
Out[146]: False
In [147]: 2>1
Out[147]: True
Copy after login

大于等于 >=

In [149]: 1>=1
Out[149]: True
In [150]: 2>=1
Out[150]: True
Copy after login

小于 <

In [151]: 6<6
Out[151]: False
In [152]: 6<7
Out[152]: True
Copy after login

小于等于 <=

In [153]: 6<=6
Out[153]: True
In [154]: 6<=7
Out[154]: True
Copy after login

逻辑运算

布尔'与' and (有假为假,全真为真)

In [156]: True and False
Out[156]: False
In [157]: True and True
Out[157]: True
In [158]: False and False
Out[158]: False
Copy after login

布尔'或' or (有真为真,全假为假)

In [159]: True or True
Out[159]: True
In [160]: True or False
Out[160]: True
In [161]: False and False
Out[161]: False
Copy after login

布尔'非' not (取相反)

In [162]: not True
Out[162]: False
In [163]: not False
Out[163]: True
Copy after login

身份运算

判断两个标识符是否引用自同一个对象 is

In [167]: a=b=3

In [168]: a is b
Out[168]: True

In [169]: a=3

In [170]: b=5

In [171]: a is b
Out[171]: False
Copy after login

判断两个标识符是否引用自不同对象 is not

In [177]: a=3
In [178]: b=5
In [179]: a is not b
Out[179]: True
Copy after login

== 与 is 的区别:

看到这里,很多小伙伴已经晕了,== 和 is 好像一样啊,看起来一样,其实是不一样的,来看下面的这段代码:

In [180]: a = 600
In [181]: b = 600
In [182]: a == b
Out[182]: True
In [183]: a is b
Out[183]: False
Copy after login

其实啊,== 比较的是值,is比较的地址,让我们用 id() 这个函数查看一下变量的地址:

In [184]: id(a)
Out[184]: 2155434581648
In [185]: id(b)
Out[185]: 2155434581904
Copy after login

可以看到,a和b俩个变量的值虽然相同,但地址却不一样,所以使用 == 和 is 的结果自然就不一样。那我们再来看一段代码:

In [187]: a = 10
In [188]: b = 10
In [189]: a == b
Out[189]: True
In [190]: a is b
Out[190]: True
In [191]: id(a)
Out[191]: 1529742064
In [192]: id(b)
Out[192]: 1529742064
Copy after login

萌新是不是莫不着头脑了,用600的时候 is 输出False,用10的时候怎么地址一样了啊,(快回头,有鬼~)其实啊,Python有一个叫做小整数对象池的东西,只要是[-5, 256]之间的整数都会使用同一份地址,这下萌新明白了哇。

位运算

重点说明:位运算针对的是二进制数

按位与 &

In [193]: a = 15   # 15 = 0000 1111
In [194]: b = 30   # 30 = 0001 1110
In [195]: c = a&b  # 14 = 0000 1110
In [196]: c
Out[196]: 14
Copy after login

按位或 |

In [193]: a = 15   # 15 = 0000 1111
In [194]: b = 30   # 30 = 0001 1110
In [197]: c = a|b  # 31 = 0001 1111
In [198]: c
Out[198]: 31
Copy after login

按位异或 ^

In [193]: a = 15   # 15 = 0000 1111
In [194]: b = 30   # 30 = 0001 1110
In [200]: c = a^b  # 17 = 0001 0001
In [201]: c
Out[201]: 17
Copy after login

按位取反 ~

In [210]: a = 60   # 60 = 0011 1100
In [211]: c = ~a  # -61 = 1100 0011
In [212]: c
Out[212]: -61
Copy after login

左移 <<

In [213]: a = 15   # 15 = 0000 1111
In [214]: c = a<<2 # 60 = 0011 1100
In [215]: c
Out[215]: 60#左移后面要加位数
Copy after login

右移 >>

In [213]: a = 15     # 15 = 0000 1111
In [218]: c = a>>2    # 3 = 0000 0011 
In [219]: c
Out[219]: 3#右移后面要加位数
Copy after login

Python运算符优先级(从高到底依次排列)

**                 # 指数 (最高优先级)
~ + -              # 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % //           # 乘,除,取模和取整除
+ -                # 加法减法
>> <<              # 右移,左移运算符
&                  # 位 &#39;AND&#39;
^ |                # 位运算符
<= < > >=          # 比较运算符
<> == !=           # 等于运算符
= %= /= //= -= += *= **=    # 赋值运算符
is is not          # 身份运算符
in not in          # 成员运算符
not and or         # 逻辑运算符
Copy after login

The above is the detailed content of Detailed summary of Python data types and operators (code examples). 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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
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.

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.

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.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

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.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles