Five basic Python data types
Learning a language often starts with Hello World. However, the author believes that there is nothing extraordinary about outputting "Hello, World" in a black box. To see through the essence of things and become familiar with a language, you must understand its underlying layer, which is what we often say. This article starts with variable types in python.
Five standard data types
The data stored in memory can be of many types.
For example, a person's name can be stored using characters, age can be stored using numbers, hobbies can be stored using sets, etc.
Python has five standard data types:
Numbers (numbers)
String (string)
List (list)
Tuple (element Group)
Dictionary
The data types belonging to the collection type are Lists, tuples and dictionaries.
1. Numbers
Number data type is used to store numerical values.
They are immutable data types, which means that changing the numeric data type will allocate a new object.
When you specify a value, a Number object is created:
var1 = 1 var2 = 2
The del statement deletes references to some objects. The syntax is:
del var1[,var2[,var3[....,varN]]]]
By using the del statement References to single or multiple objects can be deleted. For example:
del var1 del var1, var2
Four different number types:
int (signed integer type)
-
long (long integer type [can also represent octal and hexadecimal])
float (floating point type)
complex (plural)
a. int (integer)
On a 32-bit machine, the number of digits in the integer is 32 bits. The value range is -2**31~2**31-1, that is, -2147483648~2147483647
On a 64-bit system, the number of digits in the integer is 64, and the value range is -2**63~2* *63-1, that is, -9223372036854775808~9223372036854775807
b. long (long integer)
Unlike C language, Python’s long integer does not specify the bit width, that is: Python There is no limit to the size of long integer values, but in fact due to limited machine memory, long integer values cannot be infinitely large.
Note that since Python 2.2, if an integer overflow occurs, Python will automatically convert the integer data to a long integer, so now not adding the letter L after the long integer data will not cause serious consequences.
c. float (floating point type)
Floating point numbers are used to process real numbers, that is, numbers with decimals. Similar to the double type in C language, it occupies 8 bytes (64 bits), of which 52 bits represent the base, 11 bits represent the exponent, and the remaining bit represents the symbol.
d. complex (plural number)
A complex number consists of a real part and an imaginary part. The general form is x+yj, where x is the real part of the complex number and y is the imaginary part of the complex number. Here x and y are both real numbers.
Note: There is a small number pool in Python: -5 ~ 257
Small integer object - small integer object pool
In actual programming , relatively small integers, such as 1, 2, 29, etc., may appear very frequently. In python, all objects exist on the system heap. Think about it? If a small integer appears very often, Python will have a large number of malloc/free operations, which greatly reduces operating efficiency and causes a large amount of memory fragmentation, seriously affecting the overall performance of Python.
In Python 2.5 and even 3.3, small integers between [-5,257) are cached in the small integer object pool.
2. String
String or String It is a string of characters composed of numbers, letters, and underscores.
It is a data type that represents text in programming languages.
Python's string list has two value orders:
The index starts from left to right by default 0, and the maximum range is 1 less than the string length
The right-to-left index starts from -1 by default, and the maximum range is the beginning of the string
If you want to get a section from the string For strings, you can use the variable [head subscript:tail subscript] to intercept the corresponding string. The subscript starts from 0 and can be a positive or negative number. The subscript can Empty means getting to the beginning or end.
For example: the result of
s = 'i love python'
s[2:6] is love. (Consider the head but not the tail, or close the left and open the right)
Operation example:
str = 'Hello World' print(str) #输出完整字符串 print(str[0]) #输出字符串中的第一个字符 print(str[2:5]) #输出字符串中第三个至第五个之间的字符 print(str[2:]) #输出从第三个开始到最后的字符串 print(str*2) #输出字符串两次 print('say: ' + str) #输出连接的字符串
3. List (List)
List is the most frequently used data type in Python.
列表可以完成大多数集合类的数据结构实现。List里面的数据类型也可以不同,它支持字符,数字,字符串甚至可以包含列表(所谓嵌套)。
操作实例:
list = ['apple', 'jack', 798, 2.22, 36] otherlist = [123, 'xiaohong'] print(list) #输出完整列表 print(list[0]) #输出列表第一个元素 print(list[1:3]) #输出列表第二个至第三个元素 print(list[2:]) #输出列表第三个开始至末尾的所有元素 print(otherlist * 2) #输出列表两次 print(list + otherlist) #输出拼接列表
4. 元组(Tuple)
元组用"()"标识。
内部元素用逗号隔开。但是元组一旦初始化,就不能修改,相当于只读列表。
只有1个元素的tuple定义时必须加一个逗号 ,
,来消除歧义(否则会认为t只是一个数):
>>> t = (1,)>>> t (1,)
操作实例与列表相似
5. 字典(Dictionary)
字典(dictionary)是除列表以外Python之中最灵活的内置数据结构类型。列表是有序的对象结合,字典是无序的对象集合。
两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。
字典用"{ }"标识。字典由索引(key)和它对应的值value组成。
操作实例:
dict = {} dict['one'] = 'This is one' dict[2] = 'This is two' tinydict = {'name':'john','code':5762,'dept':'sales'} print(dict['one']) #输出键为'one'的值 print(dict[2]) #输出键为2的值 print(tinydict) #输出完整的字典 print(tinydict.keys()) #输出所有键 print(tinydict.values()) #输出所有值
The above is the detailed content of Five basic Python data types. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

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.

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.

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.

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.

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".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
