Python Series Learning Chapter 1 - Basic Programming

Hello, everyone, I am Token_w, and the following is my recent main direction of attack. I will update and explain the learning python and
start to update from the most basic learning python, from entry to proficiency, and interspersed with various practical cases in the middle.
Python is A very good tool-like language, easy to learn, I hope it will help
everyone ! !

1. Features of Python language

insert image description here

2. Programming language

2.1. Overview of programming languages

Programming language is an interactive system in which the computer can understand and recognize the user's operation intention. It organizes computer instructions according to specific rules, so that the computer can automatically perform various calculations.
A set of computer instructions organized according to the rules of a programming language is called a computer program.
High-level programming languages ​​can be divided into two categories according to different execution mechanisms: static language and scripting language. Static language is executed by compilation, and scripting language is executed by interpretation. For example, the C language is a static language, and the Python language is a scripting language.
The programming language that is compiled and executed is a static language, such as C language and Java language; the programming language that is interpreted and executed is a scripting language, such as JavaScript language and PHP language.

2.2, compilation and interpretation

Compilation is the process of converting source code into object code. Usually, the source code is high-level language code, and the object code is machine language code. The computer program that performs compilation is called a compiler.
insert image description here
Interpretation is the process of converting source code into object code one by one and running the object code one by one. A computer program that performs interpretation is called an interpreter.
insert image description here
Compilation is a one-time translation, once the program is compiled, there is no need to compile the program or the source code.

  • Compilation produces object code that executes faster for the same source code.
  • The object code can run without a compiler, and it can be used flexibly on the same type of operating system.
    Interpretation requires an interpreter and source code each time the program runs.
  • Interpretation and execution need to retain the source code, which is very convenient for program error correction and maintenance.
  • As long as there is an interpreter, the source code can run on any operating system, and the portability is good.

2.3. Computer programming

Computational thinking is the third mode of thinking that is different from logical thinking represented by mathematics and empirical thinking represented by physics.

Programming is a process of solving problems

  • First of all, it is necessary to analyze the problem and abstract the interaction relationship between the content
  • devise deterministic methods for solving problems using computers,
  • Then solve the problem by writing and debugging code

This is the complete process from abstracting the problem to solving it.

3. Overview of Python language

3.1, the birth of Python language

Founder of the Python language: Guido van Rossum

2002, Python 2.x

2008, Python 3.x

3.2. Python minimal program
The Hello program written in Python language has only one line of code: print("Hello World")

>>> print("Hello World")
Hello World

">>>" in the first line is the prompt of the Python language operating environment

The second line is the execution result of the Python statement

  • The Python language supports the direct use of Chinese and other non-Western characters. The execution effect of the Python minimal program with Chinese in the operating environment is as follows:
>>> print("你好,Python")
你好,Python

4. Python development environment configuration

4.1. Installation

Go to the Python homepage to download and install the Python basic development and operating environment, URL:
official website download

  • Choose different versions according to different operating systems
  • Download the corresponding Python 3.0 series version program
    insert image description here
    insert image description here
    and click Install Now, and then all Next will be OK.

4.2, Python interpreter

The Python interpreter has two important tools:

  • IDLE: Python integrated development environment, used to write and debug Python code;
  • Pip: Python third-party library installation tool, used to install third-party libraries on the current computer

4.3. Editing method of Python program

Start the interactive Python runtime environment to output data through IDLE
insert image description here

  • Open IDLE, click Ctrl+N to open a new window, enter the statement and save it, use the shortcut key F5 to run the program

4.4, the running mode of Python program

  • There are two ways to run a Python program: interactive and file.
  • Interactively use the Python interpreter to immediately respond to the code input by the user and give the output result.
  • The file type writes the Python program in one or more files, and starts the Python interpreter to execute the code in the file in batches.
    • The interactive mode is generally used to debug a small amount of code, and the file mode is the most commonly used programming mode.
    • The environment displayed by starting IDLE is the Python interactive operating environment. Enter the code after the >>> prompt to run, and enter exit() or quit() to exit. The line without >>> indicates the running result.
  • The file program is written in the editing window of IDLE, and you can use the shortcut key "F5" or select the "Run - Run Module" option in the menu to run the Python code
  • In addition, you can also run the Python program through the Windows command line (cmd.exe). For a file named code.py, you can use the command line python
    code.py to run the program. In the graphical operating system, you can directly run the Python program by clicking the mouse.
  • An operating system without a Python interpreter cannot run a Python program directly. It needs to package the Python source code into an executable file. This process is called "program release".

5. Basic writing method of program

5.1. IPO program writing method

  • Input data
    Input (Input) is the beginning of a program. The data to be processed by the program comes from various sources, forming various input methods, including: file input, network input, console input, interactive interface input, random data input, internal parameter input, etc.

  • Processing Data
    Processing (Process) is the process in which a program performs calculations on input data to generate output results. Methods of dealing with computational problems are collectively called "algorithms", and they are the most important components of a program. It can be said that the algorithm is the soul of a program.

  • Output Data
    Output (Output) is the way the program displays the results of calculations. The output methods of the program include: console output, graphics output, file output, network output, internal variable output of the operating system, etc.

6. Features of Python programs

  • Python is versatile.
    The Python language can be used for the development of almost any programming-related application. It is not only suitable for training into thinking, but also for specific technical fields such as data analysis, machine learning, artificial intelligence, and Web development.

  • Concise Python syntax
    Python syntax is mainly used to accurately express the problem logic, which is closer to natural language. There are only 33 reserved words, which is very concise.

  • Python ecological high-yielding
    Python interpreter provides hundreds of built-in classes and function libraries. In addition, programmers from all over the world have contributed hundreds of thousands of third-party function libraries through open source communities, covering almost all fields of computer technology. Writing Python programs can Extensive use of existing built-in or third-party code, with a good programming ecology.

In addition to the three important features of Python syntax, there are some specific features of Python programs.

  • A platform-independent
    Python program can be executed in any computer environment where an interpreter is installed, so it can run across operating systems without modification.

  • Mandatory readability
    Python uses mandatory indentation (similar to the first line of space in an article paragraph) to reflect the logical relationship between statements, which significantly improves the readability of the program, thereby enhancing the maintainability of the Python program.

  • Support Chinese
    Python 3.x version to use Unicode encoding to express all character information. Unicode is an encoding system for international general expression characters, which enables Python programs to directly support various natural language characters such as English, Chinese, French, and German, and is more flexible and efficient when processing Chinese.

7. Example analysis: Python applet

7.1. Calculation of Fibonacci sequence

1,1,2,3,5,8,13,21,34…

F(0)=0, F(1)=1,

F(n)=F(n-2)+F(n-1), where n>=2

# CalFibonacci.py
a, b = 0, 1
while a < 1000:  # 输出不大于1000的序列
        print(a, end=", ")
        a, b = b, a + b

7.2 Calculation of circle area

Calculates the area of ​​a circle from its radius.

# CalCircleArea.py
r = 25    # 圆的半径是25
area = 3.1415 * r * r  
print(area)
print("{:.2f}".format(area))    # 只输出两位小数

7.3. Draw five-pointed red star

Draw a five-pointed red star graphic with a Python program. Please test on a local PC.

# DrawStar.py
from turtle import *
color('red', 'red')
begin_fill()
for i in range(5):
    fd(200)
    rt(144)
end_fill()
done()

7.4. Timing of program running

Record and output the running time of a program that loops 10 million times.

# CalRunTime.py
import time
limit = 10*1000*1000
start = time.perf_counter()
while True:
    limit -= 1
    if limit <= 0:
        break
delta = time.perf_counter() - start
print("程序运行时间是:{}秒".format(delta))

7.5. Draw colorful circles

Draw 7 circles of different colors to form a colorful circle pattern. Please test on a local PC.

# DrawSevenColorfulCircles.py
import turtle
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple']
for i in range(7):
    c = colors[i]
    turtle.color(c, c)
    turtle.begin_fill()
    turtle.rt(360/7)
    turtle.circle(50)
    turtle.end_fill()
turtle.done()

This section is mainly aimed at beginners, explaining what python is, its functions, characteristics, etc., and giving a few simple cases to increase readers' understanding of the language. I hope it will be helpful to everyone.

The Python drama is about to be staged, let's follow the drama together.

Guess you like

Origin blog.csdn.net/weixin_61587867/article/details/131847406