1.1 Compiled and interpreted
-
Compiled: The system is compiled to base 2 at one time and then executed.
- Advantages: High execution efficiency
- Disadvantages: not cross-platform, low development efficiency
- Representative language: C
-
Interpreted: The binary is interpreted line by line and then executed.
- Advantages: Cross-platform, high development efficiency
- Disadvantages: Low execution efficiency
- Language: Python
1.2 annotations
1.2.1 Functions of annotations
Add comments to your code in a language you’re familiar with for later viewing and understanding, as well as for others to view and understand the code.
1.2.2 Single-line comments
Start with #, and everything after # will not be executed by the program.
Print ("hello world")Copy the code
Note: To ensure the readability of the code, it is recommended to add a space after #, and then add the description.
You can also add comments after the code, and for readability, it is recommended that there be at least two Spaces between the # and the code.
Use CTRL +/ shortcuts in PyCharm to add and remove comments to your code.
Print (" Hello world") # This is a one-line commentCopy the code
1.2.3 Multi-line comments
If you want to add a large number of comments, you can use multi-line comments that start with a “”” and end with a “””, and the text in the middle is the text to be commented. You can also start with a “” and end with a “”.
Print ("hello world") # print("hello world"Copy the code
1.3 Indentation (code blocks)
Python blocks of code are not controlled by curly braces {}. Python’s most distinctive feature is the use of indentation to write modules. The amount of indented white space is variable, but it is strictly enforced that all block statements must contain the same amount of indented white space.
- According to conventional management, you should always use four Spaces indented;
- Many Python programmers recommend no more than 80 characters per line.
- To separate different parts of your program, you can use blank lines, but don’t use too many blank lines in your program.
1.3.1 Multiple statements form a code block
- In Python, a newline terminates a statement.
- A set of statements with the same indent form a block of code.
- Compound statements such as if, while, def, and class that begin with a keyword and end with a colon:, followed by one or more lines of indented code that form a block of code.
1.4 One line and multiple lines
1.4.1 Multiple statements are displayed on the same line
Python can use multiple statements on the same line, separated by semicolons.
1.4.2 A statement is written in multiple lines
Python statements usually end with a new line, but you can use a slash () to break a single line into multiple lines. If a statement contains [], {}, (), you do not need to use a multi-line hyphen.
aa = ["spring", "summer", "autumn",
"winter"]
Copy the code
1.5 the quotation marks
Python can use single, double, or triple quotation marks to represent strings. The quotation marks must start and end with the same type. Triple quotation marks can consist of multiple lines, and can be used as comments at certain points in a file.
1.6 Basic use of variables
In Python, every variable must be assigned before it can be used. After the variable is assigned, it is created. The equal sign is used to assign a value to the variable.
Print (qq_number) print(qq_password) # ----- # 123456789 # 12345Copy the code
Description:
-
The name of a variable is defined only when it first appears. When it appears again, it directly uses a previously defined variable (in the same scope).
-
Python defines variables without specifying the type of the variable. At runtime, the interpreter automatically deduces the exact type of the data stored in the variable.
-
The variable name can contain letters, digits, and underscores (_). It cannot start with a digit, and cannot be the same as a keyword.
-
Variable names in Python are case sensitive.
-
When you define variables, you should leave a space on the left and right sides of the = for code formatting purposes.
-
Variable names that begin with an underscore have a special meaning:
- _foo begins with a single underscore. _foo represents a class attribute that is not directly accessible. It needs to be accessed through the interface provided by the class.
- __foo, which begins with a double underscore, represents a private member of the class, and __foo__, which begins and ends with a double underscore, represents flags that are specific to special methods in Python, such as __init__(), which represents a class constructor.
-
In Python, if the variable name consists of two or more words, it can be named as follows:
- Use lowercase letters for each word, and use _ _ between words.
- In Python, variable, function, and module names are linked with underscores (user_name_and_password).
- Class names in Python use the big hump naming method (capitalize the first letter of each word, PersonModel).
1.7 constant
Constant refers to a constant quantity, such as PAI 3.141592653… , or does not change during the course of the program.
For example, if Xiao Ming’s age changes, that is a variable, but in some cases, his age does not change, that is a constant. There is no special syntax for constants in Python, and it is conventional for programmers to use all uppercase variable names to represent constants.
AGE_OF_PP = 56
Copy the code
1.8 the keyword
Keywords are identifiers that are already used internally in Python. We cannot define variables with the same name as identifiers.
To view keywords in Python, run the following command:
Import keyword print (keyword. Kwlist) -- -- -- -- -- -- the output -- -- -- -- -- -- [' False ', 'None', 'True', 'and', 'as' and' 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']Copy the code
1.9 Reference to variables
Variables and data are stored in separate, data stored in the memory of a place, variable holds the data in the memory address, called the reference, using the id () function can view variables stored in data memory address, in addition, if the variable has been defined, when to a variable assignment is essentially to modify the data.
The essence of function passing is that the argument holds a reference to the data, not the data that the argument holds.
def test(num): Print (" %d = %d" % (num, Id (num)) a = 10 print("a = %d" % id(a)) # print("a = %d" % id(a)) #Copy the code
Returns inside a function that actually returns a reference to the data, not the data itself.
def test(): Result = "hello" print(" %s = %d ") % (result, Id (result)) return result a = test() print(" %d "% id(a))Copy the code
Parameters used in Python is the manner in which “the object reference”, if a function is a mutable object received a reference list or dictionary, can change the original value of the object, if the function is an immutable object received (tuples, Numbers, strings, etc.), a reference, cannot directly modifying the original object. Variables and data are stored in memory, but the variables and data are stored separately, the data is stored in memory, the variable holds the address of the data in memory, the address of the data recorded in the variable is called a reference, you can use the id() function to view the address of the variable. In addition, immutable data types include tuple, string, numeric types int, bool, float, complex, long, etc. Mutable data types include lists, dictionaries, etc.
When we use a dictionary, the key value of the dictionary must be of an immutable data type.
A = '1234' b = a a = 'hello' print("a:", a) print("b:", b) print("a:", id(a)) print("b:", id(a)) print("b:", id(a)) print("b:", id(a)) print("b:", id(a)) print("b:", id(a)) print("b:", id(a)) ", id(b)) # 2. A = [1, 2, 3, 4] b = a b[3] = 8 print("a:", a) print("b:", b) print("a:", id(a)) print("b:", id(a)) print("b:", id(a)) print("b:", id(a)) print("b:", id(a)) print("b:", id(a)) print("b:", id(a)) ", id(b)) # ------- ------- # a: hello # b: 1234 # a [1, 2, 3, 8] # b: [1, 2, 3, 8] # a memory address: 2699911201224 # B memory address: 2699911201224Copy the code
1.10 Local and Global variables
A local variable is a variable defined inside a function and can only be used inside a function. A global variable is a variable defined outside a function (not within a function) and can be used inside all functions.
Within a function, you can retrieve data from a reference to a global variable, but you are not allowed to modify the reference directly.
Print ("func1==>%d" % num) def func2(): "func1==>%d" % num) def func2(): Print (" func2 = = > % d % "num) func1 () function func2 # () -- -- -- -- -- -- -- -- output -- -- -- -- -- -- -- -- -- 90 # # func1 = = > func2 = = > 10Copy the code
When modifying a global variable inside a function, you can use the global keyword.
Num = 10 def func1(): global The num # global keyword tells the interpreter that the following variable is a global variable, and local variables are not created when assignment statements are used. print("func1==>%d" % num) def func2(): Print (" func2 = = > % d % "num) func1 () function func2 (#) output -- -- -- -- -- -- -- -- - -- -- -- -- -- -- -- -- # func1 = = > 10 # func2 = = > 10Copy the code
Suggestions for naming global variables:
Global variable names should be prefixed with g_ or gl_.
1.11 Program Interaction (INPUT)
The input() function pauses the program, waits for the user to enter some text, retrives the user’s input, and stores its value in a variable for easy use.
name = input("What is your name?" ) age = input("How old are you?" ) hometown = input("Where is your hometown?" ) print("Hello ", name, "your are ", age, "years old, you came from", hometown)Copy the code
Note: The result of a variable is a string, regardless of what the user enters.
1.12 Print
The output of Python3 uses the print function, which is divided into newline output and non-newline output. By adding a string to the parentheses of print(), you can output the specified text to the screen. In parentheses, you can also put an expression.
print(value, ... , sep=' ', end='\n', file=sys.stdout, flush=False)Copy the code
- The sep argument is used to indicate which character is used to separate values in the output. By default, Spaces are used as separators.
- Non-newline output: Output all the arguments in parentheses on one line, just add one after each argument. There is no comma after the last argument, a comma between the arguments, and a space after the output.
- Newline output: print () defaults to newline output. Print (b,end= “) print(b,end= “)
1.12.1 Output commonly used functions
- STR (): converts objects to human-readable form, returning the string form of an object.
- Repr (): converts an object to a form that can be read by the interpreter. It returns the string form of the object and can print all characters, including escaped characters.
# - * - coding: utf-8 - * - x = "abcde \ nf \ ng" print (STR) (x) print (repr (x)) -- -- -- -- -- -- -- -- -- the output -- -- -- -- -- -- -- -- -- -- abcde f g 'abcde \ nf \ ng'Copy the code