Categories :

Loops, conditions and exceptions in Python Part 1

For a start, something I will explain. The blog is called “I learn to program” and not “I teach program” — in other words, this is not a tutorial on programming. I’m no expert and don’t have the skills of the teacher, but because if you can take it upon themselves to teach someone, it is only yourself.

The blog has two purposes. The first is personal: I want to systematize and consolidate the knowledge that I have. As you know, one of the best ways to learn is to teach and explain to others what I know. That is why I would love to read reviews and indications of errors (last time, by the way, valuable comments in our Facebook). The second goal is advocacy. I sincerely believe that now to learn programming can anyone, and I want to show by example: time editor of Look At Me can write code, it can write anyone.

Image result for loops in programming

Note: All code is provided for Python versions 3.0 and older, and therefore may not work on older versions.

Last time we wrote a simple program that calculates the square of the entered integer. In the end I called two drawbacks of the program. First, it stops working immediately after it displays the result. Second, it is not protected from inaccuracies in the input: if we enter not a number, a letter, the program will show error and closes. Let’s start with the first flaw. To eliminate it, we introduce the notion of cycle — design, which allows you to perform the same set of operations as many times as desired — if desired, even indefinitely.

def square(int):

return int * int

while True:

number = int(input(“Enter an integer: “))

print(“The square of {} is {}.”.format(number, square(number)))

Compared to the code in the previous issue of the blog, I made only one change: put the last two lines in the unconditional loop while True:. Instructions executed by the loop are placed in a code block indented by four spaces (PEP 8 standard) — just like the contents of a function. The a loop function as follows:

The interpreter sees the keyword while and a condition which is specified after it. In our case this condition is True the data type boolean. To boolean is also False. In other words, this is a Boolean type which can have only two values — true or false. The words while and True to form a kind of idiomatic construction, telling the interpreter: follow the instructions inside the block indefinitely — if only something do not interrupt.

The interpreter executes the statements inside the block in order from the first row to the last.

Image result for loops in programming

The interpreter sees that a code block has ended, and checks whether the condition specified after the while keyword. If it is true, the interpreter returns to the first line of the block (note that block, not the whole program) and made to order. If the condition is not met, the loop exits, and the interpreter jumps to the line that immediately follows the last line of the block. In our case the condition will be executed always, regardless of what is happening inside the unit. But in theory we could specify another condition, for example, to make the design work only three times:

i = 0

while i < 3:

number = int(input(“Enter an integer: “))

print(“The square of {} is {}.”.format(number, square(number)))

i++

I have introduced a variable i and assigned it a value of 0, and therefore it will be of datatype int. Next, I put in the loop a new line i++. ++ is the increment operator (increment): after the execution the value of variable i is added 1. Recording i++ is equivalent to write i = i + 1, it’s just a shorter version. Thus, after the third execution of all instructions of the block the value of i reaches 3, the condition i < 3 (i less than three) will cease to run and the cycle is complete.