How do high school students learn python with zero basic knowledge? Can high school students learn python by themselves?

This article mainly introduces whether it is easy to find a job after learning Python with a high school degree. It has certain reference value. Friends in need can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to understand it together.

 

The ninth day of learning python

Based on our study in the past few days, we have mastered Python's data types, statements and functions, and can basically write many useful programs using the PYTHON library "IMITATION" .

However, as we learn step by step, more code is definitely not better. We need to learn some advanced features to help us simplify the code. Never write three lines to solve a problem that can be solved in one line. The simpler, the better!

slice

For example, we want to take some elements of a list or tuple. A list is as follows:

>>> names = ['Mike','Bob','Alice','LIHUA','XIAOMING']

What do we do if we take the first three elements?

>>> names[1],names[2],names[3]
'Mike','Bob','Alice'

But this is a stupid method. If we take the first n elements, there is no way. If we use our brains, we will know that we can use a loop.


>>> r = []
>>> n = 3
>>> for i in range(n):
...     r.append(names[i])
... 
>>> r
['Mike', 'Bob', 'Alice']

Take the first N elements, that is, the elements with index 0-(N-1)

For this kind of operation that often takes a specified index range, it is very cumbersome to use loops. Therefore, Python provides the slice (Slice) operator, which can greatly simplify this operation.

Corresponding to the above problem, take the first three elements and complete the slicing with one line of code:

>>> names[0:3]
['Mike', 'Bob', 'Alice']

names[0:3]Indicates that the index 0is taken starting from the index until the index 3, but not including the index 3. That is, the indexes 0, 1, 2, are exactly 3 elements.

It can also be omitted if the first element is 0:

>>> names[:3]
['Mike', 'Bob', 'Alice']

Of course, we can take any number, let's first create a sequence from 0-100:

>>> N = list(range(101))
>>> N
[0, 1, 2, 3, ..., 100]

At this time, we can easily take out any sequence of numbers through slicing, such as taking the first 10 digits:

>>> N[0:10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

For the first 10 numbers, take one from every two:

>>> N[:10:2]
[0, 2, 4, 6, 8]

All numbers, take one out of every five:

>>> N[::5]
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]

Take the last 5:

>>> N[-5:]
[96, 97, 98, 99, 100]

Even without writing anything, just write [:] to copy a list as it is:

>>> N[:]
[0, 1, 2, 3, ..., 99]

Tuple is also a kind of list, the only difference is that tuple is immutable. Therefore, tuples can also be operated by slicing, but the result of the operation is still a tuple:


>>> (0, 1, 2, 3, 4, 5)[1:3]
(1, 2, 3)

A string 'xxx'can also be viewed as a list, with each element being a character. Therefore, strings can also be sliced, but the result is still a string:

>>> 'ABCDEFG'[:3]
'ABC'
>>> 'ABCDEFG'[::2]
'ACEG'

In many programming languages, various interception functions (for example, substring) are provided for strings. The actual purpose is to slice strings. Python does not have an interception function for strings. It only requires one operation to slice, which is very simple.

iteration

If a list or tuple is given, we can fortraverse the list or tuple through a loop. This traversal is called iteration.

In Python, iteration is for ... indone through subscripts, while in many languages ​​such as C, iteration over lists is done through subscripts, such as Java code:

for (i=0; i<list.length; i++) {
    n = list[i];
}

It can be seen that forthe level of abstraction of Python's loops is higher than that of C's forloops, because Python's forloops can not only be used on lists or tuples, but also on other iterable objects.

Although the data type list has subscripts, many other data types do not have subscripts. However, as long as it is an iterable object, it can be iterated regardless of whether it has a subscript. For example, dict can be iterated:

>>> d = {'a': 1, 'b': 2, 'c': 3}
>>> for key in d:
...     print(key)
...
a
c
b

Because dict is not stored in a list order, the order of iterated results is likely to be different.

By default, dict iterates over keys. If you want to iterate value, you can use it for value in d.values(). If you want to iterate key and value at the same time, you can use it for k, v in d.items().

Since strings are also iterable objects, they can also be used in forloops:


>>> for xq in 'ABC':
...     print(xq)
...
A
B
C

Therefore, when we use fora loop, as long as it acts on an iterable object, forthe loop can run normally, and we don't care whether the object is a list or another data type.

So, how to determine whether an object is an iterable object? The method is to judge by the Iterable type of the collections module:

>>> from collections import Iterable
>>> isinstance('ABC', Iterable) # str是否可迭代
True
>>> isinstance([1,2,3], Iterable) # list是否可迭代
True
>>> isinstance(123, Iterable) # 整数是否可迭代
False

The last little question, what if you want to implement a subscript loop similar to Java on the list? Python's built-in enumeratefunction can turn a list into an index-element pair, so that both forthe index and the element itself can be iterated in a loop:

>>> for i, value in enumerate(['A', 'B', 'C']):
...     print(i, value)
...
0 A
1 B
2 C

In the above forloop, two variables are referenced at the same time, which is very common in Python, such as the following code:

>>> for x, y in [(1, 1), (3, 4), (5, 6)]:
...     print(x, y)
...
1 1
3 4
5 6

Come on, good night!

Guess you like

Origin blog.csdn.net/i_like_cpp/article/details/132142814