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

Python Programs

The document contains multiple Python programs demonstrating various functionalities such as finding the largest and smallest numbers in a list, replacing vowels in a string, checking for palindromes, swapping numbers, generating Fibonacci series, creating a calculator, computing factorials, removing duplicates from a list, checking number presence in a list, and summing dictionary values. Each program includes user-defined functions and handles user input. The document serves as a practical guide for implementing basic programming concepts in Python.

Uploaded by

viratttt91
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Programs

The document contains multiple Python programs demonstrating various functionalities such as finding the largest and smallest numbers in a list, replacing vowels in a string, checking for palindromes, swapping numbers, generating Fibonacci series, creating a calculator, computing factorials, removing duplicates from a list, checking number presence in a list, and summing dictionary values. Each program includes user-defined functions and handles user input. The document serves as a practical guide for implementing basic programming concepts in Python.

Uploaded by

viratttt91
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Programs

Develop a python program to find largest and smallest


number in a list of numbers. List=[12,45,22,1,2,41,31,10,8,6,4]

list1 = [12, 45, 22, 1, 2, 41, 31, 10, 8, 6, 4]


largest = list1[0]
smallest = list1[0]
for num in list1[1:]:
if num > largest:
largest = num
elif num < smallest:
smallest = num
print("Largest number in the list:", largest)
print("Smallest number in the list:", smallest)

Write a program with user defined function with string as a


parameter which replaces all vowels in the string with „&‟.

def replace_vowels(string):
vowels = "aeiouAEIOU"
new_string = ""
for char in string:
if char in vowels:
new_string += "&"
else:
new_string += char
return new_string

input_string = input("Enter a string: ") # Get string input from the user
modified_string = replace_vowels(input_string)
print("Modified string:", modified_string)

Write a program using a user defined function to check if a


string is a palindrome or not.

def is_palindrome(string):
reversed_string = string[::-1]
if string == reversed_string:
return True
else:
return False

input_string = input("Enter a string: ")


if is_palindrome(input_string):
print(input_string, "is a palindrome")
else:
print(input_string, "is not a palindrome")
Write Python program to swap two numbers without using
Intermediate/Temporary variables. Prompt the user for input.

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


num2 = int(input("Enter second number: "))

num1, num2 = num2, num1

print("After swapping:")
print("First number:", num1)
print("Second number:", num2)

Write python code snippet to display n terms of Fibonacci


series.

n = int(input("Enter the number of terms: "))


a=0
b=1
if n <= 0:
print("Please enter a positive integer.")
elif n == 1:
print(a)
else:
print(a, b, end=" ")
for i in range(2, n):
c=a+b
print(c, end=" ")
a=b
b=c
Develop a python program to make a calculator and perform
the basic five arithmetic operations based on the user
input.The menu should be continuously available to the user.

def calculator():

while True:
print("\nCalculator Menu:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Exit")

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

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

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

if choice == '1':
result = num1 + num2
print("Result:", result)
elif choice == '2':
result = num1 - num2
print("Result:", result)
elif choice == '3':
result = num1 * num2
print("Result:", result)
elif choice == '4':
if num2 == 0:
print("Error: Division by zero is not allowed.")
else:
result = num1 / num2
print("Result:", result)
else:
print("Invalid choice. Please enter a number between 1 and 5.")

except ValueError:
print("Invalid input. Please enter numbers only.")

if __name__ == "__main__":
calculator()

Write a program using a user defined function to check if a


string is a palindrome or not.

string = input("Enter a string: ")

if string == string[::-1]:
print(string, "is a palindrome")
else:
print(string, "is not a palindrome")

Write a recursive function to compute the factorial of an input


number N.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

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


print(factorial(num))
Write a program to remove duplicates items of
list,list1=[10,20,10,30,20,40,30,50] using list native or list
comprehension method and print fresh list (having no
duplicates

list1 = [10, 20, 10, 30, 20, 40, 30, 50]

list2 = []
for item in list1:
if item not in list2:
list2.append(item)

print(list2)

list3 = list(set(list1))
print(list3)

list4 = [x for i, x in enumerate(list1) if i == list1.index(x)]


print(list4)

Write a user defined function to check if a number is present


in the list (list should be inputted by user) or not. If the
number is present ,return the position of the number? Print
an appropriate message if the number is not present in the
list.
def check_presence(lst, num):
for i in range(len(lst)):
if lst[i] == num:
return i
return -1

user_list_str = input("Enter a list of numbers separated by spaces: ")


user_list = [int(x) for x in user_list_str.split()]

num_to_check = int(input("Enter the number to check: "))

position = check_presence(user_list, num_to_check)

if position != -1:
print(f"The number {num_to_check} is present at position
{position}.")
else:
print(f"The number {num_to_check} is not present in the list.")

Write a python program to find the sum of all items in a


dictionary.

my_dict = {"a": 10, "b": 20, "c": 30, "d": 40}

sum_of_values = 0
for value in my_dict.values():
sum_of_values += value

print(sum_of_values)

You might also like