Key Points
Understand the common data types in Python
Understand the relevant functions of the common data types
Learn how to use Python to master logical judgment functions
Pre-Class Preparation
None.
Course Content
Processing Data
1. Data type
Type Name | English | Example | Description |
Numeric | / | Int, float, bool | / |
String | str | “Hello” , ‘Hello’ | / |
List | list | [ 1 , False , “123” ] [ 1 , [ 1 , 3 ,4 ] ] | A list, which contains any data type, is ordered, changeable, and allow duplicate values. |
Dictionary | dict | {“name”:“ziv”} {“name”: [“ziv” , “Yunlin” , “Charlie”] } | The keys of a dictionary can be integers, floats, or strings, but they must be unique. The values of a dictionary can be of any data type. Dictionaries are similar to JSON objects, but there are some differences, which will be explained in detail later. |
Tuple | tuple | (1,2,3) | Once initialized, tuples cannot be changed. Tuples are immutable objects in Python. |
Set | set | {1,2,3,5,2,2} | It is an unordered collection of unique elements. |
2. Data type conversion
list("asd") #Converted to a list a = [1,2,3] b = ["adsf","asdf","123"] dict(zip(a,b)) #Converted to a dictionary
3. String functions
Function Name | How to Use | Example | Description |
index | str.index(str, beg=0, end=len(string)) | name.index(“l”,2) | The index() method is used to check whether a string contains a substring. If beg and end parameters are specified, it checks whether the substring is within the specified range. Similar to the Python find() method, if the substring is not found within the string, the index() method will throw an exception. |
find | str.find(str, beg=0, end=len(string)) | name.find(“l”,2) | The find() method is used to check whether a string contains a substring. If beg and end parameters are specified, it checks whether the substring is within the specified range. Unlike the index() method, if the substring is not found within the string, the find() method returns -1 instead of throwing an exception. |
len | len( s ) | len(name) | The len() function in Python is used to return the length or number of items in an object, such as a string, list, tuple, and so on. |
partition | str.partition(str) | name.partition(“-”) | The partition() method is used to split a string based on a specified separator. |
replace | str.replace(old, new[, max]) | name.replace(“l”,“L”) #replace | The replace() method in Python is used to replace occurrences of a specified old string with a new string in a given string. If a third argument max is specified, the replacement will not exceed max occurrences. |
del | del s | del name | Delete objects |
split | str.split(str=“”, num=string.count(str)). | name.split(‘-’) | The split() method in Python is used to slice a string according to a specified delimiter. If the num parameter is specified, it will slice the string into num+1 substrings at most. |
name ="little-five" name[1] #Slice name[0:-2] #Slice name.index("l",2) #Index name.find("l",2) #Index len(name) #Length del name #Delete name.partition("-") #Split name.replace("l","L") #Replace name.split('-')
4. List functions
Function Name | How to Use | Example | Description |
append | list.append(obj) | name.append(“alex”) | The append() method is used to add a new object to the end of a list. |
extend | list.extend(list2) | name.extend([“alex”,“green”]) | The extend() method is used to add multiple values from another sequence to the end of a list at once, effectively extending the original list. |
insert | list.insert(index, obj) | name.insert(1,“alex”) | The insert() method is used to insert a specified object into a list at a specified position. |
pop | list.pop(obj,[index=-1]) | name.pop(1) | The pop() method is used to take out an element from a list and return its value. By default, it takes out the last element of the list. |
remove | list.remove(obj) | name.remove(“Alex”) | The remove() method is used to remove the first matching element with a specified value from a list. |
sorted | sorted(num) sorted(num,reverse=True) | sorted(iterable, key=None, reverse=False) | The sorted() function is used to sort all iterable objects. |
name = list(["little-five","James","Alex"]) name[0:-1] #Index-->Start from 0, instead of 1 name[1:] name[-1] name.append("alex") #Append, add as a whole name.extend(["alex","green"]) #Extend, add separately name.insert(1,"alex") #Insert, add by index name.pop(1) #Extract a = name.pop(1) name.remove("Alex") #Delete del name[1] sorted(num) sorted(num,reverse=True)
5. The functions of the dictionary
#Define a dictionary info ={ "name":"little-five", "age":22, "email":"99426353*@qq,com" } info.pop("name") #Extract value info["age"] #Extract value by key #Several ways to iterate for each in info: print(each) for each in info.keys(): print(each) for each in info.values(): print(each) for each in info.items(): print(each)
Note:
JSON and dictionaries in Python are very similar, but there is a difference when their values are boolean.
Bool Type | True | False | Null |
JSON | true | false | null |
Python dictionary | True | False | None (Technically, it is the NoneType) |
When copying JSON data into Python, we need to handle it manually. In the upcoming courses, we will learn how to use functions to handle this process.
Logical Function
#For "if" and "while", use boolean expressions to determine if 1==1: print(123) a = 5 if a==1: print(123) elif a==2: print(321) else: print(111) while a > 1: print(a) a += 1 if a==10: continue if a==15: break else: print("gogogo") #"for" is most widely used for i in range(0,4): print(i) else: #After a for loop finishes executing, the program will continue print(123) #"for" can be used to index list, string, and dictionary