basic python programming 3
basic logic
In today's tutorial I'll cover basic logic and loops:
Conditional control
The if statement is used for conditional control in python.
You can see how the if statement is used: first, the if is followed by the judgment condition, and the two = means to judge whether i is equal to 3. If i is equal to 3, then this executes the following code, if i is not equal to three, the following code is not going to be executed.
elif is the abbreviation of else if. If the statement in if is not executed, then if the condition in elif is true, the statement in elif is executed.
If there is an else statement, the else statement will be executed if the previous conditions are not met.
It's worth noting that if and elif will only be executed once.
The following is the simplest form of an if statement:
you can add else
So in the if statement, you can just write the if statement, you can also combine if and elif, or you can combine if and else, the most complete if statement includes if, elif and else.
Small exercise: Leap year judgment.
Determine if a year is a leap year.
Since 1984, astronomy has adopted the Julian year (not to be confused with the Julian calendar in the calendar) as a unified year time unit, which is specified as an exact 365.25 days, with an error of 1 day every 3000 years compared with the tropical year. In the modern Gregorian calendar, a normal year has 365 days, and a leap year has 366 days.
In the Gregorian calendar, most of the years that are divisible by 4 are leap years, but years that are divisible by 100 but not divisible by 400 are not leap years. For example, 1900 is a normal year, and 2000 is a leap year.
The above is a way of writing leap year detection program.
We commented out the previous method, and the new method can also judge leap years.
The first method first determines all normal years, and then the remaining years are leap years. The second method first determines all leap years, and then the rest are normal years.
The if statement uses Boolean algebra.
So when we reverse the logic, we run into the De Morgan formula problem.
A simple example is:
In a computer test, the scores range from 0 to 100. Now the number of people with scores from 60 to 80 is counted. The statistical method is to select the number of people with scores greater than 60 and scores less than 80.
If we want to count people other than the above scores, we need to count the number of people whose scores are less than or equal to 60 or whose scores are greater than or equal to 80.
When reversing the logic, not only reverse the and and or, but also reverse the logical sign (greater than becomes less than or equal, greater than or equal to less than, less than becomes greater than or equal, less than or equal to greater than) This is De Morgan formula.
This is reflected in the above example of judging leap years in ordinary years.
If statements can be nested:
It is possible to use multiple levels of if for nesting, but doing so clutters up the code, so don't do it if it's not necessary.
loops
There are two kinds of loop statements in python, while and for.
while loops
For code clarity, the while loop is only used as an infinite loop.
An infinite loop is a loop that does not stop. This loop will continue to run until an error occurs in the program or is terminated manually or a break statement is read.
In a computer program, if a program is terminated, the program will return to the operating system, but if there is no operating system in an embedded device or a microcontroller, the program must be written as an infinite loop, otherwise the program in the microcontroller will run away ( go where it shouldn't).
This is a complete while program. In most cases, while is an infinite loop, and there will be no break out of the process.
break
break can terminate the loop. There is usually a judgment condition before the break (otherwise, the loop will be terminated as soon as it reaches the break, and the loop will be meaningless)
Check the program just now, break is executed when i is greater than 10, so when i is equal to 11, that is, when i is greater than 10, the loop is aborted.
continue
Continue is another loop control condition other than break.
If the program executes to continue, the current round of the loop is terminated and the loop will enter the next round.
When the program runs to 4, the program enters the continue state, then this round of 4 is skipped, as in the following output.
for loops
In order to make the program look simple, in general, the while loop is only used as an infinite loop, and the for loop assumes other loop tasks.
You will see that the for loop prints each item in the list.
Loops can be controlled more easily with ranges:
This way we can print out the numbers in the range.
Python is a flexible language, so we can use for to construct arrays:
We can also add calculations to it:
Similarly we can construct dictionaries using this method:
A basic operation is: if we need to manually write an FFT (fast Fourier transform) program, then a large amount of sin and cos data needs to be calculated, but we can calculate it in advance and call these data later. Because it takes a lot of time to calculate functions like sin, but it takes almost no time to call, we can do the repeated calculation in advance, so that there is no need to repeat the calculation.
Note that sin and cos in the math library in python use radians, so your data should also be in radians.
The cos table can also be calculated using a similar method.
After these chapters, you have mastered the Turing-complete python language, and you can start practicing programming!
Statistics
Start time of this page: January 13, 2022
Completion time of this page: January 13, 2022
Last updated