basic python programming
Python is a simple and convenient language. If you want to program in python, you first need to create a new notepad file and change the suffix to .py. In this way, you have created a new python script, and then you only need to write the python code into this script file to run it.
For example, the result of running the above code is:
tab
In C or C++ or Java, logic uses curly braces ({}) to enclose a block of code. In python, use tab, if you don't use tab correctly, you will get an error.
The above for loop code block is grouped using tab indentation.
If you don't use tab, then python will pop up a prompt box to report an error before running.
The result of the above code is:
This is the program to solve 0+1+2+3+4+5+6+7+8+9.
If you use the above code, then the for loop only includes the code to solve the sum, not the printing code. The printing code is outside the loop, so it will only be printed once, which is the final result:
python line / block
Each instruction in python is called a line of python.
Each of the following sentences is a python line.
In C or C++ or Java, a semicolon (;) is required at the end of each statement, but not in python, but if you add a semicolon, no error will be reported.
Each paragraph of code indented with tabs is called a python block.
The above is a python code block composed of a for loop.
Above are two python code blocks, above is a python function code block, below is a python for loop code block.
These two python code blocks can output the results of the first 9 items of the Fibonacci sequence.
When writing programs, we should make the function of each code block clear, so that our programming will be more efficient with less effort.
Blank line
Blank lines that you can use in python make your program more legible. Normally blank lines are used between two code blocks, you can blank one or more lines to make the division before the code block clearer.
Don't abuse blank lines as it will make your code fragmented!
python comments
When programming, we write a lot of code, and we may confuse the meaning of the code ourselves. So comments are very important.
When we check the previous program, it will be very convenient if there are comments (explaining the meaning of code and variables).
There are three ways to comment in python:
The pound sign (#) is a comment method that comes with python, and the content after # will be automatically skipped by the program (these comments are for humans, and the machine does not need to understand the meaning)
The second way of commenting is to write a string, which is not assigned to any variable and has no meaning. The python garbage collection system will recycle this string, so writing it this way will not put a memory load on the python program.
The contents of the string have no effect on the program, so this method is used as a comment in many languages (such as cmd, bash, VB)
The way of three quotation marks can also be used for python comments. Similarly, the three quotation marks are a string, but it is not assigned to any variable, so this string will not affect the operation of the program, and the garbage collection system of python will recycle it. This string, so there is no need to consider the memory impact of this string.
The advantage of using triple quotes over pound signs and single quotes is that triple quotes support multiple lines, so you can write multi-line comments.
You can use the above methods to make your comments more prominent.
operator
arithmetic operators
Python provides operators for various calculations:
comparison operator
Comparison operators are used to compare the size of numeric values.
assignment operator
Like C++, python supports assignment to self-increment operations:
You will find that i=i+5 and i+=5 mean the same thing.
All operations support the increment operator:
The new version of python also has a walrus operator (:=), but this operator is rarely used in companies and programs for compatibility reasons, so I won't discuss it here.
bitwise operators
Bit manipulation is very important in computers, but it doesn't seem to be very important in a high level language like python.
Basic bitwise operations include AND, OR, NOT, XOR, left shift, right shift, etc.:
Use the 0b keyword to enter a binary number (each digit can only have 0 or 1)
Use the bin function to output numbers in binary form.
Binary AND means that if the corresponding position of the two numbers is 1, then output 1, if not, then output 0.
Binary or means that if the corresponding positions of the two numbers are not both 0, then output 1, if both are 0, output 0.
Binary XOR means that if the corresponding positions of the two numbers are the same, the output is 0, and if the corresponding positions of the two numbers are different, the output is 1.
Binary not means that if the original value of this bit is 1, output 0, if the original value of this bit is 0, output 1.
Because the highest bit of a is 1, a will become a negative number after the NOT gate operation.
Shifting left and right in the computer is to move the number to the left or right in the memory. Because the computer is binary, the whole number is doubled by moving one bit to the left (equivalent to multiplying by 2), and moving one bit to the right. The whole number is reduced by a factor of two (equivalent to dividing by 2)
It is not recommended to use bit operations here, because using bit operations in python is not really bit operations, and does not save time than operations such as addition and subtraction (it does save time in Java)
And bit operations have potential hidden dangers (data overflow problem)
Here are the classic usage of bit manipulation:
The value of two numbers can be simply exchanged through the XOR operation. In Java, you need to pay special attention to the problem of incorrect results caused by negative overflow, but there is no such problem in python.
It's worth noting that using the above code in python is not a good option as native code in python is more efficient.
Because python stores label pointers, swapping two numbers using python's built-in method is just exchanging the pointers of the two numbers, which is more efficient.
Logical Operators
Logical operations are Boolean algebra operations. There are only two kinds of values in Boolean algebra, True and False. where False is 0 and True can be any value other than 0.
The above code is the usage of AND.
The above is the usage of or and not.
member operator
The member operators are in and not in, which respectively measure whether one content is in another.
Statistics
Start time of this page: January 11, 2022
Completion time of this page: January 11, 2022
Last updated