Python data types:
1. String
2. Boolean type
3. Integer
4. Floating point number
5. Numbers
6. List
7. Tuples
8. Dictionary
9. Date
1. String
str = "char" --> This is a set of strings
print(str) --> Screen output string
You can also use single quotes:
str = ‘char‘
print(str) --> The output result and meaning are the same
Using three quotes to represent multiple lines of strings, you can use single quotes and double quotes freely in triple quotes, for example:
str=‘‘‘this is string
this is pythod string
this is string‘‘‘
print(str)
2. Boolean type
Boolean type: false, true
bool = false
print(bool)
bool = true
print(bool)
3. Integer
int = 20
print(int)
4. Floating point numbers
float = 3.14
print(float)
5. Numbers
Numbers contain integers and floating point numbers.
6. List
list = [1,2,3,"char"]
nums = [1,2,3,4,5,8,9,11]
Access list value:
print(list) --> outputs the entire list element
print(list[2]) -->The third element of the output list, the element subscript starts from 0 [0, 1, 2...and so on]
print(list[0:3]) -->Output starts from the element with the subscript of the list to the element with the subscript of the subscript of the list
print(list[:3])-->Output starts from the element with the subscript of the list to the element with the subscript of the subscript of the list 0 to the element with the subscript of the subscript of the list 3
print(list[1:])-->Output starts from the element with the subscript of the list to the last element with the last element under the list
print(list[:-2])--> Cut from the first element to the second last element, but does not contain the third last element
print(list[:])-->Output elements of the entire list
Change the value of the list:
list[x] = y --> x represents the subscript value of the element that needs to be changed, and y represents the value that needs to be reassigned. For example, list[0] = 10, which means changing the 1 of the list to 10
Delete list elements:
del list[x] --> x represents the subscript value of the element that needs to be deleted. For example, del list[0] means deleting the value whose subscript list is 0
Built-in functions for lists:
(obj) Add new object at the end of the list
(obj) counts the number of times an element appears in the list
(seq) Append multiple values from another sequence at one time at the end of the list (extend the original list with the new list)
(obj) Find the index position of the first match of a certain value from the list, and the index starts from 0
(index, obj) Insert objects into list
(obj=list[-1]) Removes an element in the list (the last element by default) and returns the value of that element
(obj) Removes the first match of a value in the list
() Reverse the elements in the list, reverse
([func]) Sort the original list
7. Tuples
Python tuples are similar to lists, the difference is that the elements of tuples cannot be modified; tuples use brackets (), and lists use square brackets []; tuple creation is very simple, you only need to add elements in brackets and separate them with commas (,), for example:
tup_1 = (1,2,3,‘char‘)
tup_2 = (1,2,3,4,5,10,100)
tup_null = () --> This is to create an empty list
When there is only one element in the tuple, you need to add a comma after the element, for example: tup1 = (50,);
Tuples are similar to strings. The subscript index starts from 0 and can be intercepted, combined, etc.
Access the value of a tuple:
print(tup_1)--> Output the elements of the entire tuple
print(tup_1[1]-->The first element of the output tuple, the element subscript starts from 0 [0, 1, 2... and so on]
print(tup_1[0:3]) -->Output starts from an element with a subscript of 0 to an element with a subscript of 3
print(tup_1[:3])-->Output starts from an element with a subscript of 0 to an element with a subscript of 3
print(tup_1[1:])-->Output starts from the element with the subscript of tuple 1 to the last element
print(tup_1[:-2])--> Cut from the first element to the second end element, but does not contain the third end element
print(tup_1[:])-->Output elements of the entire tuple
Modify the value of the tuple:
The element values in tuples are not allowed to be modified, but we can join and combine tuples, for example:
tup_1 = (1,2,3,‘char‘)
tup_2 = (1,2,3,4,5,10,100)
tup_3 = tup_1 + tup_2
Then output tup_3 --> (1,2,3,'char',1,2,3,4,5,10,100)
Delete values in tuples:
Element values in tuples are not allowed to be deleted. You can use the del statement to delete the entire tuple, for example:
tup_1 = (1,2,3,‘char‘)
del tup_1
Other operations of tuples:
print(len(tup_1)) --> Get the length of the tuple
print(tup_1 + tup_2) --> Connection output of tuples
print(tup_1 * 4) --> Elements in tuples are output in 3 times
print(3 in tup_1) --> Indicates whether 3 exists in a tuple, returns a boolean value (false(not exists)|true(exist))
for i in tup_1:
print(i) --> generate an iteration, outputting the elements in the tuple in sequence
Tuple built-in functions
cmp(tuple1, tuple2) Compare two tuple elements.
len(tuple) calculates the number of tuple elements.
max(tuple) Returns the maximum value of the element in the tuple.
min(tuple) Returns the minimum value of the element in the tuple.
tuple(seq) Convert lists to tuples
8. Dictionary
Dictionary is the most flexible built-in data structure type in python except lists.
Lists are ordered combinations of objects, and dictionaries are unordered collections of objects. The difference between the two is that elements in the dictionary are accessed through keys, not through offsets.
A dictionary consists of keys and corresponding values. A dictionary is also called an associative array or hash table. The basic syntax is as follows:
dict = {‘xian‘: ‘1234‘, ‘wei‘: ‘5678‘, ‘qing‘: ‘x001‘};
It is also possible to create a dictionary like this:
dict1 = { ‘abc‘: 456 };
dict2 = { ‘abc‘: 123, 98.6: 37 };
Each key and value must be separated by a colon (:), each pair is separated by a comma, and the whole is placed in curly braces ({}). Keys must be unique, but values do not; values can take any data type, but must be immutable, such as strings, numbers, or tuples.
Access the value of the dictionary:
print(dict["xian"]) --> The value of the key value pair with the key "xian" in the dictionary is accessed
Modify the value of the dictionary:
dict['xian'] = 1024 --> Modify the value of an existing key
dict['xian01'] = 3344 --> Add a new key-value pair
Delete the value of the dictionary:
del dict['xian01'] --> Delete the key-value pair with the key of 'xian01' in the dictionary
--> Clear the dictionary
The built-in functions of the dictionary:
cmp(dict1, dict2) Compare two dictionary elements.
len(dict) calculates the number of dictionary elements, that is, the total number of keys.
str(dict) Outputs a dictionary printable string representation.
type(variable) Returns the input variable type, and if the variable is a dictionary, it returns the dictionary type.
() Delete all elements in the dictionary
() Returns a shallow copy of a dictionary
() Create a new dictionary, use elements in the sequence seq as the keys of the dictionary, and val is the initial value corresponding to all keys of the dictionary
(key, default=None) Returns the value of the specified key, if the value is not in the dictionary, return the default value
radiansdict.has_key(key) If the key returns true in the dictionary dict, otherwise return false
() Returns a traversable array of (keys, values) tuples as a list
() Returns all keys in a dictionary as a list
(key, default=None) is similar to get(), but if the key does not already exist in the dictionary, the key will be added and the value will be set to default
(dict2) Update the key/value pairs of dictionary dict2 to dict
() Return all values in the dictionary as a list