1.
Write a Program to input a Student Name, Semester, City name, and Pin code
from keyboard and display the same on output screen.
Algorithm:
Step1: Input a student name, semester, city name and pin code from the
keyboard.
Step2: display the same information.
name=input("Enter your name:")
sem=int(input("Enter your semester:"))
city=input("Enter your city name:")
pincode=int(input("Enter a pincode of your city:"))
print("\n\n\n")
print("Student Name :",name)
print("Semester:",sem)
print("City:",city)
print("Pincode:",pincode)
OUTPUT:-
Enter your name:XYZ
Enter your semester:3
Enter your city name:RAICHUR
Enter a pincode of your city:584101
Student Name : XYZ
Semester: 3
City: RAICHUR
Pincode: 584101
2. Write a program to Evaluate expressions and displays formatted output.
(a) string formatting using format() function
Algorithm:
Step 1: Consider two variables a, b and store values.
Step 2: Calculate sum and difference of these values and store these
result in variables called sum and sub respectively.
Step 3: Display the values of a, b, sum, sub using string format function.
a = 20
b = 10
sum = a + b
sub = a - b
print('The value of a is {} and b is {}'.format(a,b))
print('{2} is the sum of {0} and {1}'.format(a,b,sum))
print('{sub_value} is the subtraction of {value_a} and {value_b}'.format(value_a
= a ,value_b = b,sub_value = sub))
OUTPUT:-
The value of a is 20 and b is 10
30 is the sum of 20 and 10
10 is the subtraction of 20 and 10
3. If the ages of Ram Shyam Ajay enter in the keyboard write a program to
determine the youngest of the three.
Algorithm:
Step1: Take a input for ages if Ram,Shyam and Ajay. the keyboard,
Step2: If Ram age is greater than Shyam age and Ajay age , display “Ram
is youngest”. Step3: Otherwise,If Shyam age is greater than Ram age and
Ajay age , display “Shyam is youngest”.
Step4: Otherwise display “Ajay is youngest”.
ram=int(input("Enter a age of Ram:"))
shyam=int(input("Enter a age of Shyam:"))
ajay=int(input("Enter a age of Ajay:"))
if (ram>shyam)and ( ram>ajay):
print("Ram is youngest.")
elif shyam>ram and shyam>ajay:
print("Shyam is youngest.")
else:
print("Ajay is youngest.")
4. Since the introduction of the Gregorian calendar (in 1582), the following rule
is used to determine the kind of year:
if the year number isn't divisible by four, it's a common year;
otherwise, if the year number isn't divisible by 100, it's a leap year;
otherwise, if the year number isn't divisible by 400, it's a common year;
otherwise, it's a leap year.
Algorithm:
Step1: Input a value of a year from the keyboard.
Step2: If year is smaller than 1582 ,display “Year is not in Gregorian calender”.
Step3: Otherwise again checking for a condition.
Step4: If the year is not divisible by 4,display “common year”.
Step5: Otherwise ,If the year is not divisible by 100,display “leap year”.
Step6: Otherwise ,If the year is not divisible by 400,display “common year”.
Step7 :Otherwise, display “leap year”.
year = int(input("Enter the value of year: "))
if year < 1582:
print("Year is not in Gregorian calendar.")
else:
if year % 4 != 0:
print("Common year.")
elif year % 100 != 0:
print("Leap year.")
elif year % 400 != 0:
print("Common year.")
else:
print("Leap year.")
OUTPUT:-
Enter the value of year: 1500
Year is not in Gregorian calendar.
Enter the value of year: 2023
Common year.
Enter the value of year: 2024
Leap year.
Enter the value of year: 2000
Leap year.
5. Write a program that asks for a word, phrase, or sentence. The program
should then print whether the input is a palindrome. Note: Use For Loop
Algorithm:
Step1: Input a word ,phrase or statement from the keyboard.
Step2: Find the reverse of a word using for loop.
Step3: Store that reverse string in another variable.
Step4: If the original string is equal to reverse string, display “ The word is
palindrome”. Step5: Otherwise ,display “The word is not a palindrome”.
letter = input("Enter a word, phrase or sentence: ")
# reverse the string using slicing
reverse = letter[::-1]
print("Reversed word, phrase or sentence is:", reverse)
if letter == reverse:
print("The word, phrase or sentence is palindrome.")
else:
print("The word, phrase or sentence is not palindrome.")
OUTPUT:-
Enter a word, phrase or sentence: madam
Reversed word, phrase or sentence is: madam
The word, phrase or sentence is palindrome.
Enter a word, phrase or sentence: hello
Reversed word, phrase or sentence is: olleh
The word, phrase or sentence is not palindrome.
6. Write a program to find the factorial value of any number entered through
the keyboard. Note: Use While Loop.
Algorithm:
Step1: Input a number from the keyboard.
Step2: Initialize the result to 1 and store it to one variable.
Step3: Start a loop and multiply the result by the number.
Step4: Reduce one from the number in each iteration.
Step5: End the loop once the number reaches 1
Step6: display the result value.
n = int(input("Enter a number: "))
temp = n
fact = 1
while n != 0:
fact *= n
n -= 1
print("Factorial of {0} is {1}.".format(temp, fact))
Enter a number: 5
Factorial of 5 is 120.
Enter a number: 0
Factorial of 0 is 1.
Enter a number: 1
Factorial of 1 is 1.
7. Write a program to create two sets using set comprehension and perform
following set operations: (i) Union (ii) Difference (iii) Symmetric Difference (iv)
Intersection.
Algorithm:
Step1: Create a two sets.
Step2: Perform a union operation using | operator and display it’s result.
Step3: Perform a difference operation using - operator and display it’s result.
Step4: Perform a symmetric difference operation using ^ operator and display
it’s result. Step5: Perform a intersection operation using & operator and display
it’s result.
set1 = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100}
set2 = {2, 4, 6, 8, 10}
print("Elements of Set1 are:", set1)
print("Elements of Set2 are:", set2)
print("Union Operation:", set1 | set2)
print("Intersection Operation:", set1 & set2)
print("Difference Operation:", set1 - set2)
print("Symmetric Difference Operation:", set1 ^ set2)
OUTPUT:-
Elements of Set1 are: {1, 64, 100, 36, 4, 9, 16, 81, 49, 25}
Elements of Set2 are: {2, 4, 6, 8, 10}
Union Operation: {1, 2, 4, 6, 8, 9, 10, 16, 25, 36, 49, 64, 81, 100}
Intersection Operation: {4}
Difference Operation: {1, 64, 100, 36, 9, 16, 81, 49, 25}
Symmetric Difference Operation: {1, 2, 6, 8, 9, 10, 16, 25, 36, 49, 64, 81, 100}
8. Write a program to create a tuple and perform following operations (i)
Display the elements of tuple (ii) Find an item using index method (iii) Reverse
all the elements (iv) Display the elements from 3 rd position to 7th position (v)
Delete entire tuple
ALGORITHM:
Step 1: Start
Step 2: Declare and Initialize tuple1
Step 3: Display tuple1
Step 4: Find an item using index method.
Step 5: Reverse tuple1 elements and Display
Step 6: Display elements from 3rd to 7th position
Step 7: Delete tuple1 Step
8: Stop
tuple1 = (1, 2, 3, 4, 7, 8, 9)
print("Elements of tuple are:\n")
for i in range(len(tuple1)):
print(tuple1[i])
find = int(input("Enter element to search in tuple: "))
if find in tuple1:
i = [Link](find)
print("\nElement %d found at index %d in tuple\n" % (find, i))
else:
print("\nElement %d not found in tuple\n" % find)
print("Elements of tuple in reverse order:\n", tuple1[::-1])
print("Elements from 3rd position to 8th position:", tuple1[3:8])
print("Deleting the tuple...")
del tuple1
# After deletion, you cannot access tuple1 anymore
# So we just confirm deletion
print("Tuple deleted successfully.")
OUTPUT:-
Elements of tuple are:
1
2
3
4
7
8
9
Enter element to search in tuple: 7
Element 7 found at index 4 in tuple
Elements of tuple in reverse order:
(9, 8, 7, 4, 3, 2, 1)
Elements from 3rd position to 8th position: (4, 7, 8, 9)
Deleting the tuple...
Tuple deleted successfully.
9. Write a program to create a List of 10 odd numbers using List
Comprehension and perform the following: i) Display all the elements (ii) Find
the length of list (iii) Adding new items to list using append(), insert() (iv)
Remove certain items using pop(), remove() (v) Find the particular item using
index()
ALGORITHM:
Step 1: Start
Step 2: Declare and initialize List1 using Comprehension
Step 3: Display List1 Step 4: Obtain the length of List1
Step 5: Add items using append(), insert() to List1
Step 6: Remove items using pop(), remove() from List1
Step 7: Find the particular item using index() and display
Step 8: Stop
list1 = [i for i in range(20) if i % 2 != 0] # odd numbers from 0–19 list1 = [1, 3, 5,
7, 9, 11, 13, 15, 17, 19]
print("Elements of List are:")
for i in range(len(list1)):
print(list1[i])
print("Length of list is", len(list1))
print("Adding new item using append method")
[Link](99)
print("Adding new item using insert method")
[Link](1, 2)
print("Elements are:", list1)
print("Removing an item using pop method")
[Link](1)
print("Removing an item using remove method")
[Link](3)
print("Elements are:", list1)
find = int(input("Enter the element to search: "))
pos = [Link](find)
print("%d element found at index %d" % (find, pos))
OUTPUT:-
Elements of List are:
1
3
5
7
9
11
13
15
17
19
Length of list is 10
Adding new item using append method
Adding new item using insert method
Elements are: [1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 99]
Removing an item using pop method
Removing an item using remove method
Elements are: [1, 5, 7, 9, 11, 13, 15, 17, 19, 99]
Enter the element to search: 7
7 element found at index 2
10. Write a program to create a List of 10 Even numbers using List
Comprehension and perform the following:
(i) Sort List (i) Reverse List
(iii) Find Max, Min, and Sum (iv) Display the range of items using
slicing operator (v) Clear all elements (vi) Delete the list
ALGORITHM:
Step 1: Start
Step 2: Declare and initialize List1 using Comprehension
Step 3: Display List1 Step
4: Sort List1 Step
5: Reverse List1
Step 6: Display max, min and sum of List1
Step 7: Display items in range
Step 8: Clear List1
Step 9: Delete List1
Step 10: Stop
list1 = [i for i in range(20) if i % 2 == 0] # even numbers from 0–19 list1 = [0, 2,
4, 6, 8, 10, 12, 14, 16, 18]
print("Elements of List are ")
for i in range(len(list1)):
print(list1[i])
print("Elements of list before sort ", list1)
[Link]()
print("Elements of list after sort ", list1)
[Link]()
print("Elements of list after reverse ", list1)
print("Maximum ", max(list1))
print("Minimum ", min(list1))
print("Sum of elements ", sum(list1))
print("Elements from 3 rd position to 7 position", list1[3:8])
print("Clearing list")
[Link]()
print("Elements of list are ", list1)
OUTPUT:-
Elements of List are
0
2
4
6
8
10
12
14
16
18
Elements of list before sort [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Elements of list after sort [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Elements of list after reverse [18, 16, 14, 12, 10, 8, 6, 4, 2, 0]
Maximum 18
Minimum 0
Sum of elements 90
Elements from 3 rd position to 7 position [12, 10, 8, 6, 4]
Clearing list
Elements of list are []
11. Consider Two strings S1 “Government Polytechnic” and S2 “Karwar,
Uttarakannada” Create a program to create these 2 strings and perform the
following:
(i) Find First character in S1
(ii) Find Last but one character in S2
(iii)Find length of both strings (iv)Reverse S1 (v)Apply
strip(),lower(),upper(),split(),replace(),find(),join(),max(),min() methods
ALGORITHM:
Step 1: Start Step 2: Declare S1 and assign “Government
polytechnic” to S1 Step 3: Declare S2 and assign “Karwar, Uttarakannad” to S2
Step 4: Display first character of S1 Step 5: Display last character of
S2
Step 6: Obtain length of S1 and display Step 7: Obtain length of S2 and
display Step 8: Declare variable reverse Step 9: Use a for
loop to iterate through
Step 10: Reverse S1 string and assign to reverse variable
Step 11: Display reverse
Step 12: Declare a string list , assign to str Step 13: Use join() to join str ,
assign to str2 Step 14: Display str2 Step 15: Apply
strip() and display Step 16: Apply split() and display Step
17: Apply lower() and display Step 18: Apply upper() and display
Step 19: Apply find() and display Step 20: Apply max() and
display Step 21: Apply min() and display Step 22:
Apply replace() and display Step 23: Stop
S1 = "H K E S Polytechnic"
S2 = "RAICHUR, KARNATAKA"
print("String1 is:", S1)
print("String2 is:", S2)
# Accessing characters
print("First character of S1:", S1[0])
print("Last but one character of S2:", S2[-2])
# Length of strings
print("Length of String1 is:", len(S1))
print("Length of String2 is:", len(S2))
# Reverse string manually
reverse = ""
for i in S1:
reverse = i + reverse
print("Reversed string of String1:", reverse)
print("\nDemonstration of String Operations")
# Join method
str_list = ["Strings", "In", "Python"]
str2 = " ".join(str_list)
print("Join method:", str2)
# Strip method (removes leading/trailing spaces, here no effect)
print("Strip method:", [Link]())
# Split method
print("Split method:", [Link]())
# Case conversion
print("Lowercase:", [Link]())
print("Uppercase:", [Link]())
# Find method
print("Find Method (index of 'G'):", [Link]("G"))
# Max & Min method
print("Max Method:", max(S1))
print("Min Method:", min(S1))
# Replace method
S = [Link]("Government Polytechnic", "Diploma college")
print("Replace method:", S)
OUTPUT:-
String1 is: H K E S Polytechnic
String2 is: RAICHUR, KARNATAKA
First character of S1: H
Last but one character of S2: K
Length of String1 is: 18
Length of String2 is: 18
Reversed string of String1: cinhcetylop S E K H
Demonstration of String Operations
Join method: Strings In Python
Strip method: H K E S Polytechnic
Split method: ['H', 'K', 'E', 'S', 'Polytechnic']
Lowercase: h k e s polytechnic
Uppercase: H K E S POLYTECHNIC
Find Method (index of 'G'): -1
Max Method: y
Min Method:
Replace method: H K E S Polytechnic
12. Write a program to create two arrays (i) integer numbers array (ii) floating
point numbers array and perform following operations:
(a) Insert new elements using insert()
(b) Remove Existing elements using pop(),remove()
(c) print elements from beginning to a range use [:Index].
(d) print elements from end use [:-Index]
(e) print elements from specific Index till the end use [Index:]
(f) print elements within a range, use [Start Index : End Index]
(g) print whole List with the use of slicing operation, use [:].
(h) print whole array in reverse order, use [::-1].
ALGORITHM:
Step 1: Start Step 2: Import array module Step
3: Declare array of integer numbers and assign to ary1
Step 4: Declare array of floating point numbers and assign to ary2
Step 5: Insert an item using insert() method
Step 6: Remove Existing elements using pop() , remove()
Step 7: Display elements from beginning to a range
Step 8: Display elements from end
Step 9: Display elements from specific Index to till the end
Step 10: Display elements within a range
Step 11: Display ary2 Step 12: Display ary2 in reverse order
Step 13: Stop
import array as arr
ary1 = [Link]('i', [1, 2, 3, 4, 5, 6, 7, 8, 9])
ary2 = [Link]('d', [1.1, 2.5, 6.7, 3.5, 2.8, 9.1])
print("Elements of First array are:")
for i in range(len(ary1)):
print(ary1[i])
# Slicing examples
print("ary1[2:8] =", ary1[2:8]) # elements from index 2 to 7
print("ary2[:] =", ary2[:]) # full array
print("ary2[::-1] =", ary2[::-1]) # reversed
print("Elements of Second array are:")
for i in range(len(ary2)):
print(ary2[i])
# Insertion
print("\nInsertion")
[Link](0, 10) # insert at beginning
print("Updated array is", ary1)
# Removal
print("\nRemoval")
[Link](10) # remove first occurrence of 10
print("Updated array is", ary1)
[Link](8) # remove element at index 8
print("Updated array is", ary1)
# Slicing Operations
print("\nSlicing Operation")
print("ary1[:] =", ary1[:]) # full array
print("ary1[-1:] =", ary1[-1:]) # last element
print("ary1[2:] =", ary1[2:]) # from index 2 till end
OUTPUT:-
Elements of First array are:
1
2
3
4
5
6
7
8
9
ary1[2:8] = array('i', [3, 4, 5, 6, 7, 8])
ary2[:] = array('d', [1.1, 2.5, 6.7, 3.5, 2.8, 9.1])
ary2[::-1] = array('d', [9.1, 2.8, 3.5, 6.7, 2.5, 1.1])
Elements of Second array are:
1.1
2.5
6.7
3.5
2.8
9.1
Insertion
Updated array is array('i', [10, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Removal
Updated array is array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9])
Updated array is array('i', [1, 2, 3, 4, 5, 6, 7, 8])
Slicing Operation
ary1[:] = array('i', [1, 2, 3, 4, 5, 6, 7, 8])
ary1[-1:] = array('i', [8])
ary1[2:] = array('i', [3, 4, 5, 6, 7, 8])