Previous article: The contents of the Python tutorial (Chapter 4) are itemized
Python3 Engineer Certification Basic Exam As a countermeasure, this is a personal memo that summarizes the contents of the Python tutorial (book) in easy-to-memorize bullet points.
Python Tutorial: https://docs.python.org/ja/3/tutorial/ Chapter 5: https://docs.python.org/ja/3/tutorial/datastructures.html Books: https://www.oreilly.co.jp/books/9784873117539/
--Python3 Engineer Certification Basic Exam Score ―― 7/40 questions (17.5%) ☆☆☆☆ ★ (Importance: Medium +) --Theme --List --Tuple --Set --Dictionary --Evaluation by comparison operator and Boolean operator --Comparison of sequences and other objects
--The following are all the methods of the list object.
Method name | Description |
---|---|
list.append(x) | Add an item with the value x to the end. |
list.extend(L) | Add all items in list L to the end. |
list.insert(i, x) | Add an item with the value x at the position of index i. |
list.remove(x) | Delete the item with the value x and the lowest index. Error if the item does not exist. |
list.pop([i]) | Deletes and returns the item at index i. The index can be omitted, and if omitted, the last item will be deleted / returned. |
list.clear() | Delete all items. |
list.index(x) | Returns the minimum index of the item whose value is x. Error if the item does not exist. |
list.count(x) | Returns the number of items with a value of x. |
list.sort(key=None, reverse=False) | Sort items in place. |
list.reverse() | Reverse the items in place. |
list.copy() | Returns a shallow copy of the list. |
--The stack is last-in, first-out. --Use append (x) to push it onto the stack. --Use pop () to get from the stack. --It is fast because it is an operation at the end of the list.
--Queues are first-in, first-out. --Use insert (0, x) to queue. --Use pop () to get from the queue. --Since it is the operation at the top of the list, it is slow. --Cue implementation should use collections rather than lists.
-** List comprehension ** consists of an expression followed by a for clause, followed by 0 or more for clauses and if clauses, and the whole is enclosed in []. -* squares = * * [x \ * \ * 2 for x in range (10)] * # 0 \ * \ * 0 ~ 9 \ * \ * 9 list comprehension --List comprehensions can contain compound expressions and nested functions. --Common usage of list comprehension --Processing each member of a sequence or repeatable body to generate a new list --Create a subsequence by extracting the elements that match the conditions
--Any expression can be used for the first expression in the list comprehension. --Nested list comprehension (put another list comprehension in the first expression) is also possible.
-You can use the ** del statement ** to index items in the list.
Variables can also be deleted.
-* del a [0] * # Delete item with index 0
-* del a [2: 4] * # Deleted items with indexes 2 and 3
-* del a [:] * # Delete all items
-* del a * # Delete variable a
--Unlike pop (), it does not return deleted items.
--remove () removes the first item whose argument and value match.
-** Tuples ** are a type of sequence, consisting of comma-separated values. -Index and slice operations are possible as well as strings and lists. --Defining tuples is called tuple packing. -* t = (12345, 54321,'hello!') * # () Can be omitted -* empty = () * # Tuple with 0 items -* singleton ='hello', * # Tuple with 1 item (comma at the end is the point) --When you output a tuple, it is enclosed in (). --Print (t) # (12345, 54321,'hello!') --Assigning tuple items to individual variables is called ** sequence unpacking . - x, y, z = t * # Variable x is assigned 12345, y is assigned 54321, and z is assigned'hello!'. --Match the number of variables and the number of tuple items. --Multiple assignment ** can be done using unpacking. - x, y, z = 1, 2, 3 * # Substitute the tuple (1, 2, 3) value for each tuple (x, y, z) item --Differences between tuples and lists --It is an immutable body (immmutable). --Items can include modifiable objects such as lists. --Each item is generally accessed by unpacking or indexing. --Items of different data types are often mixed.
-** Set ** is a collection of elements that do not overlap in any order. --Python has a ** set object **, which is created using the {} and ** set () functions **. --Use set () to generate the empty set. --You can write ** set comprehension ** as well as list comprehension. - a = {x for x in 'abracadabra' if x not in 'abc'} --Supports mathematical operations such as sum, intersection, difference, and symmetric difference.
Set arithmetic | Description |
---|---|
a in x [Existence judgment] |
True if the set a has an element x(true) If not False(false) |
a | b [sum] |
A set of elements at a or b |
a - b [difference] |
A set of elements that are in a but not in b |
a & b [Crossing] |
A set of elements in both a and b |
a ^ b [Symmetric difference] |
A set of dissimilar elements in a or b |
-** Dictionary ** is an unsorted collection of "key: value" pairs. - tel = {'jack': 4098, 'sape': 4139} -* print (tel ['jack']) * Returns # 4098 -* print (tel) * # {'jack': 4098,'sape': 4139} is returned -* empty = {} * # empty dictionary --Keys cannot be duplicated, and any immutable body can be used. --String --Numerical value --Tuples (do not include mutable objects) --Specify a key for addition, deletion, and existence confirmation to the dictionary. -* tel ['guido'] = 4127 * # tel will be added with'guido': 4127 -* del tel ['jack'] * # tel removes'jack': 4098 -'sape' in tel * # Existence: True -'test' in tel * # Existence: False -** You can get the iterable of the key list with the keys () method . - keylist = list (tel.keys ()) * # Get key list in list format - keysort = sorted (tel.keys ()) * # Get the key list in sorted list format --By the way, although it is not in the tutorial, you can get a list of values with the ** values () method . - dict () Constructor ** can be used to create a dictionary from tuples. -* tel = dict ([('jack', 4098), ('sape', 4139)]) * # tel = {'jack': 4098,'sape': 4139} --Like lists and tuples, there are also ** dictionary comprehensions *. - dic = {x: x \ ** 2 for x in (2, 4, 6)} * # dic = {2: 4, 4: 16, 6: 36} Same as
--The dictionary's ** items () method ** can be used to get a tuple list (repeatable) of (key, value). -** You can get a tuple list (repeatable body) of (index, value) by passing a sequence to the enumerate () function . - If you pass two or more sequences to the zip () function **, you can get a tuple list (repeatable body) of (sequence 1 value, sequence 2 value ...). -You can get a list of items (repeated bodies) in reverse index order by passing a sequence to the ** reversed () function **. --The contents of the original sequence are not rewritten. --Note that unlike sort () below, the list is not returned. -You can get a new sorted list of items by passing a sequence to the ** sort () function **. --The contents of the original sequence are not rewritten.
--There are ** numeric operator **, ** comparison operator **, and ** Boolean operator ** as operators used to judge conditions. --The condition judgment result can be denied by ** not **.
operator | Description |
---|---|
Comparison operator | ==, !=, <, <=, >=, >There are other than |
a in x | True if a is included in sequence x(true) |
a not in x | True if a is not included in sequence x(true) |
a is x | True if a and b are the same instance(true) |
a is not x | True if a and b are not the same instance(true) |
Boolean operator | Alias, short circuit operator |
A and B | Both A and B are True(true)Then True(true) |
A or B | A or B is True(true)Then True(true) |
not A | A is True(true)Then False(false) A is False(false)Then True(true) |
--The priority of operators is numeric operator> comparison operator> not> and> or You can control the priority by enclosing it in (). --Comparison operators can be ** chained *. - if a <b == c: * # (a <b) and (b == c) --The Boolean operator evaluates from right to left and ends when the evaluation is decided. - A and B and C --If A and B are False, and C is not evaluated - A or B or C --If A or B is True, or C is not evaluated --If a general value is used instead of a Boolean value, the return value of the short circuit operator will be the last evaluated argument. --The result of a Boolean expression such as comparison can be assigned to a variable. --You cannot assign in an expression.
--The size comparison of sequence type objects is evaluated by the following steps.
Next article: The contents of the Python tutorial (Chapter 6) are itemized
Recommended Posts