Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Python (3150910) Lab Manual

Experiment No.: -______ Date: -___________


Specific Outcomes:
After performing this experiment, the student will be able to:
• Understand the concept of a dictionary ,decision-making statements, and loops.
• Apply the concept of decision-making statements and loops to develop a program for
the given task.

1. Develop a program to check entered number by a user is positive or


negative/zero and display it.

Apply: 1. if...else statement

Algorithm:

Code:
# Input a number from the user
number = float(input("Enter a number: "))

# Check if the number is positive, negative, or zero


if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")

ELECT.ENGG.DEPARTMENT ____ GOVT. ENGG.COLLEGE,BHARUCH


Python (3150910) Lab Manual

2. Develop a program to allows the users to enter a variable. Find the type of an
entered variable and display it.

Apply: 1. if .. elif .. else statement

Algorithm:

Code:
# Ask the user to enter a variable
user_input = input("Enter a variable: ")

# Check the type of the entered variable


if user_input.isdigit():
print("Entered variable is of type 'integer'.")
elif user_input.replace('.', '', 1).isdigit():
print("Entered variable is of type 'float'.")
elif user_input.lower() == 'true' or user_input.lower() == 'false':
print("Entered variable is of type 'boolean'.")
elif user_input.startswith('"') and user_input.endswith('"'):
print("Entered variable is of type 'string'.")
else:
print("Entered variable is of type 'unknown'.")

3. Develop a program to find and display the sum of all numbers stored in a list.

Apply: 1. for loop

ELECT.ENGG.DEPARTMENT ____ GOVT. ENGG.COLLEGE,BHARUCH


Python (3150910) Lab Manual

Algorithm:

Code:
numbers = [1, 2, 3, 4, 5]

# Initialize sum to 0
sum = 0

# Iterate through each number in the list


for num in numbers:
# Add the number to the sum
sum += num

# Display the sum


print("The sum of all numbers in the list is", sum)

4. Develop a program to demonstrate the use of the range function with for loop.

Apply: 1. for loop


2. range function
Algorithm:

ELECT.ENGG.DEPARTMENT ____ GOVT. ENGG.COLLEGE,BHARUCH


Python (3150910) Lab Manual

Code:

➢ Program 1: range() with Stop Argument

➢ Program 2: range() with Start and Stop Arguments

➢ Program 3: range() with Start, Stop and Step Arguments

➢ Program 4: to iterate through a list using indexing

➢ Program 5: to iterate the loop n times


# Program 1: Using range() with only a stop argument
for i in range(5): # This will iterate from 0 to 4
print(i)

# Program 2: Using range() with start and stop arguments


for i in range(2, 6): # This will iterate from 2 to 5
print(i)

ELECT.ENGG.DEPARTMENT ____ GOVT. ENGG.COLLEGE,BHARUCH


Python (3150910) Lab Manual

# Program 3: Using range() with start, stop, and step arguments


for i in range(1, 10, 2): # This will iterate odd numbers from 1 to 9
print(i)

# Program 4: Iterating through a list using indexing


my_list = [10, 20, 30, 40, 50]
for i in range(len(my_list)):
print(my_list[i])

# Program 5: Iterating the loop n times


n = 3 # Change 'n' to the desired number of iterations
for _ in range(n):
print("This loop iterates", n, "times.")

5. A. Develop a program to skip the “e” alphabet from the given string” engineering
that changes the world” and display it.

B. Develop a program to display 1 to 1O numbers by skipping the number “5”.

Apply: 1. while Loop with else


2. continue statement

Algorithm:

ELECT.ENGG.DEPARTMENT ____ GOVT. ENGG.COLLEGE,BHARUCH


Python (3150910) Lab Manual

Code:
#A
string = "engineering that changes the world"
result = ""

for char in string:


if char == "e":
continue
result += char

print(result)

#b
num = 1

while num <= 10:


if num == 5:
num += 1
continue
print(num)
num += 1

6. Develop a program to find and display the sum of numbers entered by the user.
(if entered number is N then sum = 1+2+3+...+N)

Apply: 1. while loop

ELECT.ENGG.DEPARTMENT ____ GOVT. ENGG.COLLEGE,BHARUCH


Python (3150910) Lab Manual

Algorithm:

Code:
num = int(input("Enter a number: "))
sum = 0
i=1

while i <= num:


sum += i
i += 1

print("Sum of numbers from 1 to", num, "is", sum)

7. Develop a program to allows the users to enter a fruit name. Based on the input
name, the program should search for it from the basket list and show its quantity
if the fruit is on the list. If the fruit is not found, the while loop is terminated
normally and the else clause will be executed to add a new fruit to the list.

Apply: 1. while Loop with else


2. break statement
3. dictionary

Algorithm:

ELECT.ENGG.DEPARTMENT ____ GOVT. ENGG.COLLEGE,BHARUCH


Python (3150910) Lab Manual

Code:
basket = {
"apple": 5,
"banana": 3,
"orange": 2
}

fruit = input("Enter a fruit name: ")

while fruit not in basket:


print("Fruit not found in the basket.")
add_fruit = input("Do you want to add it to the basket?
(yes/no): ")

if add_fruit.lower() == "yes":
quantity = int(input("Enter the quantity: "))
basket[fruit] = quantity
break

elif add_fruit.lower() == "no":


break

else:
print(f"The quantity of {fruit} in the basket is
{basket[fruit]}")

Conclusion:
___________________________________________________________________
___________________________________________________________________

ELECT.ENGG.DEPARTMENT ____ GOVT. ENGG.COLLEGE,BHARUCH


Python (3150910) Lab Manual

___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________

Sign of Faculty In-charge

ELECT.ENGG.DEPARTMENT ____ GOVT. ENGG.COLLEGE,BHARUCH

You might also like