The road to Python learning-----basic data types
basic data type
python version 3.7.7
Variables in Python do not need to be declared. However, each variable must be assigned a value before it is used , and the variable will not be created until the variable is assigned. In Python, a variable is a variable and has no type. What we mean by "type" is the type of the object in memory that the variable refers to. which is
a = 7, where a is a variable, or a label, which points to a memory space that stores the type int and the value is 7.
Python variable assignment
Python allows assigning values to multiple variables at the same time:
a = b = c = d = 100
- 1
You can also specify multiple variables for multiple objects (and the variable types can be inconsistent):
a , b , c = 1 , 5.20 , "Iloveyou"
print ( a , b , c )
1 5.2 Iloveyou
- 1
- 2
- 3
- 4
Standard data types:
Number (number), String (string), Tuple (tuple) are immutable data ;
When immutable data is passed as a function parameter in Python, it is equivalent to C/C++ value passing. When assigning a value in a function, a copy of it will be created and will not affect the original value.
List (list), Set (collection), Dictionary (dictionary) are variable data ;
When variable data is passed as a parameter in a Python function, it is equivalent to passing by reference in C++, and modification in the function will cause the actual parameter to be modified .
Number :
int (only one, long integer), float, bool, complex
You can use the type() built-in function to see the type of the object pointed to by the variable
a , b , c = 1 , 5.20 , "Iloveyou"
print ( a , b , c )
print ( type ( a ) , type ( b ) , type ( c ) )
'''
1 5.2 Iloveyou
<class 'int'> <class 'float'> <class 'str'>
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
In addition, isinstance can also be used to judge:
a , b , c = 1 , 5.20 , "Iloveyou"
print ( isinstance ( a , int ) )
'''
True
'''
- 1
- 2
- 3
- 4
- 5
- 6
The difference between isinstance and type is:
- type() does not consider a subclass to be a superclass type.
- isinstance() will consider the subclass to be a superclass type.
class A :
pass
class B ( A ) : # class B is a subclass
pass of class A
print ( isinstance ( A ( ) , A ) )
print ( isinstance ( B ( ) , A ) )
print ( type ( A ( ) ) == A )
print ( type ( B ( ) ) == A )
'''
True
True
True
false
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
Numerical operations on Number
+, -, *, / (divide, get a floating point number), // (divide, get an integer), % (remainder), ** (power)
String
Single and double quotes are used exactly the same in python .
Strings can be concatenated together with the + operator and repeated with the * operator
a = "I "
b = "Love "
c = "You"
fin = a + b + c
print ( fin )
fin = a * 3
print ( fin )
I Love You
III
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Strings in Python cannot be changed.
Python does not have a separate character type, a character is a string of length 1.
Strings in Python can be indexed in two ways (sequential and reverse), starting with 0 from left to right, and starting with -1 from right to left.

The grammatical format of string interception is as follows: variable [head subscript: tail subscript: step size] , and the intercepted length is (tag_tail - tag_head - 1).
love = "I Love You!"
print ( love [ 0 ] )
print ( love [ - 2 ] )
print ( love [ 0 : 7 ] ) #The length of the interception is tag_tail - tag_head - 1, that is, from 0 to (7- 1)
print ( love [ 0 : 10 : 2 ] )
print ( love [ - 5 : - 1 : 1 ] )
'''
I
u
I Love
ILv o
You
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
escape character\
Python uses backslashes as escape characters. If you don't want to escape, you can add r before the string to represent the native string:
love = "I \nLove \nYou!"
love_r = r "I \nLove \nYou!"
print ( love )
print ( love_r )
'''
I
Love
You!
I \nLove \nYou!
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
List (list)
Lists in python are very important and can simulate the data structure of stacks and queues.
The types of the elements in the list can be different, it supports numbers , strings and even lists (so-called nesting ).
A list is a comma-separated list of elements written between square brackets [ ] .
my_list = [ 'I' , 'L' , 'o' , 'v' , 'e' , 'Y' , 'o' , 'u' , '!' , 520 , 2.25 ]
- 1
Like strings, lists can also be indexed and truncated, and the truncated list returns a new list containing the desired elements . (note that it returns a list)
Intercept format: variable [head subscript: tail subscript]
my_list = [ 'I' , 'L' , 'o' , 'v' , 'e' , 'Y' , 'o' , 'u' , '!' , 520 , 2.25 ]
another = [ "hello world" , "hello boy" ]
print ( my_list )
print ( my_list [ 0 : :2 ] ) # step size 2 read
print ( my_list [ 3 : 7] )
print ( my_list [ - 3 : - 1 ] )
print ( my_list * 2 )
print ( my_list [ - 1 : : - 1 ] ) # reverse reading
print ( my_list + another )
'''
['I', 'L', 'o', 'v', 'e', 'Y', 'o', 'u', '!', 520, 2.25]
['I', 'o', 'e', 'o', '!', 2.25]
['v', 'e', 'Y', 'o']
['!', 520]
['I', 'L', 'o', 'v', 'e', 'Y', 'o', 'u', '!', 520, 2.25, 'I', 'L', ' o', 'v', 'e', 'Y', 'o', 'u', '!', 520, 2.25]
[2.25, 520, '!', 'u', 'o', 'Y', 'e', 'v', 'o', 'L', 'I']
['I', 'L', 'o', 'v', 'e', 'Y', 'o', 'u', '!', 520, 2.25, 'hello world', 'hello boy' ]
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Tuple
A tuple is similar to a list, except that the elements of the tuple cannot be modified . Tuples are written in parentheses () , and elements are separated by commas.
my_list = ( 'I' , 'L' , 'o' , 'v' , 'e' , 'Y' , 'o' , 'u' , '!' , 520 , 2.25 )
another = ( "hello world" , "hello boy" )
print ( my_list )
print ( my_list [ 0 : : 2 ] )
print ( my_list [ 3 :7 ] )
print ( my_list [ - 3 : - 1 ] )
print ( my_list * 2 )
print ( my_list [ - 1 : : - 1 ] )
print ( my_list + another )
'''
('I', 'L', 'o', 'v', 'e', 'Y', 'o', 'u', '!', 520, 2.25)
('I', 'o', 'e', 'o', '!', 2.25)
('v', 'e', 'Y', 'o')
('!', 520)
('I', 'L', 'o', 'v', 'e', 'Y', 'o', 'u', '!', 520, 2.25, 'I', 'L', ' o', 'v', 'e', 'Y', 'o', 'u', '!', 520, 2.25)
(2.25, 520, '!', 'u', 'o', 'Y', 'e', 'v', 'o', 'L', 'I')
('I', 'L', 'o', 'v', 'e', 'Y', 'o', 'u', '!', 520, 2.25, 'hello world', 'hello boy' )
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Although the elements of the tuple cannot be modified, it is possible to re-point the variable to a new tuple:
connect = ( 0 , )
print ( connect )
connect = my_list + another
print ( connect )
'''
(0,)
('I', 'L', 'o', 'v', 'e', 'Y', 'o', 'u', '!', 520, 2.25, 'hello world', 'hello boy' )
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
In fact, you can think of strings as a special kind of tuple .
Although the elements of a tuple are immutable, it can contain mutable objects such as lists.
Constructing tuples with 0 or 1 elements is special, so there are some additional syntax rules:
tup_empty = ( ) # empty tuple
tup_one = ( 3 , ) # ! ! ! A tuple with only one element, if removed, will point tup_one to an int type variable 3
- 1
- 2
Collection (Set)
A set is composed of one or more large and small wholes of different shapes, and the things or objects that constitute a set are called elements or members. The set is unordered and non-repeating, and there will be only one repeated element.
Use curly braces { } or the set() function to create sets. Note: To create an empty set , you must use set() instead of { } , because { } is used to create an empty dictionary .
empty_d = { } #Create an empty dictionary
empty_s = set ( ) #Create an empty set
- 1
- 2
Collection test and operation:
student = { 'Tom' , 'Jim' , 'Mary' , 'Tom' , 'Jack' , 'Rose' }
print ( student ) # Output the collection, duplicate elements are automatically removed
# Membership test
if 'Rose' in student :
print ( 'Rose is in the set' )
else :
print ( 'Rose is not in the set' )
# set can perform set operations
a = set ( 'abracadabra' )
b = set ( 'alacazam' )
print ( a )
print ( b )
print ( a - b ) # difference of a and b
print ( a | b ) # union of a and b
print ( a & b ) # intersection of a and b
print ( a ^ b ) # elements in a and b that do not exist at the same time
'''
{'Jim', 'Rose', 'Jack', 'Tom', 'Mary'}
Rose is in the collection
{'r', 'd', 'b', 'a', 'c'}
{'l', 'm', 'z', 'a', 'c'}
{'b', 'r', 'd'}
{'r', 'd', 'b', 'a', 'c', 'z', 'l', 'm'}
{'a', 'c'}
{'z', 'r', 'l', 'm', 'd', 'b'}
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
Dictionary (dictionary)
A list is an ordered collection of objects, and a dictionary is an unordered collection of objects.
The difference between the two is that the elements in the dictionary are accessed by key , not by offset.
A dictionary is a type of mapping. A dictionary is identified by { } , which is an unordered set of keys : values
Among them, the key (key) must use immutable types: string, tuple, number
dict = { }
dict [ 'one' ] = "1 - rookie tutorial"
dict [ 2 ] = "2 - rookie tools"
tinydict = { 'name' : 'runoob' , 'code' : 1 , 'site' : 'www.runoob.com' }
print ( dict )
print ( dict [ 'one' ] ) # prints the value with key 'one'
print ( dict [ 2 ] ) # prints the value with key 2
print ( tinydict ) # prints the full dictionary
print ( tinydict . keys ( ) ) # print all keys
print ( tinydict . values ( ) ) # print all values
'''
{'one': '1 - Rookie Tutorial', 2: '2 - Rookie Tools'}
1 - Tutorial for rookies
2 - Rookie Tools
{'name': 'runoob', 'code': 1, 'site': 'www.runoob.com'}
dict_keys(['name', 'code', 'site'])
dict_values(['runoob', 1, 'www.runoob.com'])
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
The constructor dict() can build a dictionary directly from a sequence of key-value pairs as follows:
print ( dict ( [ ( 'Runoob' , 1 ) , ( 'Google' , 2 ) , ( 'Taobao' , 3 ) ] ) )
print ( { x : x ** 2 for x in ( 2 , 4 , 5 ) } )
print ( dict ( a = 1 , b = 2 , c= 3 ) )
'''
{'Runoob': 1, 'Google': 2, 'Taobao': 3}
{2: 4, 4: 16, 5: 25}
{'a': 1, 'b': 2, 'c': 3}
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9