Software Tech

Introduction To Python Programming – part 4

Hello, and welcome to Part 4 of the series “Introduction to Python Programming.” If you have not gone through

Introduction To Python Programming – part 4

Hello, and welcome to Part 4 of the series “ to Python .” If you have not gone through the previous episode, kindly find the links below.

Introduction to Python programming – part 3
Introduction to Python programming – part 2
Introduction to Python programming – part 1

Python If … Else

Python Conditions and If statements
We can use logical operations in Python

Equals: a == b
Not Equals: a != b


Greater than: a > b
Greater than or equals to: a >= b

These conditions can be used in IF statements and LOOPS

If Statements

We write if statements using the IF keyword

a = 120
b = 2000
if b > a:
print(“b is greater than a”)
#We used to check which number is greater between a and b

Indentation

Indentation (whitespace) is very important. Without proper indentation in Python, the code will not work.
If statement without indentation, it will raise an error.


fig1: An image showing a proper indentation in Python

Elif

The elif keyword is used for running the second block of code after the if statements. You are simply telling Python to run this code in the elif block if the previous was not met.

a = 200
b = 200
if b > a:
print(“b is greater than a”)
elif a == b:
print(“a and b are equal”)

”'
In this example the elif code block will
run because the IF condition is not met.
”'

Else

The else block will run when the two previous conditions are NOT met.

yam = 24000
Potato = 20000

if Potato > yam:
print(“Potato is more expensive than yam”)
elif Potato == yam:
print(“The two prices are equal”)
else:
print(“Yam is more expensive than potato”)

As the yam is greater than the potato in this example, the first condition is false as well as the elif condition; therefore, we move on to the else condition and print “Yam is more expensive than potato” on the screen.
NOTE: You can have an else block without elif

a = 360
b = 120

if b > a:
print(“b is greater than a”)
else:
print(“b is not greater than a”)

Short Hand If

If there is only one sentence that needs to be executed, it can be placed on the same line as the if statement.
One line IF statement

a = 20
b = 400

if a > b: print(“Python is the best”)

Short Hand If … Else
You can put both the if and else statements on the same line if you only need to execute one statement:
One line if else statement

a = 20
b = 400

print(“This is awesome”) if a > b else print(“We move”)

Additional else statements may appear on the same line:

a = 200
b = 200

print(“My Name is akinnimi stefan”) if a > b else print(“I programming”) if a == b else print(“You will love it here also”)

And

And is a logical operator. It is used in Python to combine and compare two statements.
Check if yam is greater than potato AND if yam is greater than onions

yam = 4000
potato = 2500
onions = 500

if yam > potato or yam > onions:
print(“The yam is more expensive than potato and onions”)

Or

OR is a logical operator. It is used in Python to combine and compare two conditional statements.

Rice = 4000
Noodles = 2500
Oats = 5000

if Rice > Noodles or Rice > Oats:
print(“The Rice is more expensive than either Noodles or Oats”)

Not

NOT is a logical operator. It is used in Python to reverse conditional results.

a = 400
b = 2500
c = 500

if not a > b:
print(“a is NOT greater than b”)

Nested If

You can put an IF statements inside of another IF statements. This is called Nesting. Just sure to indent it properly.

a = 900

if a > 850:
print(“A is Above eight-hundred”)
if a > 850:
print(“A is Above eight-hundred and fifty,”)
else:
print(“A is below eight-hundred.”)

The pass Statement

Add the pass statement if your if statement is empty for whatever reason to avoid receiving an error. If clauses can't be left empty.

a = 33
b = 200

if b > a:
pass

Python Loops

Python has two primitive loop commands:
while loops
for loops

The while Loop

The while loop allows a series of statements to be executed while a condition is true.

#As long as i is less than 10, the code will continue to run.
i = 1

print(i)
i += 1

NOTE: Unless you remember to increase i, the cycle will never end.

The break Statement

The break statement is used to stop the loop even when the while condition is true.

#Exit the loop when i is 3
i = 1

print(i)
if i == 5:
break
i += 1

The continue Statement

The condition statement is used to stop the iteration and continue with the next

#Continue to the next iteration if i is 10
i = 0

i += 1
if i == 10:
continue
print(i)

The else Statement

The else statement is used to run a block of code when the condition is no longer true.

#Whenever the condition is false, print a message.
i = 1

print(i)
i += 1
else:
print(“i is no longer less than ten”)

Python For Loops

The allows us to run a series of instructions once for each element of a list, tuple, set, etc.

#Print each fruit in a colour list
colours = [“red”, “blue”, “purple”, “green”]

for x in colours:
print(x)

Looping Through a String

Strings are iterable objects since they are made up of a series of characters:

#Looping thorugh the chatracters in the word “purple”
for x in “purple”:
print(x)

The break Statement

The loop is terminated with the break statement before it has iterated over all of the objects.

#Exit the loop when x is white
colours = [“red”, “blue”, “purple”, “white”, “green”, “orange”]

for x in colours:
print(x)
if x == “white”:
break

Exit the loop when x is “white”, but this time the break comes before the print

colours = [“red”, “blue”, “purple”, “white”, “green”, “orange”]

for x in colours:
if x == “white”:
break
print(x)

The continue Statement

The continue statement is used to stop a current block of code and continue the next one

#Do not print blue
colours = [“red”, “blue”, “purple”, “white”, “green”, “orange”]

for x in colours:
if x == “blue”:
continue
print(x)

The range() Function

The range() function can be used to repeatedly loop through a block of code.
The range() function returns a series of numbers that, by default, starts at 0 and increments by 1 before stopping at a predetermined value.

for x in range(6):
print(x)

NOTE: The range in this case is not the values of 0 to 6, but the values 0 to 5

The initial value for the range() function is 0 by default, but a starting value can be specified by adding a parameter: range(3, 7), which indicates values 3 to 7 (but excluding 7):

#Using the starting parameter
for x in range(3, 7):
print(x)

By default, the range() function increases the series by 1, but a third parameter can be used to provide a different increment amount, as in range(2, 20, 2):

#The sequence will be advanced by 2 (the default is 1).
for x in range(2, 20, 2):
print(x)

Else in

When using a for loop, the else keyword designates a piece of code that will run after the loop has finished:

#Print all numbers from 0 to , and print a message when the loop has ended

for x in range(10):
print(x)
else:
print(“Finished counting!”)

If a break statement is used to end the loop, the else block will not be executed.

#Break the loop when x = 5, and see what happens with the else block
for x in range(10):
if x == 5: break
print(x)
else:
print(“finished counting”)

Nested Loops

The ability to nest a loop inside another loop is what makes Python very powerful.
The “inner loop” will only be executed once for each iteration of the “outer loop”:

#Print each colour for every fruit
colour = [“red”, “green”, “white”]
fruits = [“apple”, “banana”, “cherry”]

for x in colour:
for y in fruits:
print(x, y)

The pass Statement

For some reason, if there is an empty for loop, add the pass statement to prevent an error from occurring. For loops cannot be empty.

for x in [0, 1, 2]:
pass
About Author

Akinnimi Stefan Emmanuel

1 Comment

  • hey

    lovely blog hope all is well
    happy blogging.

Leave a Reply

SOFAIO BLOG We would like to show you notifications for the latest news and updates.
Dismiss
Allow Notifications