What Xiaoting is sharing with you today is Python3 Basic data type .
Python3 Basic data type
Python Variables in do not need to be declared . Each variable must be assigned a value before use , The variable will not be created until it is assigned a value . Variable has no type , What we say " type " Is the type of object in memory that the variable refers to .
Equal sign = Is used to assign values to variables . Equal sign = On the left is a variable name , Equal sign = On the right is the value stored in the variable . for example :
counter = 111 # Integer variables miles = 1111.11 # Floating point variables name = "python" # character string print(counter) #Python3 result :111 print(miles) #Python3 result :1111.11 print(name) #Python3 result :python
a = b = c = 1 # It means that three variables are allocated to the same memory space .
You can also specify multiple objects . for example :
A,B,C = 3,2,'python' # Two integer objects 3 and 2 Assigned to variables A and B, String object ’python' Assign to a variable C.
print(a,b,c) Python3 result :1 1 1 print(A,B,C) Python3 result :3 2 python
Support int、float、bool、complex( The plural ); a, b, c, d = 1, 1.2, True, 1+3j print(type(a), type(b), type(c), type(d)) Python3 result :<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>
Numerical operation :
3 + 5 # Add ,Python3 result :8;
3 - 5 # Subtraction ,Python3 result :-2;
3 * 5 # Multiplication ,Python3 result :15;
3 ** 5 # chengfang ,Python3 result :243
3 / 7 # division , Get a floating point number ,Python3 result :0.42857142857142855;
6 / 3 # Division of values (/) Always return a floating point number , To get integer usage // The operator .Python3 result :2.0;
3 // 7 # division , Get an integer ,Python3 result :0;
3 % 5 # Remainder ,Python3 result : 3;
3+2j+2+2j # Plural operation ,Python3 result :(5+4j);
Python3 Use single quotes for strings in (') Or double quotes (") Cover up ;
The backslash \ or Three double quotes """...""" or Three single quotes '''...''' It can be used as a continuation , Indicates that the next line is a continuation of the previous line , It can span multiple lines .
print(" I love python") Python3 result : I love python print(' I love python') Python3 result : I love python a='wo ai python,life is \ short ,we neeed python ' print(a) Python3 result :wo ai python,life is short ,we neeed python
The syntax format of string truncation is as follows :
Variable [ Header subscript : Tail subscript ], Include header subscript , Does not include tail subscript
Index value to 0 For starting value ,-1 To start at the end .
Python There are two ways to index strings in , From left to right 0 Start , From right to left -1 Start .
Python String in cannot be changed .
plus (+) Is the concatenator of a string , asterisk (*) Means to copy the current string , The number that follows is the number of copies . Examples are as follows :
str = 'python'
print(str) # Output string
Python3 result :python
print(str[0:-1]) # Output all characters from the first to the last
Python3 result :pytho
print(str[0]) # The first character of the output string
Python3 result :p
print(str[2:5]) # Output characters from the third to the fifth
Python3 result :tho
print(str[2:]) # Output all characters after the third one
Python3 result :thon
print(str * 2) # Output string twice
Python3 result :pythonpython
print(str + ' is good!') # Connection string
Python3 result :python is good!
List The list is Python The most frequently used data type in .
List can complete the data structure implementation of most collection classes .
The types of elements in a list can vary , It supports Numbers , Strings can even contain lists ( The so-called nested ).
The list is written in square brackets [] Between 、 Comma separated list of elements .
Just like a string , The list can also be indexed and intercepted , After the list is truncated, a new list containing the required elements is returned .
Syntax format of list interception : Variable [ Header subscript : Tail subscript ]
Index value to 0 For starting value ,-1 To start at the end .
plus (+) Is the list join operator , asterisk (*) Is a repeat operation . The following example :
list1 = [ 'abcd', 123 , 1.23, 'python', [4,5,6,7,8] ] list2 = [321, 'life'] print (list1) # Output complete list , Python3 result :['abcd', 123, 1.23, 'python', [4, 5, 6, 7, 8]] print (list1[0]) # The first element of the output list , Python3 result :abcd print (list1[1:3]) # Output from the second to the third element , Python3 result :[123, 1.23] print (list1[2:]) # Output all elements starting with the third element , Python3 result :[1.23, 'python', [4, 5, 6, 7, 8]] print (list2 * 2) # Output twice list2 list , Python3 result :[321, 'life', 321, 'life'] print (list1 + list2) # Connection list , Python3 result :['abcd', 123, 1.23, 'python', [4, 5, 6, 7, 8], 321, 'life']
Tuples (tuple) Like a list , The difference is that the elements of a tuple cannot be modified .
Tuples are written in parentheses (()) in , The elements are separated by commas .
The element types in tuples can also be different :
tuple1 = ('abc', 123, 1.23, 'python', 456) tuple2 = (789, 'lucky') print(tuple1) # Output full tuples Python3 result :('abc', 123, 1.23, 'python', 456) print(tuple1[0]) # The first element of the output tuple Python3 result :abc print(tuple1[1:3]) # The output starts with the second element and goes to the third element Python3 result :(123, 1.23) print(tuple1[2:]) # Output all elements starting with the third element Python3 result :(1.23, 'python', 456) print(tuple2 * 2) # Output tuples twice Python3 result :(789, 'lucky', 789, 'lucky') print(tuple1 + tuple2) # Join tuples Python3 result :('abc', 123, 1.23, 'python', 456, 789, 'lucky')
aggregate (set) Is a sequence of elements that are not repeated in disorder .
The basic function is to test membership and remove duplicate elements .
You can use braces { } perhaps set() Function to create a collection .
Be careful : To create an empty collection, you must use the set() instead of { }, because { } Is used to create an empty dictionary .
Create format : parame = {value01,value02,...} perhaps set(value)
student = {'Tom', 'Lili', 'Mary', 'xenia','Tom', 'Jack', 'Rose'} print(student) # Output set , Duplicate elements are automatically removed Python3 result :{'xenia', 'Mary', 'Rose', 'Lili', 'Join', 'Tom', 'Jack'} # set You can do set operations a = set('abcdefgh') b = set('abcdhtml') print(a) Python3 result :{'b', 'h', 'f', 'd', 'e', 'a', 'c', 'g'} print(a - b) # a and b The difference between the set {'e', 'f', 'g'} Python3 result :{'e', 'f', 'g'} print(a | b) # a and b Union Python3 result :{'m', 'b', 'h', 't', 'f', 'd', 'e', 'a', 'l', 'c', 'g'} print(a & b) # a and b Intersection Python3 result :{'b', 'h', 'd', 'c', 'a'} print(a ^ b) # a and b Elements that do not exist at the same time in Python3 result :{'m', 't', 'f', 'e', 'l', 'g'}
Dictionaries (dictionary) yes Python Another very useful built-in data type in .
A list is an ordered collection of objects , A dictionary is an unordered collection of objects .
The difference between the two is : The elements in the dictionary are accessed by keys , Instead of accessing by offset .
A dictionary is a mapping type , Dictionary use "{ }" identification , It's a disordered bond (key) : value (value) The collection .
key (key) Immutable type must be used .
Dictionary keywords must be immutable , And can't repeat .
Create an empty dictionary to use { }.
In the same dictionary , key (key) Must be unique .
dict1 = {'man': 'Tom' ,'age' :18, 'sport': 'run', 666:'car'} print (dict1['man']) # The output key is 'name' Value Python3 result :Tom print (dict1[666]) # The output key is 666 Value Python3 result :car print (dict1) # Output complete dictionary Python3 result :{'man': 'Tom', 'age': 18, 'sport': 'run', 666: 'car'} print (dict1.keys()) # Output all keys Python3 result :dict_keys(['man', 'age', 'sport', 666]) print (dict1.values()) # Output all values Python3 result :dict_values(['Tom', 18, 'run', 'car'] Constructors dict() You can build a dictionary directly from the sequence of key value pairs as follows : print(dict([('python', 1), ('xenia', 2), ('Taobaolife', 3)])) Python3 result :{'python': 1, 'xenia': 2, 'Taobaolife': 3}
Welcome to xiaotinger's blog :https://blog.csdn.net/u010986753
DB Written interview history link
http://mp.weixin.qq.com/s/Vm5PqNcDcITkOr9cQg6T7w
● The author of this article : Xiaoting'er
● Author's blog address :https://blog.csdn.net/u010986753
● copyright , Welcome to share this article , Reprint please keep the source