Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Python Lab2

Uploaded by

naikerfan3
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)
27 views

Python Lab2

Uploaded by

naikerfan3
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/ 5

Python Lab 2

1. Python Program to count the number of characters in a given string.

def count_characters(string):
count = 0
for char in string:
count += 1
return count

string = input("Enter a string: ")


character_count = count_characters(string)
print(f"The number of characters in the given string is: {character_count}")

Output

2. Python Program to count the number of vowels in a given string

def count_vowels(string):
count = 0
for char in string:
if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u' or \
char == 'A' or char == 'E' or char == 'I' or char == 'O' or char == 'U':
count += 1
return count

string = input("Enter a string: ")


vowel_count = count_vowels(string)
print(f"The number of vowels in the given string is: {vowel_count}")

Output

3. Given two numbers, write a Python code to find the Maximum of two numbers (taken from user).
Input: a = 2, b = 4
Output: 4

def find_maximum(a, b):


if a > b:
return a
else:
return b

a = float(input("Enter the first number: "))


b = float(input("Enter the second number: "))
maximum = find_maximum(a, b)
print(f"The maximum of the two numbers is: {maximum}")

Output
4. Write a program to find factorial of a number using Iteration.

def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result

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


fact = factorial(number)
print(f"The factorial of {number} is: {fact}")

Output

5. Write a program to find factorial of a number using Recursion.

def factorial(n):
if n < 0:
return "Factorial is not defined for negative numbers."
elif n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)

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


fact = factorial(number)
print(f"The factorial of {number} is: {fact}")

Output

6. Write a program to print the ASCI value of all the characters in a string.

def print_ascii_values(string):
for char in string:
print(f"Character: {char} => ASCII Value: {ord(char)}")

input_string = input("Enter a string: ")


print_ascii_values(input_string)

Output
Hint: ord() is used to find the ASCI value of a character
7. Write a python program for Simple Calculator having following:
Select operation.
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit

def calculator():
while True:
print("\nSelect operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Exit")

choice = input("Enter your choice (1-5): ")

if choice == '5':
print("Exiting the calculator. Goodbye!")
break

if choice in ['1', '2', '3', '4']:


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

if choice == '1':
result = num1 + num2
print(f"The result of {num1} + {num2} = {result}")

elif choice == '2':


result = num1 - num2
print(f"The result of {num1} - {num2} = {result}")

elif choice == '3':


result = num1 * num2
print(f"The result of {num1} * {num2} = {result}")
elif choice == '4':
if num2 != 0:
result = num1 / num2
print(f"The result of {num1} / {num2} = {result}")
else:
print("Error: Division by zero is not allowed.")
else:
print("Invalid choice. Please select a valid operation.")

calculator()

Output

8. Create a function with TWO arguments (one of them is the default argument), and call the function.

def greet(name, greeting="Hello"):


print(f"{greeting}, {name}!")

greet("irfan") # Uses default greeting


greet("irfan", "Hi") # Uses custom greeting

Output

9. Write a Python program that calculates the sum of even and odd numbers separately within a given range. Implement a function
that takes the range as input and returns the sum of even and odd numbers within that range.

def sum_even_odd(start, end):


even_sum = 0
odd_sum = 0

for num in range(start, end + 1):


if num % 2 == 0:
even_sum += num
else:
odd_sum += num

return even_sum, odd_sum

start_range = int(input("Enter the start of the range: "))


end_range = int(input("Enter the end of the range: "))

even_sum, odd_sum = sum_even_odd(start_range, end_range)


print(f"The sum of even numbers between {start_range} and {end_range} is: {even_sum}")
print(f"The sum of odd numbers between {start_range} and {end_range} is: {odd_sum}")

Output

10. Write a Python program to generate the multiplication table for a given number. Implement a function that takes the number as
input and prints its multiplication table up to a certain range (e.g., up to 10).

def multiplication_table(number, up_to=10):


print(f"Here's the multiplication table for {number}:\n")
for i in range(1, up_to + 1):
result = number * i
print(f"{number} * {i} = {result}")

number = int(input("Please enter a number to see its multiplication table: "))


multiplication_table(number)

Output

You might also like