Programming Fundamentals Lab 4
Programming Fundamentals Lab 4
Lab Objectives:
1. Simple if condition
2. If with else condition
3. Simple loop
4. Nesting with condition and loop
In general, statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on. There may be a situation when you need to execute a block of code
several number of times.
Programming languages provide various control structures that allow for more complicated execution
paths.
A loop statement allows us to execute a statement or group of statements multiple times. The following
diagram illustrates a loop statement −
Programming Fundamentals 1
Lab 04 – Conditional Statements and Simple Loops
1. Simple if Condition
Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need
to determine which action to take and which statements to execute if outcome is TRUE or
FALSE otherwise.
Following is the general form of a typical decision making structure found in most of the programming
languages.
Programming Fundamentals 2
Lab 04 – Conditional Statements and Simple Loops
Python programming language assumes any non-zero and non-null values as TRUE, and if it is either zero
or null, then it is assumed as FALSE value.
Program 1: Practicing with simple if condition. Execute the following program with a = 400 and b =
500. Then change the value of a =500 and b = 400.
Code:
a = 400
b = 500
if a == 400:
print("The value of a is equal to 400")
if a < b:
print("The value of a is less than b")
if a > b:
print("The value of a is greater than b")
Programming Fundamentals 3
Lab 04 – Conditional Statements and Simple Loops
Output 2:
Programming Fundamentals 4
Lab 04 – Conditional Statements and Simple Loops
Code:
a = 10
b=5
c = 20
Output:
Program 3: Practicing with simple if condition if any of the condition is true using OR operator.
Code:
a = 105
b = 50
c = 200
if a > b or a > c:
print("At least one of the conditions are True")
Output:
An else statement can be combined with an if statement. An else statement contains the block of code that
executes if the conditional expression in the if statement resolves to 0 or a FALSE value.
The else statement is an optional statement and there could be at most only one else statement following if.
Programming Fundamentals 5
Lab 04 – Conditional Statements and Simple Loops
Program 4: Write a program which takes the lower limit and upper limit then find which of the number
are prime number.
Code:
# Python program to display all the prime numbers within an interval
if number > 1:
for i in range(2,number):
if (number % i) == 0:
break
else:
print(number)
Output:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 6
Lab 04 – Conditional Statements and Simple 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 language, 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.
Program 5: Write a program which takes the initial and final values from the user then print the sum of
all the number.
Code:
initial_value = eval(input("Enter the initial value for the range :"))
final_value = eval(input("Enter the final value for the range :"))
numbers = range(initial_value,final_value)
sum = 0
Output:
Program 6: Write a program which takes the number of rows and columns from the user and generate the
values in form of list.
Code:
print(multi_list)
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 7
Lab 04 – Conditional Statements and Simple Loops
Program 7: Write a program which will check the data type of given data in a loop.
Code:
datalist = [300, 12.65, 5+6j, True, 'Faisal', (5, -7), [8, 52],{"color":'blue', "color":'red'}]
for item in datalist:
print ("Type of ",item, " is ", type(item))
Output:
Code:
print("\t\t\t ASCII Character")
Output:
Programming Fundamentals 8
Lab 04 – Conditional Statements and Simple Loops
Code:
print("Python program to convert decimal number into binary, octal and hexadecimal number
system")
Output:
For Example:
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
The syntax for a nested for loop statement with if condition in Python programming language is as
follows −
Programming Fundamentals 9
Lab 04 – Conditional Statements and Simple Loops
else:
statements(s)
statements(s)
A final note on loop nesting is that you can put any type of loop inside any other type of loop. For
example, a for loop can be inside another for loop or inside condition or condition may be called inside a
loop and vice versa.
Program 10: Write a Python program to construct the following pattern, using a nested for loop.
*
**
***
****
*****
****
***
**
*
Code:
n=5;
for i in range(n):
for j in range(i):
print ('* ', end="")
print('')
for i in range(n,0,-1):
for j in range(i):
print('* ', end="")
print('')
Programming Fundamentals 10
Lab 04 – Conditional Statements and Simple Loops
Program 11: Write a program which calculates the vowels from the given string.
Code:
print("This program will count total number of vowels from user defined sentence")
string=input("Enter your string:")
vowels=0
for i in string:
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U'):
vowels=vowels+1
print("Number of vowels are:")
print(vowels)
Output:
Programming Fundamentals 11
Lab 04 – Conditional Statements and Simple Loops
Programming Exercise
1. Write a program which solves the quadratic equation. The user will enter the value of a, b and c.
The program will then check the denominator that if denominator is zero or not. If its zero it can
reply the equation cannot solve as there is a zero division else, it will execute the program and
will generate two solutions.
2. Calculate the arithmetic sequence of n numbers. The program will generate the nth term of the
sequence, whereas the user will enter the first term and the common difference. The program will
then ask either to continue or not, if the user will enter yes it will ask the next nth term to
calculate. Example: you have entered the first term as 3 and common difference 6 you are
interesting in 35th term. So it will calculate and generate the answer as 207. Now it will again ask
for you to continue if you agree it will ask next term like 45 th or 96th term to calculate.
= 𝑎1 + (𝑛 − 1)𝑑
3. Write a program which will check either the giving string is Palindrome or not. Palindrome is a
string when we reverse the string it will generate the original string. Example CIVIC, MOM, 010,
1001, etc. So if you enter the word which is Palindrome it will say yes your string is Palindrome
otherwise it will generate sorry message.
[Hint: As user may enter upper or lower case, so you may use myString.casefold(). A casefold() will
remove all the upper and lower case problems. To generate the reverse of string you can use
reversed() function on your string.]
4. Write a program which will collect your name, your father’s name, your roll number and your
subjects (5 Subjects with name and numbers). At the end it will generate a result with your name,
your father’s name, your details subjects, marks you have obtained with total marks with grade
and percentage.
5. Generate a table from initial value to final, depending upon the user starting and ending range in
matrix form such as:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15-
4 8 12 16 20
5 10 15 20 25
Programming Fundamentals 12
Lab 04 – Conditional Statements and Simple Loops
[Hint: The following operation will be helpful for you when multiplying matrices.
Programming Fundamentals 13