01 string
Strings are the most commonly used data type in Python. We can use quotation marks ('or') to create strings.
Creating strings is simple, just assign a value to a variable. For example:
value1 = 'hello' value2 = "Python"
Python does not support single character types, which are also used as a string in Python.
Python accesses substrings, using square brackets to intercept strings, as shown in the following example:
value1 = 'hello' value2 = "Python" print(value1[0]) print(value2[1:4])
Now let's introduce some ways to better understand strings.
string = "hello python" # Capitalize the first character of a string print(string.capitalize()) # Returns the number of times substrings appear in string. # If you specify beg or end, the number of times str occurs within the specified range is returned print(string.count('o')) # Determine whether the string ends with str. # If you specify beg or end, you determine whether the specified range ends with str. print(string.endswith('on')) # Determine whether str is included in string, if it is included, return the index value of the starting position, otherwise return - 1 print(string.find('py')) print(string.find('wow')) # format string print(string.format()) # Similar to find method, but if str is not in string, an exception will be reported # print(string.index('wow')) # Combine all elements in seq into a new string using string as separator # print(string.join(seq)) # Replace str1 in string with str2, and if num is specified, no more than num times will be replaced. print(string.replace('ello','i')) # Slice string with str as delimiter. If num is specified, only num + substrings are separated print(string.split(" ")) # Remove the blank symbols on both ends of string print(string.strip())
Note: All of the above returns copies of the original string.
02 List - List
Lists are the most basic data type in Python and can appear as comma-separated values in square brackets.
Each element in the list is assigned a number - its location, or index, the first index is 0, the second index is 1, and so on.
List operations include indexing, slicing, adding, multiplying, and checking members.
Create a list by enclosing comma-separated data items in square brackets. As follows:
list1 = ['a','b','c','d'] list2 = [1,2,3,4,5,6,7,8,9] //Some basic operations on lists: list1 = ['a','b','c','d'] list2 = [1,2,3,4,5,6,7,8,9] # Value of access list print(list1[0]) print(str(list2[1:5])) # Additional values in the list list1.append('Chrome') print(list1) list2.append(10) print(list2) # Delete list elements del list2[5] print(list2) list1.remove('Chrome') print(list1) # Elements in the reverse list list1.reverse() print(list1) # Sort lists list2.sort() print(list2)
03 tuple
Python tuples are similar to lists, except that elements of tuples cannot be modified.
Tuples are bracketed and lists are bracketed.
tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) //Some basic operations on tuples: # Modify tuples tup1 = (12, 34.56) tup2 = ('abc', 'xyz') # The following modification of tuple element operations is illegal. # tup1[0] = 100 # Create a new tuple tup3 = tup1 + tup2 print tup3 # Deleting element values in tuples is not allowed, but del can be used to delete the entire tuple. tup = ('physics', 'chemistry', 1997, 2000) del tup print tup
04 sets
The set in Python is consistent with the set in mathematics. No repetitive elements are allowed, and intersection, Union and difference sets can be performed.
set1 = {1, 2, 3, 3, 3, 2} print(set1) print('Length =', len(set1)) set2 = set(range(1, 10)) print(set2) set1.add(4) set1.add(5) set2.update([11, 12]) print(set1) print(set2) set2.discard(5) # The absence of remote elements causes KeyError if 4 in set2: set2.remove(4) print(set2) # Traversing Collection Containers for elem in set2: print(elem ** 2, end=' ') print() # Converting tuples into collections set3 = set((1, 2, 3, 3, 2, 1)) print(set3.pop()) print(set3) # Intersection, union, difference and symmetric difference of sets print(set1 & set2) # print(set1.intersection(set2)) print(set1 | set2) # print(set1.union(set2)) print(set1 - set2) # print(set1.difference(set2)) print(set1 ^ set2) # print(set1.symmetric_difference(set2)) # Judgment subset and superset print(set2 <= set1) # print(set2.issubset(set1)) print(set3 <= set1) # print(set3.issubset(set1)) print(set1 >= set2) # print(set1.issuperset(set2)) print(set1 >= set3) # print(set1.issuperset(set3))
05 dictionary
Dictionaries are another variable container model and can store arbitrary types of objects.
Each key => value pair of a dictionary is coloned: split, comma between each key pair, split, and the entire dictionary is included in brackets {}, in the following format:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
The following code demonstrates how to define and use dictionaries.
scores = {'Xiao Bai': 95, 'Bai Yuanfang': 78, 'Di Renjie': 82} # The corresponding values in the dictionary can be obtained by keys print(scores['Xiao Bai']) print(scores['Di Renjie']) # Traverse the dictionary (traversing the keys and then getting the corresponding values through the keys) for elem in scores: print('%s\t--->\t%d' % (elem, scores[elem])) # Update the elements in the dictionary scores['Bai Yuanfang'] = 65 scores['Zhuge Wang Lang'] = 71 scores.update(Cold noodles=67, Fang Qihe=85) print(scores) if 'Wu Zetian' in scores: print(scores['Wu Zetian']) print(scores.get('Wu Zetian')) # The get method also gets the corresponding value through the key, but it can set the default value. print(scores.get('Wu Zetian', 60)) # Delete elements from a dictionary print(scores.popitem()) print(scores.popitem()) print(scores.pop('Xiao Bai', 100)) # Empty the dictionary scores.clear() print(scores)
06 daily quiz
1. Fifteen Christians and fifteen non-Christians were in distress at sea. In order to survive some people, they had to throw fifteen of them into the sea. One person thought of a way to do this:
Everyone gathered in a circle. Someone started counting from 1 to 9 and threw them into the sea. The people behind him counted from 1 to 9, and the people who reported to 9 continued to throw them into the sea until 15 people were thrown away. Thanks to God's blessing, 15 Christians survived.
Ask them how they first stood, where they were Christians and where they were non-Christians.