ICP
ICP
Submitted by:
Roll no: 2022-ME-141
Degree program: BS
Mechanical
Engineering
Secton:
Submitted to:
Professor Waseem
Submission date:
19-01-2023
Python If….else statement
# If the number is positive, we print an appropriate message
num=3
if num>0:
print(num,"is a positive number")
print("This is always printed")
num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")
num=3
if num>=0:
print("Positive or Zero")
else:
print("Negative number")
num=5
if num>0:
print("Positive number")
if num==0:
print("Zero")
if num<0:
print("Negative number")
num=-5
if num>=0:
print("Positive or Zero")
else:
print("Negative number")
# In this program,
num=3.4
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
# In this program, we input a number
# an appropriate message
num=float(intput("Enter a number:"))
if num>= 0:
if num== 0:
else:
print("Negative number")
Lab Manual
Write your first code of python language on your jupyter notebook by declaring
a variable.
marks=850
print(marks)
Write the above code on your computer editor to store the value in the variable
percentage=70.78
print(percentage)
Write The above code on your computer editor to check the value stored in the
variable.
number=400.6
print(number)
Write the above code on your computer editor to store the value in the variable
letter='C'
print(letter)
Write the above code in your computer to declare a char type variable in your
Program
myname='Iqra'
print(myname)
studentname='Qaiser'
print (studentname)
Write a python program that prints a box using stars (*)
print("****")
print("* *")
print("* *")
print("****")
Write the above code on your editor to get the value of the “name” variable from the user.
Hint: I = Q / t
Assume that Ali is a student who wants to calculate his aggregate for taking admission in UET.
we shall use a computer program that would take his obtained marks and after processing, it
Firstly, we shall need to ask Ali about his marks and store those values somewhere so we can
number=int(input("Enter a number:"))
print("First three multiples of", number,"are:", number, number+number , number+nu
mber+number)
Write a python program to print the first three multiples of two given numbers
of a sequence, we call it a series. Thanks to Gauss, there is a special formula we can use to
The following example prints "Hi Python!" 100 times to the console.
count = 0
while count < 100:
print("Hi Python!")
count= count+1
print("Done!")
Write a Python program that prints the numbers from 0 to 9.
count=0
while count<10:
print(count)
count=count+1
print("Done!")
print(count)
Write a Python program that prints the sum of numbers from 1 to 20.
sum=0
i=1
while i<=20:
sum=sum+i
print (i)
i=i+1
print("sum is",sum)
The for Loop
A for loop iterates through each value in a sequence. for loop is a counter-controlled loop
Suppose the input is 2 3 4 5 0 (one number per line). What is the output of the following code?
number=0
sum=0
for count in range(5):
number=int(input("Enter an integer:"))
sum += number
print("sum is", sum)
print("count is", count)
Break Statement
You can use the keyword break in a loop to immediately terminate a loop. break could be used in a while or for
loop
The following example illustrates the using of break keyword, if the sum of numbers is greater than or equal to
100 then break the loop and print the result
sum=0
number=0
while number<20:
number+=1
sum+=number
if sum>=100:
break
print("The number is", number)
print("The sum is", sum)
Continue Statement
You can use the continue keyword in a loop skip all the remaining statements in the current iteration. When it is
encountered, it ends the current iteration and program control goes to the end of the loop body. In other words,
continue keyword breaks out of an iteration, while the break keyword breaks out of a loop. continue could be
used in a while or for loop.
The following example prints the numbers from 0 to 100 except those numbers that divide by7
for i in range(100):
if i%7==0:
continue
print(i)
In this example, the condition is always True, so the loop never ends. To solve this problem, we need to
increment count variable every iteration, to make the condition False eventually.
count=0
while count<5:
print("The count is:", count)
Lab Work
Write a program that prints the sum of all positive integers less than 50
sum=0
for i in range(1,50):
sum+=1
print ("Sum numbers(1 to 49)=", sum)
Write a program that sums the integer numbers entered by the user. The program requests
from user to enter an integer until 0 is entered, if so, the sum is displayed.
a=input("Enter a number:")
min=a
for i in range(9):
a=input("Enter a number:")
if a<min:
min=a
print("The minimum number =", min)
Write a Python program that displays the sum of even numbers form 0 – 100, and the
sum of odd numbers from 0 – 100, and the sum of all numbers from 0 – 100.
count=0
sumOdd=0
sumEven=0
sumAll=0
while count<=100:
sumAll+=count
if count%2==0:
sumEven+=count
else:
sumOdd+=count
count+=1
print ("Sum even numbers=", sumEven)
print ("Sum odd numbers=", sumOdd)
print ("Sum all numbers=", sumAll)