You still have to take notes when you study. One is to deepen your impression of learning, and the other is to facilitate future review. I would like to thank CSDN for providing this platform.
He is not a pure professional. He mainly studied C language in school. He has almost forgotten it now. He took the opportunity to learn Python to improve his programming ability.
catalogue
1: Python installation and execution
2: Identifiers and keywords in Python
3: Python expressions, indents, and comments
4: Variables and literals in Python
1: Python installation and execution
Only methods under Windows are provided here. Please search for other operating systems by yourself.
Directly search the official Python website on the web page and download it. What I download here is the latest 3.9.6.
After downloading and installing, there will be several things. What we need is IDLE (python 3.x,)
python programming is different from C language. python is divided into two programming modes: 1 Interactive programming; 2. Document type.
After entering IDLE, interactive programming is displayed. As shown in the figure below.
Python 3.9.5 (default, May 4 2021, 03:36:27) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> # Execute single line code >>> print("Hello") Hello >>> >>> # Entering the \ symbol at the end of the statement can perform line feed and execute multiple lines of code >>> if True: \ ... print(True) ... True >>>
In this way, python will follow your footsteps to execute your statements, so that you can clearly see the execution results of the statements you write.
Another is to click file and New file in the upper left corner after entering IDLE. Open a file, write your code in it, and then save it. Remember when you save it, don't you py suffix. This is file programming.
I prefer the second method.
2: Identifiers and keywords in Python
The following identifiers are reserved words or keywords of the language and cannot be used as ordinary identifiers. Keywords must be spelled exactly as listed here.
False await else import pass None break except in raise True class finally is return and continue for lambda try as def from nonlocal while assert del global not with async elif if or yield
These keywords are very important. You need to remember the usage of these keywords! Remember! Remember!
In addition to keywords, some identifier classes have special meanings. The naming pattern of these identifier classes is the beginning and end of the following underlined characters: (these can be understood, do not need to be understood)
-
_*
It will not be imported by "from module import *. Special identifier_ It is used to store the latest evaluation result in the interactive interpreter; It is saved in builtins Module. When not in interactive mode_ It has no special meaning and is not predefined. See import statement . Annotation_ As a name, it is often used to connect international text; See gettext Module documentation for details on this engagement.
-
__*__
The system defined name is informally called "dunder". These names are defined by the interpreter and its implementation, including the standard library. More such names will be defined in future Python releases. In any case, any} does not comply with the requirements explicitly indicated in the document__*__ Name usage can lead to an error without warning.
-
__*
Private name of the class. When used in a class definition, this name is overridden in a mixed form to avoid name conflicts between the "private" properties of the base and derived classes. See Identifier (name) .
3: Python expressions, indents, and comments
3.1 expression
Expression is the basis of any programming language. In short, a minimum expression with complete functions is an expression.
Example: simplest expression
print("Hello World")
Example: incomplete expression
for v in [1,2,3]: #The for loop statement does not contain a loop body
After modification
for v in [1,2,3]: print(v)
Multiline expression
By default, Python expressions do not support line feed writing. Example:
# Writing error ab = "a" + "b"
We can use the \ symbol to realize line feed, which can be understood as telling the interpreter that the expression is not over. Example:
# Correct writing ab = "a" + \ "b"
What do you call an interpreter? The interpreter is similar to the compiler. It is to compile the code we write into the code well-known to the computer. What's the difference between an interpreter and a compiler? A: 1. An interpreter is a program that directly executes instructions written in a programming language, and a compiler is a program that converts the source code into a low-level language; 2. The compiler generates an independent program, and the interpreted program always needs the interpreter to run. If you still don't understand, you can find it yourself.
In addition, the expressions in the three symbols (), [], {} (see the difference between the three symbols below) support line feed by default. Example:
# () is represented as a whole, and () will increase the calculation priority in mathematical calculation str = ( "a" + "b" + "c" ) # [] represents a List structure (List) arr = [ "a", "b", "c", ] # {} represents a dictionary structure tel = { 'jack': 4098, 'sape': 4139 }
In Python; The symbol default expression does not need to end with a semicolon (this is also a big difference from the C language). If you need to write two expressions on the same line, you can separate them with a semicolon. Example:
print("Hello"); print("World")
3.2 indent
Most programming languages use {} symbols to define code blocks. Examples:
// PHP language code if (true) { echo "Hello"; } // C language code #include int main() { if (1 > 0) { printf("Hello"); } return 0; }
Use indentation instead of {} to define code blocks in Python (you can't arbitrarily indent the first line!), example:
if True: print("Hello World") # Write an expression on a blank line
Indent correctly, indenting your code block with 4 spaces. Python code must be indented strictly, or the Python interpreter will throw out errors:
IndentationError: expected an indented block
3.3 notes
Comments are used to explain the function of a piece of code. A single line comment can start with #. Python uses three double quotes' ', or three single quotes'', for multiline comments.
python document string
Let's take a look at an example:
# Specify a function that does not contain a document string def fn_str(): print("fn_str") # Use__ doc__ Print help for abc print(fn_str.__doc__) # "None" will be output
# Add document string for function def fn_str(): """This is a function""" print("fn_str") print(fn_str.__doc__) # The output "this is a function" is displayed
The document string is used to add document information for functions or classes. In addition to printing with doc, it can also be viewed with the help() function.
4: Variables and literals in Python
4.1 variables
Variable is one of the most important elements of programming language
Let's start with an example:
foo = "a" # Foo is a variable, and the value of foo is "a" # It is called a variable because the value of the variable can be changed foo = "b" print(foo) # Output b
Variable is the carrier of data. Python can store various data types.
Python uses the = sign to declare variables
numberVar = 123 # Declare a variable named numberVar whose value is a number floatVar = 123 # Declare a variable named numberVar whose value is a number booleVar = True # Declare a variable named numberVar whose value is a number noneVar = True # Declare a variable named numberVar whose value is a number listVar = [1,2,3,4,5] # Declare a variable named numberVar whose value is a number # You can also "store" a function in a variable 👇 def func(): print("this a function") funcVar = func funcVar() # Output "this a function"
4.2 face value
The literal value is the specific value of the text and cannot be modified. It may seem awkward: the literal value of 10 is 10.
Example:
print(10) # Print the literal value of 10 and output 10 print("abc") # Print the literal value of abc and output abc
# If you try to modify the literal value, you will receive an error explained by Python "abc" = 111 #Output: error: syntax error: cannot assign to literal
Summary:
1, This section describes how to install Python on different systems and how to run the Python interpreter and execute Python code.
main points:
- Using python -c command [arg] Run Python code in mode
- Using python -m module [arg] Running Python modules in mode
- Executing Python code in interactive mode
2, Please memorize the keywords.
3, This section describes how Python writes but line and multiline expressions, how to indent, how to write comments, and document strings
main points:
- Single line expression
- Multiline expressions: \, (), {}, []
- Notes: #, '', ''“
- Document string: "" Docstrings "" "
4, In this section, readers can follow the code example to practice how to declare the , variable , and what is the , literal.