Python Some standard types are defined , Used to store various types of data .
Python There are currently six standard data types :
Category one :
These six data types can be subdivided into immutable data types and variable data types
Immutable data (3 individual ):Number( Numbers )、String( character string )、Tuple( Tuples )
Variable data (3 individual ):List( list )、Dictionary( Dictionaries )、Set( aggregate )
Category two :
These six data types can be subdivided into 4 Number types and 5 Two sequence types , common 9 Number types .
4 The two types of numbers are : integer (int), floating-point (float), The plural (complex), Boolean value (bool)
5 The two sequence types are : character string (string), list (list), Tuples (tuple), Dictionaries (dict), aggregate (set)
【 notes 】 When we need to store a set of data , You need to use the sequence type , The sequence assigns an index to each element , The first is 0, The second is 1, Discuss and deduce in turn .
Python3 Four different types of numbers are supported :
int( Long integer )
float( floating-point )
complex( The plural )
bool( Boolean type )
【 Be careful 】 stay Python 3 in , There is only one integer type int, Expressed as a long integer , No, python2 Medium Long. stay Python2 There is no Boolean in , It uses numbers 0 Express False, use 1 Express True. To Python3 in , hold True and False Defined as a keyword , But their value is still 1 and 0, They can be added to numbers .
# Numerical type
>>> n=100 # Define a variable
#type: View data types of variables
>>> print(type(n))
Output :<class ‘int’>
>>> pi=3.14
>>>print(type(pi))
Output :<class ‘float’>
Data in double or single quotes , It's just a string .
The single character is in Python Is also used as a string .
# Character
string1="Python"
print(type(string1))
Output :<class ‘str’>
# Multiline string
string3='''Python baidu '''
print(string3)
print(type(string3))
Output :
Python
baidu
<class ‘str’>
Create a list , Just use different data items separated by commas square brackets [ ]
Just round it up .
The data items of a list do not need to have the same type .
list1 = ["qing", "ping", 27]
list2 = [1, 2, 3, 4]
list3 = ["a", "b", "c"]
Tuples are similar to lists , But the element values of tuples cannot be modified or deleted , It can be sliced and connected .
Tuples are easy to create , Enclosed in parentheses , Separated by commas . Tuples use braces , The list uses brackets .
Tuple = () # Creating a tuple
Tuple = ("a","b","c","d")
Tuple = ([1,2,3],"a",3,)
Be careful 1: Tuple of an element , Add a... At the back “,”
Tuple =(2022,)
Be careful 2: Arbitrary with “,” Separate sequences , Default is tuple .
>>> a = 1,2,3,4
>>> a
(1, 2, 3, 4)
>>> a =[1,2,3,4],5,"str"
>>> a
([1, 2, 3, 4], 5, 'str')
Be careful 3:Python Tuples tuple() Function to convert a list to a tuple .
Dictionaries are stored in key value pairs , Elements can be key visit . Dictionaries are written by Curly braces { }
Enclosed contains ,key : value Two parts .
dict = {
'name':' Monitor of the class ', 'id':100, 'sex':'f', 'address':' Earth Asia China Beijing '}
print(dict["name"],dict["sex"])
A collection is an unordered and indexed collection . stay Python in , Sets are written in curly braces .
Be careful : have access to Curly braces { }
perhaps set() Function to create a collection , But to create an empty collection, you must use set() instead of { }, because { } Is used to create an empty dictionary ,
s1= {
"apple", "banana", "cherry"}
print(s1)