Pthon Programming From Beginning to Practice Reading Practice Notes

1 Start

1.1 Build environment

1.1.1 Python Version Selection

Recommended above 3.6, python2 encoding is very confusing

1.1.2 Installation Package Download

It is recommended that you download it directly from the official website. For example, the latest version of 2021/11/27 is 3.10.0. https://www.python.org/downloads/release/python-3100/ Windows installer (64-bit) link under Web page
In particular, there are many platforms for managing python and its dependent libraries, such as anaconda, but for beginners, there is no need to go one step at a time, and many times a stable official version is sufficient for most situations.

1.1.3 Some related software that may be used

  • Sublime Text
    A text editor that executes programs for beginners
  • PyCharm Community Edition
    No need for a professional edition, the community can handle most of the tasks

1.2 Installing environments on different operating systems

1.2.1 windows

  1. Check to see if python is installed on the system
    Open cmd and enter python
  2. Install Sublime Text

1.2.2 macOS

1.2.3 Linux

1.3 Run the helloworld program

Mainly using sublime text, which is lightweight, but seems to compile a little slower

2 Variables and simple data types

2.1 Run

2.2 Variables

Naming and use of 2.2.1 variables

2.3 String

2.3.1 str method to change case

2.3.2 String Energy

There is another format, f'{a} {b} {c}', equivalent to'{} {}'. format(a, b, c) is equivalent to'%d%d'% (a, b, c)

2.3.3 Tab

2.3.4 Delete blanks

str.lstrip()
str.rstrip()
str.strip()

2.4 Number

x = 2 ** 5 # The power is **
x = 1000_0000  # Numbers can be grouped by underlining, which is equivalent to x = 10000000
x, y, z = 0, 0, 0  #You can assign values to multiple variables at the same time, for example

2.5 python Zen

3 List

What is the 3.1 List

3.1.1 Access list elements

3.1.2 Index starts at 0, -1 is the first in reverse order

3.1.3 Use each value in the list

3.2 Additions and deletions of list elements

3.2.1 Modification

list[idx] = new_value

3.2.2 Increase

list.append(value)  # Add a value at the end
list.insert(idx, value) In Index idx Increase everywhere value,new value The index of is idx,primary>=idx Of value,idx++
list.expend(list)  # Add List

3.2.3 Delete

del list[idx]
last_value = list.pop()  # Usually used to take and delete the last element
idx_value = list.pop(idx)  # Take and delete elements indexed by idx
list.remove(value)  # Delete the element of a specific value, note that this only deletes the first element of that value

3.3 Organization List

3.3.1 Permanent Sort

list.sort(reverse=False)  # list will be permanently sorted

3.3.2 Temporary Sort

list_sorted = sorted(list)

3.3.3 Reverse Order

list.reverse()

3.3.4 List Length

len(list)

4 Action List

4.1 Traversal and for Loop

for item in item_list:
    print(item)

4.2 Indentation

4.3 Create a list of values

4.3.1 range

for i in range(1,6):  # The right side of the range is <, not <=, and in this case does not contain 6
    print(i)

4.3.2 Generating list s with range s

start = 1
end = 11
step = 2
lst = list(range(start, end, step))  # Equivalent to for (i=start; i<end; i+=step)
print(lst)

4.3.3 List Statistics

lst = [0, 1, 2, 3, 5, 5, 5, 9, 9]
max_num = max(lst)  # Maximum value in list
min_num = min(lst)  # Minimum value in list
sum_num = sum(lst)  # List Sum
value_count = lst.count(5)  # Number 5 appears in the list, returning 3
value_idx = lst.index(5)  # The first occurrence of the number 5 in the list, returning 4
value_count = lst.count(7)  # Number 7 in the list, returns 0
value_idx = lst.index(7)  # Error in the first occurrence of number 7 in the list

4.4 Use part of the list

4.4.1 slices

stars = ['yangchaoyue', 'liuyifei', 'tongliya', 'zhouxingchi', 'wujing']
print(stars[0:3])  #Same IDX = 0; IDX < 3; Idx++.
print(stars[:3])  #Same IDX = 0; IDX < 3; Idx++.
print(stars[2:]) #Same IDX = 2; IDX < len(lst); Idx++.
print(stars[-2:]) #The last two, idx=len(lst) -2; Idx<len(lst); Idx++.

4.4.2 Traversing slices

4.4.3 Replication List

stars = ['yangchaoyue', 'liuyifei', 'tongliya', 'zhouxingchi', 'wujing']
stars_ptr = stars # Quote
stars_copy = stars[:] # copy
stars.append('shenteng')  # The contents of stars have been modified
print(stars)  
print(stars_ptr) # Quote
print(stars_copy) # copy

4.5 tuples

Unwritable List

dimensions = (720, 480, 3)
print(dimensions)
print(dimensions[2])
for dim in dimensions:
    print(dim)

4.6 Format Code

PIP8 Guide
Easy to Read > Easy to Write
Indents are replaced by spaces
Line length less than 80 characters, comment line less than 72 characters

Posted by Billett on Wed, 01 Dec 2021 12:04:47 -0800