WELCOME MESSAGE
PROGRAM DEFINITION
To write a Python program that inputs a welcome message and displays it.
OUTPUT:
Enter your welcome message: Welcome to Python Coding Class
Welcome message: Welcome to Python Coding Class
PROGRAM 1:
welcome_message = input("Enter your welcome message: ")
# Display the welcome message
print("Welcome message: " + welcome_message)
2. LARGER/SMALLER NUMBER
PROGRAM DEFINITION
To write a Python program that inputs two numbers and displays both the larger and the smaller
number.
OUTPUT:
Enter the first number: 89
Enter the second number: 67
The larger number is: 89.0
The smaller number is: 67.0
PROGRAM 2:
# Input two numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Compare the numbers to find the larger and smaller
if num1 > num2:
larger = num1
smaller = num2
elif num2 > num1:
larger = num2
smaller = num1
else:
print("Both numbers are equal.")
exit()
# Display the larger and smaller numbers
print(f"The larger number is: {larger}")
print(f"The smaller number is: {smaller}")
3. PRIME OR COMPOSITE NUMBER
PROGRAM DEFINITION
To Write a Python program that takes an input number and checks whether it is a prime or
composite number.
OUTPUT:
i)Enter a number: 5
5 is a prime number.
ii)Enter a number: 98
98 is a composite number.
PROGRAM 3:
def is_prime(num):
if num <= 1:
return False
if num <= 3:
return True
if num % 2 == 0 or num % 3 == 0:
return False
i=5
while i * i <= num:
if num % i == 0 or num % (i + 2) == 0:
return False
i += 6
return True
# Input a number from the user
try:
num = int(input("Enter a number: "))
if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is a composite number.")
except ValueError:
print("Invalid input. Please enter a valid integer.")
4. Fibonacci Series
PROGRAM DEFINITION
To write a Python program that displays the terms of a Fibonacci series
OUTPUT:
Enter the number of terms in the Fibonacci series: 16
Fibonacci Series with 16 terms:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
PROGRAM:
# Function to generate and display the Fibonacci series
def generate_fibonacci(n):
fib_series = []
a, b = 0, 1
for _ in range(n):
fib_series.append(a)
a, b = b, a + b
return fib_series
# Input the number of terms you want in the Fibonacci series
n = int(input("Enter the number of terms in the Fibonacci series: "))
if n <= 0:
print("Please enter a positive integer.")
else:
fibonacci_series = generate_fibonacci(n)
print("Fibonacci Series with {} terms:".format(n))
for term in fibonacci_series:
print(term, end=" ")
5. Compute GDC and LCM
import math
def compute_gcd(a, b):
return math.gcd(a, b)
def compute_lcm(a, b):
return a * b // math.gcd(a, b)
# Input two integers
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
# Compute and display the GCD and LCM
gcd = compute_gcd(num1, num2)
lcm = compute_lcm(num1, num2)
print(f"GCD of {num1} and {num2} is {gcd}")
print(f"LCM of {num1} and {num2} is {lcm}")
PROGRAM DEFINITION:
Write a program that compute the greatest common divisor and least common
multiple of two integers.
OUTPUT:
Enter the first integer: 56
Enter the second integer: 34
GCD of 56 and 34 is 2
LCM of 56 and 34 is 952
6. Analyzing String Characteristics"
def count_characters(string):
# Initialize counters
vowels = 0
consonants = 0
uppercase = 0
lowercase = 0
# Define a set of vowels for easier checking
vowel_set = set("AEIOUaeiou")
for char in string:
if char.isalpha():
if char in vowel_set:
vowels += 1
else:
consonants += 1
if char.isupper():
uppercase += 1
elif char.islower():
lowercase += 1
# Display the counts
print("Vowels:", vowels)
print("Consonants:", consonants)
print("Uppercase characters:", uppercase)
print("Lowercase characters:", lowercase)
# Input string
input_string = input("Enter a string: ")
# Call the function to count and display the characters
count_characters(input_string)
PROGRAM DEFINITION:
Write a program that Count and display the number of vowels,
consonants, uppercase, lowercase characters in string.
OUTPUT:
Enter a string: Analysing the Characters
Vowels: 7
Consonants: 15
Uppercase characters: 2
Lowercase characters: 20
___________________________________________________________________________________
7. Palindrome Check and Case Conversion for Strings
def is_palindrome(input_string):
# Remove spaces and convert to lowercase for a case-insensitive check
clean_string = input_string.replace(" ", "").lower()
# Check if the cleaned string is the same as its reverse
return clean_string == clean_string[::-1]
def convert_case(input_string):
# Convert the input string to lowercase
lowercase_string = input_string.lower()
# Convert the input string to uppercase
uppercase_string = input_string.upper()
return lowercase_string, uppercase_string
# Get user input
user_input = input("Enter a string: ")
# Check if the input is a palindrome
if is_palindrome(user_input):
print("The input is a palindrome.")
else:
print("The input is not a palindrome.")
# Convert the case of characters in the input string
lowercase, uppercase = convert_case(user_input)
print(f"Lowercase: {lowercase}")
print(f"Uppercase: {uppercase}")
PROGRAM DEFINITION:
Write a program in Python Input a string and determine whether it is a
palindrome or not convert the case of characters in string.
OUTPUT:
Enter a string: Racecar
The input is a palindrome.
Lowercase: racecar
Uppercase: RACECAR
________________________________________________________________________
8. SWAP ELEMENTS
num_list = input("Enter a list of numbers separated by spaces: ").split()
num_list = [int(num) for num in num_list]
if len(num_list) % 2 != 0:
print("Please enter an even number of elements in the list.")
else:
for i in range(0, len(num_list), 2):
if i + 1 < len(num_list):
num_list[i], num_list[i + 1] = num_list[i + 1], num_list[i]
print("List with elements at even positions swapped with elements at odd positions:")
print(num_list)
PROGRAM DEFINITION:
Write a program that accept numbers and swap elements at the even location
with the elements at the odd location.
OUTPUT:
Enter a list of numbers separated by spaces: 4 67 5 8 45 66
List with elements at even positions swapped with elements at odd positions:
[67, 4, 8, 5, 66, 45]
____________________________________________________________________
9. Search for Element in List/Tuple
element_tuple = tuple(input("Enter a tuple of elements separated by spaces: ").split())
search_element = input("Enter the element to search for: ")
if search_element in element_tuple:
print(f"{search_element} is found in the tuple.")
else:
print(f"{search_element} is not found in the tuple.")
PROGRAM DEFINITION:
Write a Python program for Input a list/tuple of elements, search for a given
element in the list/tuple.
OUTPUT:
Enter a tuple of elements separated by spaces: 45 6 8 34 98
Enter the element to search for: 34
34 is found in the tuple
______________________________________________________________________
10. Display Names of Students with Marks Above 75
n = int(input("Enter the number of students: "))
student_data = {}
for i in range(n):
roll_number = input(f"Enter roll number for student {i + 1}: ")
name = input(f"Enter name for student {i + 1}: ")
marks = float(input(f"Enter marks for student {i + 1}: "))
student_data[roll_number] = {'name': name, 'marks': marks}
print("Names of students with marks above 75:")
for roll_number, data in student_data.items():
if data['marks'] > 75:
print(data['name'])
PROGRAM DEFINITION:
Write a program to Create a dictionary with the roll number, name and marks of
students in a class and display the names of students who have marks above 75.
OUTPUT:
Enter the number of students: 2
Enter roll number for student 1: 01
Enter name for student 1: Ravi
Enter marks for student 1: 78
Enter roll number for student 2: 02
Enter name for student 2: Vaishu
Enter marks for student 2: 45
Names of students with marks above 75:
Ravi
_________________________________________________________________
11. PATTERN
#pattern 1
print("Pattern 1:")
for i in range(1, 6):
print('*' * i)
#pattern 2
print("Pattern 2:")
for i in range(5, 0, -1):
for j in range(1, i + 1):
print(j, end='')
print()
#pattern 3
print("Pattern 3:")
for i in range(5):
for j in range(i + 1):
print(chr(65 + j), end='')
print()
PROGRAM DEFINITION:
Write a program to generate the following patterns.
OUTPUT:
Pattern 1:
*
**
***
****
*****
Pattern 2:
12345
1234
123
12
1
Pattern 3:
A
AB
ABC
ABCD
ABCDE
___________________________________________________________________
12. SORTING AN ARRAY OF STRINGS
names = list()
n = int(input("Enter the number of names: "))
for i in range(n):
name = input("Enter name: ")
names.append(name)
for i in range(n - 1):
for j in range(0, n - i - 1):
if names[j] > names[j + 1]:
# Swap elements if they are in the wrong order
temp = names[j]
names[j] = names[j + 1]
names[j + 1] = temp
print("Sorted Array:")
for i in range(n):
print(names[i])
PROGRAM DEFINITION:
Write a program in python to sort a given array of student names in alphabetical
order.
OUTPUT:
Enter the number of names: 5
Enter name: Hema
Enter name: Anu
Enter name: Zara
Enter name: Rahul
Enter name: Mohan
Sorted Array:
Anu
Hema
Mohan
Rahul
Zara
____________________________________________________________
13. FACTORIAL
PROBLEM DEFINITION:
Write a program in Python to find the factorial for a given number.
OUTPUT:
Please enter any number to find factorial: 6
The factorial of 6 is: 720
def factorial(num):
fact=1
for i in range(1, num+1):
fact=fact*i
return fact
number=int(input("Please enter any number to find factorial: "))
result=factorial(number)
print("The factorial of", number ,"is:", result)
__________________________________________________________________
14. RANDOM NUMBER GENERATOR
PROGRAM DEFINITION
To write a Python Program to generate random numbers between 1 to 6.
OUTPUT:
Rolling the dice...
You get... : 1
Do you want to continue (y/n) :y
Rolling the dice...
You get... : 5
Do you want to continue (y/n) :y
Rolling the dice...
You get... : 4
Do you want to continue (y/n) :n
import random
min = 1
max = 6
roll_again = "y"
while roll_again == "y" or roll_again == "Y":
print("Rolling the dice...")
val = random.randint (min, max)
print("You get... :", val)
roll_again = input("Do you want to continue (y/n) :")
_____________________________________________________________
15. SUM OF SERIES
# i)
x=float(input("Enter value of X:"))
n=int(input("Enter the value of n (for x ** n):"))
s=0
for a in range (n+1):
s+=x**a
print("SUM of First",n,"terms:",s)
ii) & iii)
Above Program refer Text book Page number 275
Workout and find the Output and write in record.
PROGRAM DEFINITION:
Write a Python Program to input the value of x and n and print the sum of the
following series:
OUTPUT:
i) Enter value of X:3
Enter the value of n (for x ** n):2
SUM of First 2 terms: 13.0