Follow 👆 the public account and reply 'python' to receive the beginner's tutorial! Source from the internet, delete if infringing
1. Hello World
After installing Python, open IDLE (Python GUI). This program is the Python language interpreter, and the statements you write can run immediately.
[Tutorial The method of obtaining is at the end of the article!!]
[Tutorial The method of obtaining is at the end of the article!!]
We write a famous program statement:
Press Enter, and you will see this famous quote introduced into the programming world by K&R. In the interpreter, choose “File” — “New Window” or use the shortcut Ctrl+N to open a new editor.
Write the following statement:
Save it as a.py file.
Press F5, and you will see the program’s running result.
This is the second way to run Python.
You can also double-click the saved a.py file to see the program result.
Python programs can run directly, which is an advantage compared to Java.
2. Internationalization Support
Let’s greet the world in a different way. Create a new editor and write the following code:
When you save the code, Python will prompt you to change the character set of the file, as shown below:
Change the character set to a more familiar form:
The program runs well as usual.
3. Convenient and Easy-to-use Calculator
Using the calculator that comes with Microsoft is really cumbersome. Open the Python interpreter and calculate directly:
4. Strings, ASCII, and UNICODE
You can print predefined formatted strings as follows:
How are strings accessed? Please see this example:
Please note the difference between ASCII and UNICODE strings:
5. Using Lists
Similar to the List in Java, this is a convenient and easy-to-use data type:
6. Conditional and Loop Statements
7. Defining Functions
Additionally, introduce a convenient and easy-to-use function:
8. File I/O
9. Exception Handling
10. Classes and Inheritance
11. Package Mechanism
Each .py file is called a module, and modules can import each other. Please refer to the following example:
Modules can be defined inside packages.
The way Python defines packages is a bit quirky. Suppose we have a parent folder, which has a child subfolder, and the child contains a module a.py.
How does Python know this file structure? It’s simple; each directory has a file named __init__.py. The content of this file can be empty.
This structure is shown below:
So how does Python find the modules we defined? In the standard package sys, the path attribute records the Python package paths. You can print it out:
Usually, we can put the module’s package path into the environment variable PYTHONPATH, and this environment variable will be automatically added to the sys.path attribute.
Another convenient method is to directly specify our module path into sys.path in the code:
12. Summary
You will find this tutorial quite simple.
Many Python features are implicitly presented in the code, including:
Python does not require explicit declaration of data types, keyword explanations, string function interpretations, etc.
A skilled programmer should be quite familiar with these concepts, so after you squeeze out an hour to read this short tutorial, you can quickly get familiar with Python through the transfer analogy of existing knowledge and start programming with it. Of course, the idea of learning Python in 1 hour is a bit sensational.
To be precise, programming languages include syntax and standard libraries.
Syntax is equivalent to martial arts moves, while standard library application practice experience is similar to internal skill, which requires long-term training.
Python has learned from the strengths of Java, providing a large number of very convenient standard libraries for programmers to utilize. (This is also the reason for Python’s success).
How to Obtain:
-
Like + Look Again
-
Reply in the public account: “python”
Get the latest 2024 Python beginner learning materials,Reply in the backend:Python