This article mainly shares with you a detailed explanation of the basic data types of Python. Friends who need it can take a look at
Python’s own shell
Go to the directory where the python file is located, Then run
python xxx.py (such as C:\work>python hello.py)
3.pythoncharm and other IDE
4.sublime Text etc. Plug-in editor
Python data types are divided into variable types and immutable types
Python basic data types
The variable type is
Number (number):
Including int, float, bool, complex (plural).
Note:
1. Python can assign values to multiple variables at the same time, such as a, b = 1, 2.
2. A variable can point to objects of different types through assignment.
3. The division of numerical values (/) always returns a floating point number. To obtain an integer, use the // operator.
4. During mixed calculation, Python will convert integers into floating point numbers.
5. Power a**b
6. Complex number a+bj or complex(a,b)
String (string):
List (list)
Dictionary (dictionary)
Sets (set)
A set (set) is an unordered sequence of non-repeating elements.
The basic function is to perform membership testing and remove duplicate elements.
You can use curly brackets { } or the set()
function to create a set. Note: To create an empty set, you must use set() instead of { }, because { } is used to create an empty dictionary.
#!/usr/bin/python3student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'} print(student) # 输出集合,重复的元素被自动去掉# 成员测试if('Rose' in student) : print('Rose 在集合中')else : print('Rose 不在集合中')# set可以进行集合运算a = set('abracadabra') b = set('alacazam') print(a) print(a - b) # a和b的差集print(a | b) # a和b的并集print(a & b) # a和b的交集print(a ^ b) # a和b中不同时存在的元素
The immutable type is
Tuple (tuple)
List content
Constructing a tuple containing 0 or 1 elements is special
tup1 = () # 空元组tup2 = (20,) # 一个元素,需要在元素后添加逗号
==Tuples can also be spliced using the + operator. ==
>>> t = ('a', 'b', ['A', 'B']) >>> t[2][0] = 'X' >>> t[2][1] = 'Y' >>> t ('a', 'b', ['X', 'Y'])
==python Description of variables==
The declaration of a python variable is a reference to an object. For a variable type, if its copy changes, it itself will also change
>>> a [1]>>> a=b=[]>>> a []>>> b []>>> b.append(0)>>> b [0]>>> a [0]>>>
For immutable types Type, its variable value will not be affected by the copy
>>> a=b=(1,2,3)>>> a (1, 2, 3)>>> b (1, 2, 3)>>> b+(4,) (1, 2, 3, 4)>>> b (1, 2, 3)>>> b=b+(4,)>>> b (1, 2, 3, 4)>>> a (1, 2, 3)
Function | Description |
---|---|
int(x [,base]) |
Convert x to an integer |
float(x) |
Convert x to a floating point number |
complex(real [ ,imag]) |
Create a plural number |
##str(x) | Convert object x to string |
repr(x) | Convert object x to expression The formula string |
eval(str) | is used to evaluate the valid Python expression in the string, And return an object |
Convert sequence s into a tuple | |
Convert sequence s into a list | |
Convert to a variable set | |
Create a dictionary. d must be a sequence of (key, value) tuples. | |
Convert to immutable collection | |
Convert an integer to a character | |
Convert a character to its integer value | |
Convert an integer to a hexadecimal string | |
Convert an integer to an octal string |
The above is the detailed content of Detailed explanation of python's basic data types. For more information, please follow other related articles on the PHP Chinese website!