Prior to the start
I have long intended to write a series of articles discussing the basics of Python from a non-beginner’s perspective.
There are many beginner’s guides, ranging from beginner’s to advanced, and some are excellent, like my own beginner’s Python tutorial by Mr. Liao xuefeng. To be honest, it is not easy to add to these introductory courses, and I have no intention of studying introductory programming.
What I want is to sort out the Foundation of Python, compare a little source code, and write something through my own research. In this process, I can lay a solid foundation and improve my skills.
The series is tentatively titled The Python Basics Manual. This is more like a Python manual for yourself, My version of the Python Cookbook.
Look at the basics, write examples, read the source code, and then make notes, hope to help more friends learning Python help.
Since it is not for beginners, it requires the reader to have a certain foundation. For example, you should know how to use the command line before reading this article, and if you don’t, I recommend learning Zed A. Shaw’s Command Line Quickstart (which you can read on Zed A. Shaw’s website).
(You don’t have to, of course. I’ve tried many ways to encourage my friends (those who prefer Windows) to use the command line, but nothing has worked. I don’t want to do it now, and since someone can tolerate the inconvenience of not being able to use the command line, I don’t know what to say. After all, I can use the command line, not very well, but enough to deal with my problems, and you can’t use the command line, so it’s not my problem. On the command line powerful at the same time, that they are looking at the window, for “from a specific file to extract specific fields to batch rename hundreds of file” simple entry-level job command line do not know how to start, so we have to start baidu “batch rename a key artifact. Exe download” geeks who secretly distress is a pleasant thing)
Well, that’s a good thing to say, but Python can do this quite gracefully, too, and I’ll show you how to do this in Python in a future article if I have a chance. But that requires us to take a look at how Python actually works 👇👇👇
The Python interpreter
You know That Python is an interpreted language, so there is something called the Python interpreter. The Python code we write runs through the interpreter.
Without further ado, here’s how to use the interpreter:
Start the
$ python
: Enter the interactive environment.$ python -
: The script name is specified as ‘-‘ (for standard input), and$ python
Equivalent.$ python -c command [arg] ...
: Executes Python statements on the command line, similar to the -c option in the shell. Wrap command in ‘single quotes’, where python statements are written, similar to awk.$ python -m module [arg] ...
: Some Python modules can also be used as scripts. This is similar to typing the full pathname on the command line to execute the module source file.$ python filename
: Executes the Python source file.$ python -i filename
: Executes the Python source file, then does not exit, leaving the Python interactive command line.
exit
- Enter an end-of-file character (for Unix systems
Control-D
For Windows, yesControl-Z
) causes the interpreter to exit with a 0 status code. - The input
quit()
Command to exit the interpreter. - The input
exit()
Command to exit the interpreter.
Command line argument passing
When the interpreter is called, the script name and additional arguments are passed in a list of strings called sys.argv, similar to the CHAR *argv[] of the C main function.
Import sys before fetching sys.argv.
The behavior of argv is discussed in the form of C/C++ switch-case statements:
switch(Enter command) {case "$ python":
// sys.argv has one element: sys.argv[0] is an empty string
break;
case "$ python -":
// sys.argv[0] is set to '-'
break;
case "$ python -c command [arg] ...":
// sys.argv[0] set to '-c'
break;
case "$ python -m module [arg] ...":
// sys.argv[0] is set to the full name of the specified module
break;
}
/* select * from 'switch' where 'expression' must be an integer! * /
Copy the code
⚠️ [Note] : Arguments following the -c directive or the -m module are not captured by the Python interpreter’s opt-handling mechanism, but are left in sys.argv for script operations. Such as:
$ python3 -i -c 'import sys' 1 2 3
>>> sys.argv
['-c', '1', '2', '3']
Copy the code
Specifies the source code
By default, Python source files are encoded in UTF-8. The #! Insert at least one special comment line after the first line to define the encoding of the source file:
# -*- coding: encoding -*-
Copy the code
List of available Encodings
⚠️ [Note] If you use Microsoft Windows’ Notepad ‘to edit Python source code, use UTF-8 without BOM!
Python annotation
No one would deny the importance of comments. Before we start talking about other syntax, it’s worth taking a look at how comments are written.
Comments in Python start with the # character and continue to the end of the actual line (representing the actual line feed, not the editor’s wrap). Comments can start at the beginning of a line, after whitespace or code, but do not appear in the string.
# This is a comment
Copy the code
When writing document comments, we often borrow multi-line strings:
def func(a) :
"" the function func is a detailed behavior description of what to do... Args: a: what is parameter a? Returns: What is the returned value? """
class Cls(object) :
""" class summary detailed behavior description... Attributes: a: description of b: description of B """
Copy the code
[Note] Documentation comments are covered in more detail later in this series in Python Code Styles.
Basic Python operators
Computers, as the name suggests, are good at computing! Since programming languages are the ones in which humans and computers communicate, they should make it easy for computers to do math in simple ways — starting with addition, subtraction, multiplication, and division. We’re all familiar with addition, subtraction, multiplication, and division in Python. It’s basically written like we saw in elementary school math:
The operator | role |
---|---|
+ ,- ,* |
Used for addition, subtraction, and multiplication operations |
/ |
Division, returning a floating point number |
// |
Except for transport, the return result is rounded (Floor, round down) |
% |
Take over operations |
支那 |
Power operation |
(a) |
Used for grouping |
= |
Used to assign a value to a variable |
= = |
To determine whether the values are equal |
⚠️ [A few notes] :
- Variables must be “defined” (assigned) before they can be used, otherwise an error occurs.
- In interactive mode, the value of the most recent expression is assigned to a variable
_
. - Floating-point numbers are fully supported; In mixed integer and floating point calculations, the integer is converted to floating point.
>>> a = 1
>>> b = 2
>>> c
Traceback (most recent call last):
File "<stdin>", line 1.in <module>
NameError: name 'c' is not defined
>>> a + b
3
>>> _ + 3 # (3) + 3
6
>>> c = _ * 2 # c = (6) * 2
>>> c
12
>>> d = 0.3
>>> a + d
1.3
Copy the code
For integer types, you can also do bit operations like C:
operation | instructions |
---|---|
`x | y` |
x & y |
X and Y are bitwise logical and |
x ^ y |
X and Y are xor bits |
x << n |
Let’s move x to the left by n bits |
x >> n |
Let’s move x to the right by n bits |
~x |
X is a bitwise logical non |
To be continued…
Next: [Python basic data type]