Supplementary content
1, The way we manipulate the code, the terminal file terminal string needs quotation marks
The file name ends in. py. The interpreter declares the terminal execution file
The execution process of the input command belongs to code -- > interpreter -- > syntax and lexical analysis
Create a hello.py file in the / root directory, as follows:
print("hello,world")
Execute the hello.py file, namely: python /root/hello.py
The internal execution process of python is as follows:
If you need more tutorials, just scan the code on wechat
👆👆👆
Don't forget to scan the code to get the information [HD Java learning roadmap]
And [full set of learning videos and supporting materials]
2, Interpreter
When executing python3 /root/hello.py in the previous step, it is clearly pointed out that the hello.py script is executed by the Python interpreter.
If you want to execute a python script like a shell script, such as. / hello.py, you need to specify an interpreter in the header of the hello.py file, as follows:
#!/usr/bin/env python3 print("hello,world")
In this way, execute:. / hello.py.
ps: before execution, give the permission to execute hello.py, chmod 755 hello.py
#!/usr/bin/env python3 print("hello,world")
3, Content coding
python The interpreter is loading .py When the code in the file is, the content is encoded (default) ascill) ASCII(American Standard Code for Information Interchange,American standard information exchange code) is a set of computer coding system based on Latin alphabet, which is mainly used to display modern English and other Western European languages. It can only be represented by 8 bits (one byte) at most, i.e. 2**8 = 256,So, ASCII The code can only represent 256 symbols at most. obviously ASCII Codes cannot represent all the characters and symbols in the world. Therefore, a new code that can represent all characters and symbols is needed, namely: Unicode Unicode(Unified code, universal code, single code) is a character code used on computers. Unicode It is produced to solve the limitations of the traditional character coding scheme. It sets a unified and unique binary coding for each character in each language, and stipulates that although some characters and symbols are represented by at least 16 bits (2 bytes), that is, 2 **16 = 65536, Note: at least 2 bytes are mentioned here, maybe more UTF-8,Yes Unicode For the compression and optimization of coding, it no longer uses at least 2 bytes, but classifies all characters and symbols: ascii The contents of the code are saved with 1 byte, European characters with 2 bytes and East Asian characters with 3 bytes... So, python The interpreter is loading .py When the code in the file is, the content is encoded (default) ascill),If it is the following code: report errors: ascii Code cannot represent Chinese
4, Notes
- Shell annotation method:
- Single line comment: # annotated content
- Multiline comment: < <! Note content!
- Python annotation method
- Single line comment: # annotated content
- Multiline comment: "" annotated content "" "
5, Execute script to pass in parameters
Python has a large number of modules, which makes the development of Python programs very simple. The class library includes three:
- Modules provided internally in Python
- Open source modules in the industry
- Modules developed by programmers themselves (warm tips, python modules can be c language, etc.)
Python internally provides a sys module, in which sys.argv is used to capture the parameters passed in when executing Python scripts
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys print(sys.argv)
6, pyc file
When executing Python code, if other. py files are imported, a. pyc file with the same name will be automatically generated during execution, which is the bytecode generated after compilation by the Python interpreter.
https://www.jianshu.com/p/f0c6130f7b13
ps: bytecode can be generated by compiling the code; bytecode can also be obtained by decompiling.
7, Input
#!/usr/bin/env python # -*- coding: utf-8 -*- # Assign the user input to the name variable name = input("Please enter user name:") # Print input print(name)
When entering the password, if you want to be invisible, you need to use the getpass method in the getpass module, that is:
#!/usr/bin/env python # -*- coding: utf-8 -*- import getpass # Assign the user input to the name variable pwd = getpass.getpass("Please input a password:") # Print input print(pwd)
5, Variable
1. Declaration of variables
python is a dynamic language
- Variables do not need to be declared in advance
- The type of the variable does not need to be declared
Each variable must be assigned before use, and the variable will not be created until it is assigned.
In Python, a variable is a variable. It has no type. What we call type is the type of object in memory that the variable refers to.
The equal sign (=) is used to assign values to variables.
The left side of the equal sign (=) operator is a variable name, and the right side of the equal sign (=) operator is the specific value it points to.
a = 1 a = 'Qianfeng'
2. Variable naming rules
Can contain the following characters
- Upper and lower case letters (a-z,A-Z)
- Variable names are case sensitive; A and A are different variables
- Number (0-9)
- Underscore (_) = = cannot start with a number==
3. Variable naming rules
- Do not start with a single underline or a double underline; for example, _useror _user
- Variable names should be easy to read; for example, user_name, not username
- Do not use the module name in the standard library (built-in) or the module name of a third party
- Do not use these Python built-in Keywords:
>>> import keyword >>> keyword.kwlist ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] > The data printed on it is Python Is called a list, and the list is Python A data structure in. The data structure will be described in detail in later chapters. >The list is Python3 Keywords in. #You can use the following method to verify that returning True is the keyword of Python >>> keyword.iskeyword('del') True >>>
4. Variable assignment
In python, the equal sign = is used to assign values to variables,.
The left side of the equal sign is called the variable name, and the right side of the equal sign is called the value of the variable, specifically the object
n = 5
5. What are variables in Python
How to correctly understand the assignment process of variables in python?
s = 'hello'
The above variable assignment should be said to be more reasonable to assign variables to objects.
The string object hello will be created in memory first, and then the variable name s will be assigned to this object.
Therefore, to understand variable assignment in Python, you should always look to the right of the equal sign.
The object is created or obtained on the right, and then the variable name on the left is bound to the object, which is like labeling the object.
Variable names are essentially object labels or object names. Of course, an object can have multiple labels or names. For example, Zhang San and Xiao Zhang refer to the same person
See the following code example:
a = 1 b = a a = 2 print(b) # b could it be?
When a = 1, see the following figure:
When b = a, see the following figure:
When a = 2, see the figure below:
b = a above, we call it When the reference is passed, the object has two names (labels), a and b
6. Multivariate assignment of variables
In Python 3, you can assign values to variables like this
In [2]: x, y, z = 1, 2, 3 In [3]: x Out[3]: 1 In [4]: y Out[4]: 2 In [5]: z Out[5]: 3
Of course you can
If you need to unlock and assign the data in a sequence type one by one, the variable name to the left of the equal sign needs to be the same as the number of elements in the sequence type data.In [10]: a, b, c = 'abc' In [11]: a Out[11]: 'a' In [12]: b Out[12]: 'b' In [13]: c Out[13]: 'c'
This multiple assignment method can also be called tuple unpacking in Python.
7. In Python, objects (values of variables) have three properties
# The unique identifier is the integer representation of the object in memory CPython Can be understood as# Memory address # You can use the id function to view id(10) # Directly to an object id(n) # Give you the variable name # Types and objects have different types. Use the type function to view them type(10) type(n) # Value, object itself 10
6, Operator
1. Arithmetic operation
#!/usr/bin/python3 a = 21 b = 10 c = 0 c = a + b print ("c The value of is:", c) c = a - b print ("c The value of is:", c) c = a * b print ("c The value of is:", c) c = a / b print ("c The value of is:", c) c = a % b print ("c The value of is:", c) # Modify variables a, b, c a = 2 b = 3 c = a**b print ("c The value of is:", c) a = 10 b = 5 c = a//b print ("c The value of is:", c) c The value of is: 31 c The value of is: 11 c The value of is: 210 c The value of is: 2.1 c The value of is: 1 c The value of is: 8 c The value of is: 2
2. Comparison operation
3. Assignment operation
#!/usr/bin/python3 a = 21 b = 10 c = 0 if ( a == b ): print ("a be equal to b") else: print ("a Not equal to b") if ( a != b ): print ("a Not equal to b") else: print ("a be equal to b") if ( a < b ): print ("a less than b") else: print ("a Greater than or equal to b") if ( a > b ): print ("a greater than b") else: print ("Less than or equal to b") # Modify the values of variables a and b a = 5; b = 20; if ( a <= b ): print ("a Less than or equal to b") else: print ("a greater than b") if ( b >= a ): print ("b Greater than or equal to a") else: print ("b less than a") 1 - a Not equal to b 2 - a Not equal to b 3 - a Greater than or equal to b 4 - a greater than b 5 - a Less than or equal to b 6 - b Greater than or equal to a
4. Logical operation
#!/usr/bin/python3 a = 10 b = 20 if ( a and b ): print ("variable a and b All for true") else: print ("variable a and b One is not true") if ( a or b ): print ("variable a and b All for true,Or one of the variables is true") else: print ("variable a and b Not for true") # Modify the value of variable a a = 0 if ( a and b ): print ("variable a and b All for true") else: print ("variable a and b One is not true") if ( a or b ): print ("variable a and b All for true,Or one of the variables is true") else: print ("variable a and b Not for true") if not( a and b ): print ("variable a and b All for false,Or one of the variables is false") else: print ("variable a and b All for true") 1 - variable a and b All for true 2 - variable a and b All for true,Or one of the variables is true 3 - variable a and b One is not true 4 - variable a and b All for true,Or one of the variables is true 5 - variable a and b All for false,Or one of the variables is false
5. Member operation
#!/usr/bin/python3 a = 10 b = 20 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print ("variable a In the given list list in") else: print ("variable a Not in the given list list in") if ( b not in list ): print ("variable b Not in the given list list in") else: print ("variable b In the given list list in") # Modify the value of variable a a = 2 if ( a in list ): print ("variable a In the given list list in") else: print ("variable a Not in the given list list in") 1 - variable a Not in the given list list in 2 - variable b Not in the given list list in 3 - variable a In the given list list in
7, Process control statement
1. Judge
Basic syntax format:
#if Conditional statement: # Note that this must end with a colon in English # The statement executed when the conditional statement is true n = input("Enter number>>:") n = int(n) # The data received by input is of string type if n == 5: print('equal') n = input("Enter number>>:") n = int(n) if n == 5: print('equal') else: # else must be followed by an English colon print('No') #_*_ coding:utf-8 _*_ n = input("Enter number>>:") # You must enter a number to test if not n: # If empty print("Null value") # Output null value else: #otherwise n = int(n) # Convert n to integer if n == 5: # If equal to 5 print('ok') # Output ok elif n > 5: # If greater than 5 print('Big') # The output is too large else: # otherwise print('Small') #The output is small
2. Nesting
n = input("Enter number>>:") if n.isdigit(): f_n = int(n) if f_n == 5: print('ok') elif f_n > 5: print('Big') else: print('Small') else: print('please enter a number')
3. Circulation
while True: n = input("Enter number>>:") n = int(n) if n == 5: print('equal') break elif n > 5: print('Big') else: print('Small')
4. Iteration
- What is iteration
Iteration is the activity of repeating the feedback process, and its purpose is usually to approach and achieve the desired goal or result.
Each repetition of the process is called an "iteration".
- For loop (English: for loop)
Is an iterative statement of a programming language that allows code to run repeatedly.
The biggest difference between it and other loops, such as the while loop, is that it has a loop counter.
This enables the for loop to know the execution order during the iteration and remember the position of the last element to be cycled.
for i in 'hello world': print(i)
range(n)
Generates a sequence of integers that can be cycled. The elements of the default sequence start from zero
The number of generated elements is n
for i in range(5): print(i)
break and continue
Range (start index: end index: step) works the same way as slicingfor i in range(0, 10): print(i) if i < 3: inp_num = int(input(">>:").strip()) # inp_num = int(inp_num) if inp_num == 15: print('You get it') break elif inp_num > 15: print("High") else: print("Low") else: print("Game over") break for i in range(2, 10, 2): print("Cycle to", i) if i == 4: continue print(i) if i == 6: break
5. Expand knowledge
for ... else
• 1. When the for loop body is normally executed, execute the code after else
• 2. When break is executed, the code behind else will not be executed
for i in range(2): if i == 10: print('get it') n = i break else: print("Nothing")
If you need more tutorials, just scan the code on wechat
👆👆👆
Don't forget to scan the code to get the information [HD Java learning roadmap]
And [full set of learning videos and supporting materials]