The two-day Python basics series of articles is the python part of the "learn by example" programming course. The original English Github repository is clicked here, and all the content has been posted to the short book (see chapters).
This series is only for learning and reference. My ability is limited. Many professional terms are in the process of learning. Please correct me if you make a mistake.
brief introduction:
From Wikipedia
Python is a widely used high-level programming language, a general-purpose programming language, created by Guido Van Rossum, the first edition was released in 1991. It can be regarded as an improved LISP (adding some advantages of other programming languages, such as object-oriented). As an interpreted language, Python's design philosophy emphasizes code readability and concise syntax (especially the use of space indentation to divide code blocks instead of braces or keywords). Compared to C++ or Java, Python allows developers to express ideas with less code. Regardless of whether it is a small or large program, the language tries to make the structure of the program clear.
Guido van Rossum (Dutch: Guido van Rossum, January 31, 1956 -), born in Haarlem, the Netherlands, is a computer programmer, the original designer and main architect of the Python programming language. In the Python community, Guido Van Rossum is considered a "benevolent dictator" (BDFL), which means that he still pays attention to the Python development process and makes decisions when necessary.
Get the Python suitable for your system from the official website-https://www.python.org/
Most Linux distributions have Python installed by default
See also this guide for more details and how to set up a virtual environment, how to use pip (never use sudo pip unless you know what you are doing).
This example uses Unix-like system, Python version 3 and uses bash shell
You can also run Python code online
pythontutor-code executor for python 2 and python 3, visual code flow, with sample programs
jupyter-a web application: allows you to create and share dynamic documents containing code, formulas, visualizations and explanations
ideone-online compilation and debugging tool, allowing you to execute and compile more than 60 programming languages online
Python Interpreter shell
It is assumed that you are familiar with the command line. If not, check the basic tutorials and Linux integration resource list on ryanstutorials
Let's start learning to use Python from a simple program:
#! /usr/bin/python3
print("Hello World")
The first line has two parts
/usr/bin/python3
is the path of the Python interpreter#!
Called shebang, it specifies the interpreter that executes this script file.The third line outputs the Hello World
message, and the print
function will add a newline after it by default.
Run Python program
You can use some text editors like gedit, vim or other editors to write script programs. After saving the file, add execute permission and run the program from the terminal.
$ chmod +x hello_world.py
$ ./hello_world.py
Hello World
Here is how to find the Python path and its version:
$ type python3
python3 is /usr/bin/python3
$ python3 --version
Python 3.4.3
If you have studied the Python 2 tutorial or have experience in Perl, it is easy to forget to add parentheses to the print
function. This is a common mistake.
#! /usr/bin/python3
print "Have a nice day"
$ ./syntax_error.py
File "./syntax_error.py", line 3
print "Have a nice day"^
SyntaxError: Missing parentheses in call to 'print'
Single-line comments start at
#
#!
Only the first line of the program has a special meaningIn later chapters we will see multi-line comments
#! /usr/bin/python3
# Greeting message
print("Hello World")
Further reading
>>>
_
saves the result of the last output expressionUp
key to match historical commands The Ctrl+l
key combination is used to clear the screen and save any typed commands intact exit()
Exit$ python3
Python 3.4.3(default, Oct 142015,20:28:29)[GCC 4.8.4] on linux
Type "help","copyright","credits" or "license"for more information.>>>print("hi")
hi
>>> abc
Traceback(most recent call last):
File "<stdin>", line 1,in<module>
NameError: name 'abc' is not defined
>>> num =5>>> num
5>>>3+47>>>12+ _
19>>> exit()
Further reading
The library contains built-in modules (written in C)—providing system feature interfaces such as file I/O and modules written in Python—providing standard solutions for many schedule programming problems.
Some of these modules encourage and enhance the compatibility of Python programs by abstracting platform-specific functions into platform-compatible APIs.
Editor: Wu Pancheng
Recommended Posts