Skip to content

[Python] Tutorial (3) for-loop, if-else

Last Updated on 2021-03-29 by Clay

Recording these basic Python language is only for beginners who are fully linguistic. It should be said that the essays I write are completely new to the beginners.

People with experience may not be suitable for such simple teaching. (I'm sorry!)

If you are an experienced person, and you want to learn Python more, maybe you can check out of my other Python notes I've written, such as tips on various packages ... and more.

If you want to quickly learn the basic syntax of the Python programming language (about for-loop and if-else), it is highly recommended that Python's official Tutorial: https://docs.python.org/3.6/tutorial/


for-loop

"for-loop", also known as the "loop" (like "while-loop"), refers to a function that can repeatedly let the program handle the instructions for us.

First, let's take a look at a simple sample code:

for n in range(10):
    print(n)


The instruction print() we mentioned in the previous tutorial. That allows us to print the results on the terminal or screen.

In here, we can print the n variable, and observe the trend of n.

Output:

0
1
2
3
4
5
6
7
8
9

It's a "for-loop" in Python.

The variable "n" is a variable we set to iterate over.
Range is the interval we set to iterate. The preset starts from 0 and stop as 10.

So the value we print is every number in the range 0-9.


If-else

The meaning of "if-else", as the name suggests, we can understand that this is a condition we set in the code.

If it meets the conditions set by "if", then we execute the program under if code.

Let's take a look at the actual code:

a = 3
if a == 3:
    print('good!')


Output:

good!

First, we set the variable "a" to 3, and we create a conditional judgment: if a == 3.

The meaning is, if the variable a is equal to 3, we will execute the code after "indentation" is executed.

Yeah! Python is very different from other programming language. If we want to execute the code in the conditional judgment (or for-loop), we don't use brackets "{}" to code the code like C and Java. Live, but directly to indent or not to distinguish!

And then we get a simple example:

a = 3
if a == 3:
    print('good!')

print('Nice!')


Output:

good!
Nice!

We change our code like that:

a = 3
if a == 4:
    print('good!')

print('Nice!')


the output is changed.

Nice!

This is because we have changed the value of the variable a, so that the if-judgment we code doesn't match, and we will not print the part of "good"!

The part of "Nice" is not part of the conditional judgment because it is not indented, but the part that Interpreter normally performs.

Of course, in addition to set a condition to match, we can also set a variety of conditions at the same time to determine which code we should execute.

Like this example below:

for n in range(1, 10):
    if n % 3 == 0:
        print(n, 'Divided by 3')
    elif n % 3 == 1:
        print(n, 'After being divided by 3, the remaining 1')
    else:
        print(n, 'After being divided by 3, the remaining 2')


The "elif" is the meaning of "else if", which means that we set the second conditional judgment:

When the first conditional judgment is not match, the second condition is automatically started; If the second condition is not match, the third condition will be automatically judged ...... and so on.

The "else" refers to the code block that will be executed if the above conditions are not match.

Lets take a look at the results printed after execution:

 1 After being divided by 3, the remaining 1
 2 After being divided by 3, the remaining 2
 3 Divided by 3
 4 After being divided by 3, the remaining 1
 5 After being divided by 3, the remaining 2
 6 Divided by 3
 7 After being divided by 3, the remaining 1
 8 After being divided by 3, the remaining 2
 9 Divided by 3

Have you printed the results according to the conditions we set?

The above is a complete explanation of Python's for-loop and if-else. You may wish to try and write different situations. Believe me, this is very interesting!


Read More

Leave a ReplyCancel reply

Exit mobile version