April 19, 2019
1. Tuples
- Tuples are defined with (), arrays are defined with [], and once the definition is complete, tuples cannot be modified. Tuples can hold different types of data types / feel a little like structures in C++.
- Define empty tuples
empty_typle = ()
- Tuples/definitions containing only one element need to be comma
empty_typle = (5) # It is of type int empty_typle = (5,) # It is a tuple tuple type
- Common operations for tuples
info_tuple = ("zhangsan", "zhangsan", 18, 175) # 1. Value and Index print(info_tuple[0]) print(info_tuple.index("zhangsan")) # 2. Statistical Count print(info_tuple.count("zhangsan")) # 3. Number of statistical tuples print(len(info_tuple))
# 4. Traversal of tuples info_tuple = ("zhangsan", 18, 1.75) for my_info in info_tuple: print(my_info)
- Scenarios:
(1) Parameters and return values of functions
(2) Format string
infor = ("Xiao Ming", 18, 175) #This is the tuple print("%s Age is:%d Height is %.2f" % infor)
(3) Keep the list unmodified to ensure its security
- Conversion of tuples and lists
list = typle[. . . ]
tuple = list(. . . )
2. Dictionaries
- The difference between a list and a dictionary is that a list is an ordered data collection and a dictionary is an unordered data collection.(out of order: don't care about the data saved), it feels like a structure.Keys must be unique when using a dictionary
- Definition of dictionary:
# 1. Definition of dictionary variables xiaoming = {"name": "Xiao Ming", # Dictionary out of order, not concerned with output order "age": 18, "genfer": True, "height": 1.75, "weignt": 75.5}
- Dictionary Operation
# 2. Value print("The user's name is:%s" % xiaoming["name"]) # 3. Add, delete xiaoming["school"] = "CUMT" # k does not add, it modifies if it exists # 4. Delete xiaoming.pop("name") # Delete name # 5. Statistics print(len(xiaoming)) # 6. Merge Dictionaries asd = {"add": "papa"} xiaoming.update(asd) # 7. Empty Dictionary xiaoming.clear()
- Traversal of dictionaries
# 8. Loop through the dictionary, and the variable k is the key value each time it gets for k in xiaoming: print("%s -- %s " % (k, xiaoming[k]))
- Scenario for dictionaries and lists: Use dictionaries to define information about objects, placing individual objects in a list one by one
card_list = [ {"name": "zhangsan,", "qq": "123456,", "phone": "18252115105" }, {"name": "lisi", "qq": "1666364106", "phone": 18795387502} ] for card_info in card_list: print(card_info)
Run Results
E:\pro\05_Advanced Data Type\venv\Scriptspython.exe E:/pro/05_Advanced Data Type/hm_11_List Dictionary Application Scenario.py {'name': 'zhangsan,', 'qq': '123456,', 'phone': '18252115105'} {'name': 'lisi', 'qq': '1666364106', 'phone': 18795387502}
3. ==String==
- Define the string, note the use of single and double quotation marks
# 1. Definition of strings str1 = "heelo python" str2 = 'My nickname is"Big watermelon"'
- 0. Basic operations for string operations
# 2.len() statistical length print(len(str1)) # 3. Count occurrences of small strings print(str1.count("llo")) # 4. Determine where the substring appears print(str1.index("llo"))
- 1. Type of judgment for string operations
# 5.isspace() returns TRUE if it determines a blank character, etc. (tabs, whitespace is a blank character) print(space_str.isspace()) # 6. Determine if the string contains only numbers print(str1.isdecimal()) print(str1.isdigit()) print(str1.isnumeric())
- 2. Finding and Replacing String Operations
# 7. Find the specified string print(str1.startswith("hello")) # Whether to start with the specified string print(str1.endswith("hello")) # Whether to end at the specified location print(str1.find("llo")) # Note the difference between index and index # 8. Replace print(str1.replace("hello", "python")) # replace forms a new string without changing the original string print(str1)
- 3. The text of a string operation is against it
# 9. String text vs. poem = ["Yellow Crane Tower", "Wang Huanzhi", "Day by Day", "Yellow River inflow", "in order to see far away", "Move Up One Level"] for poem_str in poem: print("|%s|" % poem_str.center(10, " "))
- 4. String operations to remove white space characters
# 10. Remove whitespace characters print(poem_str.strip())
- 5. Split and connect
poem = "Yellow Crane Tower, Wang Huanzhi, Day by Day\t ,Yellow River inflow, in order to see far away\n,Move Up One Level" print(poem) # 11. Split String poem_list = poem.split() print(poem_list) print(poem) # 12. Merge strings use "" as separator result = " ".join(poem_list) print(result)
- 6. ==Slice of string==
# 13. String's slice python provides a backward index, followed by -1, the value between the range of the slice index num = "0123456789" print(num[2:6]) print(num[2:]) print(num[:6]) print(num[:]) print(num[::2]) print(num[1::2]) print(num[-1]) print(num[2:-1]) print(num[::-1]) # Get Reverse Order print(num[::-2])
4. Common methods: Common methods for advanced data types
function | describe |
---|---|
len(item) | Calculate the number of elements in a container |
del(item) | Delete variable |
max(item) | Return maximum |
min(item) | Return Minimum |
cmp(item1,item2) | Comparing sizes, there is no cmp in python3 |