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

PDF for Cs Project

The document provides a series of Python programming exercises that cover various topics including input/output, conditional statements, functions, and data structures. It includes examples for displaying welcome messages, finding larger/smaller numbers, checking for prime numbers, generating Fibonacci series, computing GCD and LCM, counting characters, checking for palindromes, swapping list elements, and searching within lists or tuples. Additionally, it mentions creating a dictionary for student data and updating existing data in a table.

Uploaded by

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

PDF for Cs Project

The document provides a series of Python programming exercises that cover various topics including input/output, conditional statements, functions, and data structures. It includes examples for displaying welcome messages, finding larger/smaller numbers, checking for prime numbers, generating Fibonacci series, computing GCD and LCM, counting characters, checking for palindromes, swapping list elements, and searching within lists or tuples. Additionally, it mentions creating a dictionary for student data and updating existing data in a table.

Uploaded by

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

Question-1 Write a welcome message and display

it
Answer- input
# Ask the user for a welcome message
welcome_message = input("Enter your welcome
message: ")

# Display the welcome message


print("Your welcome message is: " + welcome_message)

output
Enter your welcome message: how are you
Your welcome message is: how are you

Question-2 input two number and display the larger /


smaller number
Answer – input
# Ask the user for two numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Determine and display the larger and smaller number


if num1 > num2:
larger = num1
smaller = num2
elif num1 < num2:
larger = num2
smaller = num1
else:
larger = smaller = num1 # or num2, since they are
equal

print(f"The larger number is: {larger}")


print(f"The smaller number is: {smaller}")
output
enter the first number: 10
Enter the second number: 20
The larger number is: 20.0
The smaller number is: 10.0

Question-3 input three number and display the larger /


smaller number

Answer-
Input - # Ask the user for three numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Determine the largest and smallest number


largest = max(num1, num2, num3)
smallest = min(num1, num2, num3)

# Display the results


print(f"The largest number is: {largest}")
print(f"The smallest number is: {smallest}")

Output
Enter the first number: 30
Enter the second number: 60
Enter the third number: 90
The largest number is: 90.0
The smallest number is: 30.0

Question-4 find the largest/smallest number in a


list/tuple from python
Input
# Finding the largest and smallest number in a list
def find_largest_smallest_list(numbers):
largest = max(numbers)
smallest = min(numbers)
return largest, smallest

# Finding the largest and smallest number in a tuple


def find_largest_smallest_tuple(numbers):
largest = max(numbers)
smallest = min(numbers)
return largest, smallest

# Example usage
num_list = [3, 1, 4, 1, 5, 9]
num_tuple = (3, 1, 4, 1, 5, 9)

largest_list, smallest_list =
find_largest_smallest_list(num_list)
largest_tuple, smallest_tuple =
find_largest_smallest_tuple(num_tuple)

print(f"Largest in list: {largest_list}, Smallest in list:


{smallest_list}")
print(f"Largest in tuple: {largest_tuple}, Smallest in tuple:
{smallest_tuple}")

output-
Largest in list: 9, Smallest in list: 1
Largest in tuple: 9, Smallest in tuple: 1

Question-5 input a number and check if number is a


prime or composite number

Input # Function to check if the number is prime


def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True

# Input a number from the user


number = int(input("Enter a number: "))
# Check if the number is prime or composite
if is_prime(number):
print(f"{number} is a prime number.")
else:
print(f"{number} is a composite number.")

output- Enter a number: 78


78 is a composite number.

Question-6 display the terms of a fiboacci series


Input- # Function to generate Fibonacci series up to n
terms
def fibonacci_series(n):
fib_sequence = [0, 1]
while len(fib_sequence) < n:
next_term = fib_sequence[-1] + fib_sequence[-2]
fib_sequence.append(next_term)
return fib_sequence

# Input the number of terms from the user


num_terms = int(input("Enter the number of terms: "))
# Generate and display the Fibonacci series
if num_terms <= 0:
print("Please enter a positive integer.")
else:
series = fibonacci_series(num_terms)
print(f"The first {num_terms} terms of the Fibonacci
series are:")
print(series)

output- Enter the number of terms: 448


The first 448 terms of the Fibonacci series are:

Question 7 import math

# Function to compute GCD


def compute_gcd(a, b):
return math.gcd(a, b)

# Function to compute LCM


def compute_lcm(a, b):

gcd = compute_gcd(a, b)
return abs(a * b) // gcd

# Input two integers from the user


num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))

# Compute GCD and LCM


gcd = compute_gcd(num1, num2)
lcm = compute_lcm(num1, num2)

# Display the results


print(f"The Greatest Common Divisor of {num1} and
{num2} is: {gcd}")
print(f"The Least Common Multiple of {num1} and
{num2} is: {lcm}")
import math

# Function to compute GCD


def compute_gcd(a, b):
return math.gcd(a, b)

# Function to compute LCM


def compute_lcm(a, b):

gcd = compute_gcd(a, b)
return abs(a * b) // gcd

# Input two integers from the user


num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))

# Compute GCD and LCM


gcd = compute_gcd(num1, num2)
lcm = compute_lcm(num1, num2)

# Display the results


print(f"The Greatest Common Divisor of {num1} and
{num2} is: {gcd}")
print(f"The Least Common Multiple of {num1} and
{num2} is: {lcm}")

input- import math

# Function to compute GCD


def compute_gcd(a, b):
return math.gcd(a, b)

# Function to compute LCM


def compute_lcm(a, b):

gcd = compute_gcd(a, b)
return abs(a * b) // gcd

# Input two integers from the user


num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))

# Compute GCD and LCM


gcd = compute_gcd(num1, num2)
lcm = compute_lcm(num1, num2)

# Display the results


print(f"The Greatest Common Divisor of {num1} and
{num2} is: {gcd}")
print(f"The Least Common Multiple of {num1} and
{num2} is: {lcm}")
Question 8
Inpu-# Function to count vowels, consonants, uppercase,
and lowercase characters
def count_characters(input_string):
vowels = "aeiouAEIOU"
num_vowels = 0
num_consonants = 0
num_uppercase = 0
num_lowercase = 0

for char in input_string:


if char.isalpha():
if char in vowels:
num_vowels += 1
else:
num_consonants += 1
if char.isupper():
num_uppercase += 1
elif char.islower():
num_lowercase += 1

return num_vowels, num_consonants, num_uppercase,


num_lowercase
# Input string from the user
input_string = input("Enter a string: ")

# Count the characters


num_vowels, num_consonants, num_uppercase,
num_lowercase = count_characters(input_string)

# Display the results


print(f"Number of vowels: {num_vowels}")
print(f"Number of consonants: {num_consonants}")
print(f"Number of uppercase characters:
{num_uppercase}")
print(f"Number of lowercase characters:
{num_lowercase}")

output- Number of vowels: 11


Number of consonants: 24
Number of uppercase characters: 19
Number of lowercase characters: 16
Question 9
Input # Function to check if a string is a palindrome
def is_palindrome(s):
# Remove non-alphanumeric characters and convert to
lowercase
clean_s = ''.join(e for e in s if e.isalnum()).lower()
# Check if the cleaned string is equal to its reverse
return clean_s == clean_s[::-1]

# Function to convert the case of characters in a string


def convert_case(s):
return s.swapcase()

# Input a string from the user


input_string = input("Enter a string: ")

# Check if the string is a palindrome


if is_palindrome(input_string):
print(f'"{input_string}" is a palindrome.')
else:
print(f'"{input_string}" is not a palindrome.')

# Convert the case of characters in the string


converted_string = convert_case(input_string)
print(f'The string with converted case is:
"{converted_string}"')

output- Enter a string: "Xf12#bQz7k@Gm!"


""Xf12#bQz7k@Gm!"" is not a palindrome.
The string with converted case is: ""xF12#BqZ7K@gM!""

Question-10 input a list of number and swap element at


the even location with the elements at the odd location

Input def swap_even_odd(numbers):


# Ensure the list has an even number of elements for
swapping
length = len(numbers)
for i in range(0, length - 1, 2):
# Swap elements at even and odd locations
numbers[i], numbers[i + 1] = numbers[i + 1],
numbers[i]
return numbers

# Input a list of numbers from the user


input_list = list(map(int, input("Enter a list of numbers
separated by spaces: ").split()))
# Swap elements at even and odd locations
swapped_list = swap_even_odd(input_list)

# Display the result


print("List after swapping even and odd elements:",
swapped_list)

input
Enter a list of numbers separated by spaces: 1 2 3 4 5 6
List after swapping even and odd elements: [2, 1, 4, 3, 6,
5]

Question 11 input a list/tuple of element,serch for a given


element in the list/tuple

Input- def search_element(container, target):


# Check if the target element is in the container
if target in container:
return f"{target} is present in the container."
else:
return f"{target} is not present in the container."
# Input a list or tuple of elements from the user
input_container = input("Enter elements separated by
spaces: ").split()
container_type = input("Is this a list or tuple? ")

# Convert input to the appropriate type


if container_type.lower() == 'list':
container = list(input_container)
elif container_type.lower() == 'tuple':
container = tuple(input_container)
else:
print("Invalid container type. Please enter 'list' or
'tuple'.")
exit()

# Input the element to search for


target_element = input("Enter the element to search for:
")

# Search for the element in the container


result = search_element(container, target_element)

# Display the result


print(result)

output-Enter elements separated by spaces: apple


banana cherry date
Is this a list or tuple? list
Enter the element to search for: banana
banana is present in the container.
Question 12 creat a dictionary with the roll number,name
and marks of n students in a class and display the name
of students who have marks above 75
Question 14 how can you update exhisting data in a
table?

You might also like