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!