KANIKIG

KANIKIG

just for fun | 兴趣使然 Ph.D. in Engineering|❤️ #NFT $ETH| [Twitter](https://twitter.com/kanikig2)|[Github](https://github.com/KANIKIG)|[Telegram channel](https://t.me/kanikigtech)

Python Full Stack Learning Notes

image

Course Introduction#

Stage One: Basic to Advanced#

Three Projects

  • ATM + Shopping Cart: Procedural
  • Course Selection System: Object-Oriented
  • Computer Virus: Program, Server, Client

Stage Two: Commercial Projects#

  • BBS
  • Luffy Learning City
  • WeChat Mini Programs
  • Web Crawlers
  • Data Analysis: Financial Quantitative Trading
  • Automated Operations: CMDB, Code Deployment
  • GO Language Development
  • Artificial Intelligence Direction

Computer#

Five Major Components of a Computer#

dcc451da81cb39dbf8fed5fad5160924ab18305b.jpg

  • CPU Central Processing Unit
    • Controller: Controls all other components
    • Arithmetic Logic Unit: Mathematical and logical operations
  • Memory I/O Devices: Data Access
    • RAM: Based on electricity, loses data when powered off, used for temporary access
    • External Storage: Hard Disk, based on magnetism, slow access, permanent storage
  • Input Devices: Keyboard, Mouse
  • Output Devices: Monitor, Printer

Operating System#

Concept#

  • Controls computer hardware
  • Encapsulates complex operations of hardware

Software

  • Application Software
  • System Software

Three-Tier Structure of Computer Architecture#

  • Application Programs
  • Operating System
  • Computer Hardware

Platform and Cross-Platform#

Platform:

  • Operating System
  • Computer Hardware

Others#

Programs and Three Core Hardware#

Program: Hard Disk -> Memory

CPU reads instructions from memory to execute

CPU Detailed Explanation#

CPU Classification and Instruction Set#
  • X86-32bit: Intel
  • x86-64bit: AMD

Instruction Set, is a collection of instructions used by the CPU to compute and control the computer system.

  • Reduced Instruction Set Computer (RISC): Short instructions, stable
  • Complex Instruction Set Computer (CISC): Long instructions, rich
X86-64#
  • x86: Intel invented the world's first CPU 8086, hence this architecture is collectively referred to as x86.

  • 64-bit: The number of bits of binary instructions that the CPU can fetch from memory at once.

The CPU has downward compatibility.

Registers#

Made of the same material as the CPU, faster than memory. Stores critical data needed by the CPU, enhancing the speed of data retrieval.

Kernel Mode and User Mode#

Two types of programs

  • Operating System: Kernel mode, calls the instruction set for controlling hardware and arithmetic instruction set
  • Application Programs: User mode, only calls arithmetic-related instruction sets

Thus, frequent switching between the two states.

Multithreading and Multi-Core Chips#

Moore's Law.

Single-core dual-threading, meaning one CPU performs the work of two CPUs, pseudo-parallelism, false dual-core.

4 cores 8 threads: Each CPU has 2 threads.

  • Intel: All cores share one L2 cache
  • AMD: Each core is allocated a separate L2 cache
  • Register L1: 32-bit 32x32, 64-bit 64x64
  • Cache L2: CPU first looks in the cache, if cache hit, if not, then looks in memory
  • Memory
  • Disk
  • Tape

Speed from fast to slow

RAM#

Random Access Memory

ROM#

Read-Only Memory, speed similar to memory. Generally used for storing critical factory programs, such as BIOS.

CMOS#

Also volatile. Slow speed. Extremely low power consumption. The motherboard has a battery that powers the clock chip, storing calculations in CMOS.

Disk Structure#

v2-dc762f4e4037b261d0134171213c94a0_1440w.jpg

Hard Disk:

  • Mechanical Hard Disk, i.e., disk, relies on mechanical rotation.
    • Tracks: A circle of data bits (binary) - Byte - kB, in fact, hard disk manufacturers count by 1000.
    • Sector: 512 bytes, the smallest unit of read/write for the hard disk. The operating system reads one block at a time, i.e., 8 sectors = 4096 bytes.
    • Cylinder: Tracks of the same radius stacked together form a virtual cylinder.
    • Partition: The section between two cylinders.
  • Solid State Drive
I/O Latency#

The read/write speed of the hard disk is fast, but the slow part is the time taken to find data.

  • Average Seek Time
  • Average Latency Time: The minimum is the time for the disk to rotate half a turn.

I/O latency is the sum of the above two.

The core method to optimize programs is to reduce read/write from the hard disk, trying to read/write from memory as much as possible.

Virtual Memory Swap#

When physical memory is insufficient, it is allocated on the disk. This will introduce I/O latency.

I/O Devices#

Includes

  • Device Control: Driver
  • The device itself

Bus#

Connects various components on the motherboard for interaction.

t01696027505b77ec8c.jpg

  • PCI Bridge: North Bridge, connects high-speed devices
  • ISA Bridge: South Bridge, connects slow devices

Operating System Boot Process#

BIOS: Basic Input Output System, written into ROM devices at the factory.

Boot Process:

  1. Power on the computer
  2. BIOS runs to check hardware is functioning normally
  3. BIOS reads parameters from the CMOS memory, selects the boot device
  4. Reads the content of the first sector from the boot device (MBR Master Boot Record 512 bytes, first 446 bytes for boot information, last 64 bytes for partition information, last two are flag bits)
  5. Based on partition information, loads the BootLoader startup module to start the operating system
  6. The operating system queries BIOS for configuration information and loads drivers.

Introduction to Python#

Introduction to Programming Languages#

  1. Machine Language
  2. Assembly Language
  3. High-Level Language
    1. Compiled: c -> gcc compiler -> machine language, high execution efficiency
    2. Interpreted: py -> bytecode -> interpreter (line by line) -> machine, strong cross-platform capability

Introduction to Python#

  • Interpreted
  • Syntax Style: PEP8 Specification

Interpreter#

Interpreters can be written in any language, CPython, Jpython.

2.6-2008

2.7-2010 Transition version released later

3.0-2008

3.1-2009

3.2-2011

Two Ways to Run Python Programs#

  • Interactive
  • Script

Three Steps to Run a Program#

  1. Start the interpreter
  2. The interpreter reads the .py file into memory
  3. The interpreter interprets and executes

Variables and Basic Data Types#

Variables#

Three Major Components#

Variable Name Assignment Operator Variable Value

Variable Name#

Recommended to use lowercase with underscores

Three Characteristics of Variable Values#

  • id: Memory address of the variable value
id()
  • type
type()
  • value

Mutable and Immutable Types#

  • Mutable Types: Change value, memory address id remains unchanged
    • set
    • list
    • dict
  • Immutable: Change value, id also changes
    • number
    • bool
    • string
    • tuple

is and ==#

  • is compares whether the memory addresses of the two variables are the same
  • == compares the values of the variables

Small Integer Object Pool#

When the interpreter starts, it pre-allocates a series of memory spaces to store commonly used integers (-5, 256)

The small integer pool in the IDE will be larger.

Constants#

Python syntax does not have the concept of constants!

All uppercase represents a constant, which is just a convention, it is still a variable in reality.

Basic Data Types#

Strings#

Single, double, and triple quotes can all be used for definition; when nested, the single and double quotes should be opposite or escaped.

print('-' * 10)
# -----------

Lists#

Memory stores the memory addresses of values, not the values themselves!

If list2 = list1, then both point to the same heap, changing the value will change both.

Shallow and Deep Copy:

  • Shallow Copy: Copies the first layer of memory addresses of the list, but if there are mutable types, they will still be linked.
list2 = list1.copy()
  • Deep Copy: Completely independent copy, immutable types' id remains unchanged, mutable types' id changes.
import copy
list3 = copy.deepcopy(list1)

Dictionaries#

a = {
  "key": "value",
  "1": 1,
  "2": "qwe"
}

Garbage Collection Mechanism (GC)#

Reference Counting#

Garbage: Variable value not bound to a variable name.

Reference Counting: The number of variables bound to a certain value, Python will clear values with a count of 0.

del x  # Unbinds the variable from the value

Direct reference, indirect reference.

Mark and Sweep#

Circular reference issues may lead to memory leaks.

The memory stack area stores variable names, and the heap area stores values.

Generational Collection#

Variables that have not been collected after multiple scans will be considered commonly used variables, and their scanning frequency will be reduced.

User Interaction#

Input#

input() # Stored as str

# python2
raw_input()  # Stored as str
input()  # Requires the user to input a specific data type; the input type is the type of the variable.

Formatted Output#

  • % Formatted Output
print("name: %(name)s, age:%(age)d" % {"name": "a", "age": 10})
# %s can accept any type
  • str.format
print("name: {name}, age:{age}".format(name="a", age=10))
  • f
print(f"name: {name}, age:{age}")

Basic Operators#

Arithmetic Operators#

10 // 3  # Only keeps the integer part, i.e., integer division
3 ** 10  # Exponentiation
10 % 3  # Modulus

Comparison Operators#

1 != 2
1 < x < 3

Assignment Operators#

  • Variable Assignment
a = 1
  • Increment Assignment
a += 1
b *= 1
  • Chained Assignment
z = y = x = 10 
  • Cross Assignment
m, n = n, m
  • Unpacking Assignment
list = [1, 2, 3, 4]
a, b, c, d = list
x, y, *_ = list  # Take the first two
*_ , x, y = list  # Take the last two
# Dictionary unpacking takes values by key

Logical Operators#

Implicit Boolean: All values, except 0, None, False, Null, empty values are True.

Priority not > and > or

Short-Circuit Evaluation: Reads conditions from left to right; if one condition in a series of and is false, it stops reading further and returns that value.

Membership Operator in#

"a" in "abc"
1 in [1, 2, 3]

Identity Operator is#

Control Flow#

if#

if 16 < a < 20:

elif conditions are checked from top to bottom; the next condition is checked only if the previous one is not satisfied.

while#

Conditional Loop

while + else#

If the loop ends normally without being broken, run else.

while True:
	...
else:
	...

for#

Iterative Loop

for variable in Iterable object:
	...
  

for i in range(1, 100):
	...
  

for + else#

range#

# py2
>>> range(0, 5)
[0, 1, 2, 3, 4]
# py3 optimized
>>> range(0, 5)
range(0, 5)

print#

print("hello", end="*")  # Custom end, default is newline

Basic Data Types and Built-in Methods#

Numeric Types#

int#

Python 3 no longer has long integer type.

int('10')
bin(11) # 0b1011  0b indicates binary
oct(11) # 0o13  octal
hex(11) # 0xb  hexadecimal
int(int('0b1011', 2))  # Convert binary to decimal

float#

float('11')

Complex Numbers#

x = 10 + 2j
x.real  # 10
x.imag  # 2

Strings#

# Immutable, is a whole, cannot change a single character
str(10)
msg = "hello"

# Slicing
msg[0]  # h
msg[-1]  # o
msg[0:5]  # hello (includes start, excludes end)
msg[0:5:2]  # hlo
msg[5:0:-1]  # olle

"alex" in "alexxxx"  # true
"alex" not in "alexxxx"  # false

msg = "    eee   "
res = msg.strip()  # Default removes spaces from both sides; strings are immutable, so need to reassign
msg = "***eee***"
res = msg.strip("*")  # strip only removes from both ends, not in the middle
res = msg.lstrip()
res = msg.rstrip()

# Splitting: Splits the string by a character, returns a list
res = msg.split(":", 1)  # Defaults to split by space, split count is counted from left to right
res = msg.lsplit(":", 1)
res = msg.rsplit(":", 1)  # From right to left

res2 = ":".join(res)  # Joins with ':' as the separator

res = msg.lower()  # Lowercase
res = msg.upper()
res = msg.startswith("aaa")  # True, checks what it starts with
res = msg.endswith("aaa")
res = msg.replace("you", "me", 1)  # String replacement, number of occurrences

"123".isdigit()  # Pure numbers

msg.find("e")  # Returns index, returns -1 if not found
msg.index("e")  # Returns index, raises an error if not found
msg.count("e")  # Counts occurrences of substring
"Divider".center(50, "*")  # Fills with * on both sides
"Divider".ljust(50, "*")  # Fills on the right
"Divider".rjust(50, "*")
"Divider".zfill(50)  # Fills with 0 on the left

# is checking series, refer to documentation
isalpha()
isdigit()
isdecimal()  # Only recognizes Arabic numerals, Python 3 defaults to Unicode
isnumeric()  # Can recognize Chinese characters, Arabic numerals

Lists#

Tuples#

Dictionaries#

Sets#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.