Loops
Conditional Statements
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
We almost always need the ability to check the condition and then change the course of program of what needs to be done, and the simplest way to do so is using conditional statements.
3.1 If Statement
Conditional operations can be performed in Python using if statements.
An "if statement" is written by using the if keyword.
INDENTATION IS MANDATORY.
Unlike C++, Python relies on indentation, using whitespace, to define scope in the code whereas most other programming languages often use curly-brackets for this purpose. Not indenting your code properly will lead to syntactical errors.
elif statement
The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".
else statement
The else keyword catches anything which isn't caught by the preceding conditions. No parameter can be given along with the else keyword.
Looping Constructs
We know that computers are often used to automate the repetitive tasks. One of the advantages of using computer to repeatedly perform an identical task is that it is done without making any mistake. Loops are used to repeatedly execute the same code in a program. Python provides two types of looping constructs:
while loop
for loop
3.2 While loop
With the while loop we can execute a set of statements as long as a condition is true.
Syntax: while expression: statement(s)
Note: remember to increment i, or else the loop will continue forever.
The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1.
break statement
With the break statement we can stop the loop even if the while condition is true:
continue statement
With the continue statement we can stop the current iteration, and continue with the next:
pass statement
The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet
3.3 for loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
This is less like the for keyword in other regular programming language, and works more like an iterator method as found in other object-orientated programming languages.
Even strings are iterable objects, they contain a sequence of characters:
for loops are the most efficient looping constructs available in Python. They can iterate through almost anything.
They can iterate through lists as well.
An alternative way of iterating through each item is by index offset into the sequence itself.
With the break statement we can stop the loop before it has looped through all the items:
Exit the loop when x is "banana":
With the continue statement we can stop the current iteration of the loop, and continue with the next:
Do not print banana:
The range() Function
We saw the range() function in Chapter 2 when we studied about Built-in functions. It is basically used to specify an arithmetic progression during iteration. To loop through a set of code a specified number of times, we can use the range() function.
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.
The value for x starts as 0, as indexing starts at 0 in Python, and ends at the stop value minus 1. Like in the above example it stops at 9 since its the 10 iteration.
Using else in a for loop?
The else keyword in a for loop specifies a block of code to be executed when the loop is finished:
3.4 Nested Loops
A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for each iteration of the "outer loop":
Syntax:
You can put any type of loop inside of any other type of loop. For example a for loop can be inside a while loop or vice versa.
3.5 Recursion
Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.
The developer should be very careful with recursion as it can be quite easy to slip into writing a function which never terminates, or one that uses excess amounts of memory or processor power. However, when written correctly recursion can be a very efficient and mathematically-elegant approach to programming.
Last updated