Algorithmic thinking with python
Algorithmic thinking with python
Module 3
Page 2
Module 3
► SELECTION AND ITERATION USING PYTHON:- if-else, elif, for loop, range, while loop.
► SEQUENCE DATA TYPES IN PYTHON - list, tuple, set, strings, dictionary, Creating and
using Arrays in Python (using Numpy library).
Page 2
1
19-11-2024
Page 3
The body of the if must be indented and every line in this section of code must be
indented the same number of spaces. By convention, four space indentation is used
in Python. Most Python code editors automatically indent code after if-statements
Page 4
2
19-11-2024
if <condition>:
# block of code if condition is True
else:
# block of code if condition is False
Page 5
► Example
number = 10
The keyword else should be on its own line and at the same
if number > 0: indentation level as the if statement it belongs to. The else must be
followed by a colon (:). Any code inside the else block should be
print('Positive number') indented by the same amount.
else:
print('Negative number')
print('This statement is always
executed')
Page 6
3
19-11-2024
Multi-Way if Statements
Page 7
Multi-Way if Statements
if <condition1>:
# code block 1
In Python, the elif keyword is used to implement the "else if"
elif <condition2>: statement. It allows you to check multiple conditions after an
# code block 2 if statement, and it stands for "else if."
else:
# code block 3
Page 8
4
19-11-2024
Multi-Way if Statements
► Example
number = 0
if number > 0:
print("Positive number")
elif number == 0:
print('Zero')
else:
print('Negative number')
print('This statement is always executed')
Page 9
Exercises
► Write a Python program that prompts the user to enter a number and
checks if the number is even or odd. If the number is even, print "The
number is even." If the number is odd, print "The number is odd."
Page 10
5
19-11-2024
Exercises
► Write a Python program that takes two numbers as input from the user
and prints the larger of the two numbers. If both numbers are equal,
print "The numbers are equal."
Page 11
Exercises
► Write a Python program that takes a student's marks as input and prints
their grade based on the following criteria:
► Marks >= 90: A
► Marks 80–89: B
► Marks 70–79: C
► Marks 60–69: D
► Marks < 60: F
Page 12
6
19-11-2024
Exercises
► Write a Python program that takes a single character as input from the
user and checks if it is a vowel or a consonant. If the input is not an
alphabetic character, print "Invalid input."
Page 13
Exercises
► Write a Python program that takes a year as input and checks if the year
is a leap year.
Page 14
7
19-11-2024
Exercises
Page 16
8
19-11-2024
Repetition statements
► Python’s for loop, the control statement that most easily supports
definite iteration.
for <variable> in range(<an integer expression>):
<statement-1>
. Loop header
. Loop body
<statement-n> The colon (:) ends the loop header
Page 17
Page 18
9
19-11-2024
# Solution
for i in range(6): # range(6) generates numbers from 0 to 5
print(i)
Page 19
# Solution
for i in range(3,20,2): #range(start, stop, step)
print(i)
Page 20
10
19-11-2024
Page 21
Exercises
Page 22
11
19-11-2024
while <condition>:
<sequence of statements>
Page 23
Page 24
12
19-11-2024
► Python provides three loop control statements that control the flow of
execution in a loop.
► break statement
► continue statement
► pass statement
Page 25
► The break statement is used to immediately exit a loop, even if the loop
condition is still true.
► When a break is encountered inside a loop, the control jumps out of the
loop and the program continues with the next statement following the
loop.
# Example: Find the first number greater than 5 and stop the loop
for num in range(1, 10):
if num > 5:
print(f"First number greater than 5 is: {num}")
break # Exit the loop when the first number greater than 5 is found
Page 26
13
19-11-2024
► The continue statement in Python is used to skip the rest of the code
inside the current loop iteration and move on to the next iteration of the
loop.
► Unlike break, which exits the loop completely, continue only skips the
current iteration and allows the loop to proceed.
# Example: Skip printing even numbers and only print odd numbers
for num in range(1, 10):
if num % 2 == 0:
continue # Skip the rest of the loop for even numbers
print(num)
1
3
Output 5
7
9
Page 27
Page 28
14
19-11-2024
Enter a number 10
10 is positive.
Output This message will always print after the condition checks.
Enter a number0
This message will always print after the condition checks.
Page 29
► Functionality
► pass: Does nothing; serves as a placeholder.
► continue: Skips to the next iteration of the loop.
► Use Case
► pass: Useful when you need a block of code but haven't implemented it yet.
► continue: Useful when you want to ignore certain conditions within a loop and
proceed with the next iteration.
► Behaviour
► pass: The program continues executing subsequent lines of code.
► continue: The program jumps to the next iteration of the loop, skipping the
remaining code in the current iteration.
Page 30
15
19-11-2024
Exercises
Page 31
Exercises
Page 32
16
19-11-2024
Exercises
► Write a Python program that takes a number as input from the user and
finds the factorial of that number using a while loop.
Page 33
Random Numbers
2 4 6 4 3 2 3 6 2 2
Page 34
17
19-11-2024
Guessing game.
Page 35
Guessing game.
import random
smaller = int(input("Enter the smaller number: "))
larger = int(input("Enter the larger number: "))
myNumber = random.randint(smaller, larger)
count = 0
while True:
count += 1
userNumber = int(input("Enter your guess: "))
if userNumber < myNumber:
print("Too small!")
elif userNumber > myNumber:
print("Too large!")
else:
print("Congratulations! You've got it in", count,"tries!")
break
Page 36
18
19-11-2024
References
► https://www.freecodecamp.org/news/python-range-function-example/
► https://www.w3schools.com/python/ref_func_range.asp
Page 37
19