Vectors
Vectors
(Python)
D e p a r t m e nt O f C o m p u te r Sc i n e c e ,
Fac ulty Of Comp uting A nd Mathematic al S c ienc es,
K a n o U n i ve r s i t y O f S c i e n c e A n d Te c h n o l o g y, Wu d i l K a n o – N i ge r i a .
C re d i t T o W 3 S c ho ol s , m a i n s o ur c e o f t h e se s l i de s
Contents
❑ Python If ... Else
❑Python While Loops
❑Python For Loops
❑ Looping Through a String
❑The range( ) Function
❑ Else in For Loop
Python If ... Else
Python Conditions and If statements
Python supports the usual logical conditions from mathematics:
• 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
These conditions can be used in several ways, most commonly in "if statements" and loops.
An "if statement" is written by using the if keyword.
In this example we use two variables, a and b, which are used as part of the if statement to test whether
b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen
that "b is greater than a".
Elif
The elif keyword is pythons way of saying "if the previous conditions were not true, then try this
condition".
In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to
screen that "a and b are equal".
Else
The else keyword catches anything which isn't caught by the preceding conditions.
In this example a is greater than b, so the first condition is not true, also the elif condition is not true,
so we go to the else condition and print to screen that "a is greater than b".
You can also have an else without the elif :
Short Hand If
If you have only one statement to execute, you can put it on the same line as the if statement.
Short Hand If ... Else
If you have only one statement to execute, one for if, and one for else, you can put it all on the
same line:
You can also have multiple else statements on the same line:
And
The and keyword is a logical operator, and is used to combine conditional statements:
Or
The or keyword is a logical operator, and is used to combine conditional statements:
Nested If
You can have if statements inside if statements, this is called nested if statements.
The pass Statement
if statements cannot be empty, but if you for some reason have an if statement with no content,
put in the pass statement to avoid getting an error.
Exercise:
Print "Hello World" if a is greater than b.
Python While Loops
Python Loops
Python has two primitive loop commands:
• for loops
• while loops
The while Loop
With the while loop we can execute a set of statements as long as a condition is true.
Exercise:
Print i as long as i is less than 6.
Python For Loops
Python 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 programming languages, and works more like an iterator
method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
With the for loop does not require an indexing variable to set beforehand.
Looping Through a String
Even strings are iterable objects, they contain a sequence of characters:
The break Statement
With the break statement we can stop the loop before it has looped through all the items:
The continue Statement
With the continue statement we can stop the current iteration of the loop, and continue with
the next:
The range( ) Function
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 range( ) function defaults to increment the sequence by 1, however it is possible to specify the
increment value by adding a third parameter: range(2, 30, 3) :
Else in For Loop
The else keyword in a for loop specifies a block of code to be executed when the loop is
finished:
Note: The else block will NOT be executed if the loop is stopped by a break statement.
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":
The pass Statement
for loops cannot be empty, but if you for some reason have a for loop with no content, put in
the pass statement to avoid getting an error.