0% found this document useful (0 votes)
10 views

Lab 05

Uploaded by

hsiddiqui011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lab 05

Uploaded by

hsiddiqui011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Applications of ICT

Lab #5
Dr Asad Mansoor Khan
LE Kashaf Raheem
QUIZ 3
EVEN SEAT ODD SEAT
Write a Python Write a Python
program to display program to display
all numbers all numbers
between 100 and between 100 and
250 where the 250 where the sum
product of their of their digits is
digits is greater greater than 10.
than 10.
2
Algorithms and
Repetition
Aim
Exploring python syntax and problem-solving w.r.t repetition
structures

3
Python Syntax
for and more

4
Range Function
• Creates a list of integer values usually used to
iterate in a for loop

• range(n) will give [𝟎, 𝟏, 𝟐, … . , (𝒏 – 𝟐), (𝒏 – 𝟏)]

• Startingvalue and increment/decrement can


be provided

6
Range function
• range(a, b, c)
a = Starting value, default value is 0
b = Terminating condition, must always
be given
c = Increment/decrement, default value
is +1

7
Example:
Question Solution

Write a program that for i in range(1, 100):


prints numbers from 1- print(i)
100

for i in range(1, 100, 2):


print(i)
Write a program that
prints numbers from 1-
100 by a jump of 2
9
FOR Statement
myList = ["a", "b", "c", "d", "e"]
for i in myList:
print i
for i in range( len( myList ) ):
if myList[i]=="c":
myList[i]=None
#Results in myList???

• Can “break” out of for-loops.


• Can “continue” to next iteration.

10
Example:
Question Solution

Write a
original_str = input("Enter a string to
be revered: ")

program that Len = len(original_str)

rev_str = ""
reverses a for i in range(1, Len+1):

string entered i]
rev_str = rev_str + original_str[Len -

by user print("Reversed String is: ", rev_str)

11
Tasks
Make sure to comment your code.
Use appropriate variable name
Know the location where file is being saved
Create different files for different tasks
14
Task 1:
a) Write a program in python that gets
a string from user and checks whether
the user entered data is a palindrome
or not. E.g.,
 User enters: LEVEL, It is a palindrome as Reverse of
this remains LEVEL
 User enters: 12344321, It is a palindrome as Reverse
of this remains 12344321
b) Edit the code such that it re-runs until
user enters No or Quit

15
Note: The characters are
arranged in descending
Task 2: order

You are required to create a python program


which can convert numbers entered in ROMAN
Numerals into decimal numbers. E.g., user
entered MLXVI number becomes 1066
(1000+50+10+5+1) ROMAN Decimal
Numeral Value
M 1000
D 500
C 100
L 50
X 10
V 5
I 1 16
Task 3:
You are required to create a python program for
calculating Combinations and Permutations using the
following formulae:
𝑛!
Combinations: 𝑛
𝑟
= 𝑟!⋅ 𝑛−𝑟 !
𝑛!
Permutations: 𝑃𝑟 =
𝑛−𝑟 !

The factorial in the above is calculated as:


𝑛

𝑛! = ෑ 𝑖
𝑖=1

For example: 5! = 1*2*3*4*5 = 120

17
Next week: Python
Syntax and Problem
Solving!
Advice:
Explore Leetcode for problem solving!

19

You might also like