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

ICP

Uploaded by

Iqra Sarwar
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)
17 views

ICP

Uploaded by

Iqra Sarwar
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/ 7

Assignment

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.")

# Program checks if the number is positive or negative

# And displays an appropriate messages

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,

# we check if the number is positie or

# negatie or zero and

# display an appropriate message

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

# check if the number is positie or

# negatie or zero and display

# an appropriate message

# This tme we use nested if

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.

print("This program adds two numbers")


num1= input("Enter no 1:")
num2= input("Enter no 2:")
addition = int(num1) + int(num2)
print ("sum=", addition)
Write a program to calculate current (I) in a wire. The charge (Q) flowing through it in a time (t)

of 5 seconds is 5 Coulombs. Print the current (I) on the screen.

Hint: I = Q / t

Q = input("Enter the value of Charge-Q:")


t = input("Enter the value of Time-t:")
I = float(Q)/float(t)
print("Current-I =",I)
Scenario

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

would tell Ali his aggregate.

Firstly, we shall need to ask Ali about his marks and store those values somewhere so we can

calculate the final aggregate at the end.

matric = int(input("Enter your matric marks:"))


inter = int(input("Enter your inter marks:"))
ecat = int(input("Enter your ecat marks:"))
matric_percentage = float(((matric/1100)*100)*0.25)
inter_percentage = float(((inter/1100)*100)*0.45)
ecat_percentage = float(((ecat/400)*100)*0.30)
aggregate=float(matric_percentage) + float(inter_percentage) + float(ecat_percentage)
print("Average:",aggregate)
Write a python program to print the first three multiples of the given number.

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

number1=int(input("Enter first number:"))


number2=int(input("Enter second number:"))
print("First three multiples of", number1, "are", number1, number1+number1, number1
+number1+number1)
print("First three multiples of", number2, "are", number2, number2+number2, number2
+number2+number2)
Write a python program to print the sum of the first three multiples of two given numbers.

number1=int(input("Enter first number:"))


number2=int(input("Enter second number:"))
print("First three multiples of", number1, "are", number1, number1+number1, number1
+number1+number1)
print("First three multiples of", number2, "are", number2, number2+number2, number2
+number2+number2)
sum1=number1+(number1+number1)+(number1+number1+number1)
sum2=number2+(number2+number2)+(number2+number2+number2)
total=sum1+sum2
print("The sum of multiples is", total)
The sequence of numbers (1, 2, 3, … , 100) is arithmetic and when we are looking for the sum

of a sequence, we call it a series. Thanks to Gauss, there is a special formula we can use to

find the sum of a series:

number=int(input("Enter the number:"))


sumofSeries=((number*(number+1)/2))
print("The sum of all numbers is: ", sumofSeries)
Take two numbers as input and find the sum between these two numbers

num1=int(input("Enter number 1:"))


num2=int(input("Enter number 2:"))
total_numbers=int(num2)-(int(num1-1))
sumofNumbers=(total_numbers/2)*(num1+num2)
print("The sum of numbers between", num1, "and", num2, "is:", sumofNumbers)

Loop lab manual


The while Loop
A while loop executes statements repeatedly as long as a condition remains true. while loop is a condition-
controlled loop, it is controlled by a true/false condition. It tests the condition before executing the loop body

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)

The Infinite Loop


A loop becomes infinite loop if a condition never becomes False. You must pay attention when using while
loops because a loop may never end. Such a loop is called an infinite loop.

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=int(input("Enter numbers to sum, Zero number ends list:"))


sum=0
while a!=0:
sum+=a
a=int(input("Enter a number:"))
print("Sum numbers:", sum)
Write a program that asks the user to type 10 integers, then prints the smallest value of

the entered integers

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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy