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!
Python and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AMTo maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.
Python: Games, GUIs, and MoreApr 13, 2025 am 12:14 AMPython excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.
Python vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AMPython is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.
The 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AMYou can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.
Python: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AMPython is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.
How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PMYou can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.
How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AMHow to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...
How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AMHow to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.






