This article is mainly to share with you a detailed description of Python basic data types, the need for small partners to see
This article is mainly to share with you a detailed description of Python basic data types, the need for small partners to see.
How Python files are run
Python's own shell
Go to the directory where the Python file is located, and then run
Python xxx.py (e.g. C:\work>python hello.py)
3.pythoncharm and Other Ides
4.sublime Text Editor with plug-in
Data type
Python data types are divided into mutable types and immutable types
Python Basic data type
Where the mutable type is
Number (numeric):
includes 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 different types of objects by assigning values.
3, the value of division (/) always returns a floating-point number, to get the integer use//operator.
4. In mixed calculations, Python converts an integer to a floating-point number.
5. A**b
6, plural a+bj or complex (A, B)
String (String):
List (lists)
Dictionary (dictionary)
Sets (collection)
A collection (set) is a sequence of unordered, non-repeating elements.
The basic function is to test the membership and remove duplicate elements.
You can use curly braces {} or set ()
function to create a collection, note: Creating an empty collection must be set () instead of {}, because {} is used to create an empty dictionary.
#!/usr/bin/python3student = {' Tom ', ' Jim ', ' Mary ', ' Tom ', ' Jack ', ' Rose '}print (student) # Output set, duplicate elements are automatically removed # member test if (' Rose ' in student): print (' Rose is in the collection ') Else: print (' Rose is not in the collection ') # set can perform set operation A = set (' Abracadabra ') b = Set (' Alaca Zam ') print (a) print (a) A and B difference set print (a | b) # A and B set print (A & B) # A and B intersection print (a ^ b) # A and b do not exist simultaneously in the meta Of
Immutable type is
Tuple (tuple)
List Contents
Constructs a tuple with 0 or 1 elements, which is more special
Tup1 = () # empty tuple tup2 = (20,) # An element that needs to be added with a comma after the element
The = = Tuple can also be spliced using the + operator. ==
The so-called "invariant" of a tuple is to say that each element of a tuple, pointing to never change
>>> t = (' A ', ' B ', [' A ', ' B ']) >>> t[2][0] = ' X ' >>> t[2][1] = ' Y ' >>> t (' A ', ' B ', [' X ', ' Y '])
= =Description of the variable of python = =
The declaration of a Python variable is a reference to an object that, for a mutable type, changes itself if his copy changes.
>>> a[1]>>> a=b=[]>>> a[]>>> b[]>>> b.append (0) >>> b[0]> >> a[0]>>>
For an immutable type, the value of the variable is not affected by the copy
>>> a=b= (1, 2, 3) >>> B (1, 2, 3) >>> + (4,) (1, 2, 3, 4) >>> B (1, 2, 3) >>> b=b+ (4,) >>> B (1, 2, 3, 4) >>> A (1, 2, 3)
Python Data type conversions
| function |
Description |
int (x [, Base]) |
Convert x to an integer |
Float (x) |
Convert x to a floating-point number |
Complex (real [, Imag]) |
Create a complex number |
STR (x) |
Convert an object x to a string |
REPR (x) |
Convert an object x to an expression string |
eval (str) |
Used to evaluate a valid Python expression in a string and return an object |
Tuple (s) |
Converting a sequence s to a tuple |
List (s) |
Convert the sequence s to a list |
Set (s) |
Convert to mutable Collection |
Dict (d) |
Create a dictionary. D must be a sequence (key,value) tuple. |
Frozenset (s) |
Convert to immutable Collection |
Chr (x) |
Converts an integer to one character |
Ord (x) |
Converts a character to its integer value |
Hex (x) |
Converts an integer to a hexadecimal string |
Oct (x) |
Converts an integer to an octal string |
Related recommendations:
Basic data types for Python
Python common data types and common operations
Python basic data types detailed description