Python Programming Lab Manual
Python Programming Lab Manual
Name
B.Tech CSE ICS-552 Python Programming Lab 5
year = 2000
# To get year (integer input) from the user
# year = int(input("Enter a year: "))
# divided by 100 means century year (ending with 00)
# century year divided by 400 is leap year
if (year % 400 == 0) and (year % 100 == 0):
print("{0} is a leap year".format(year))
# not divided by 100 means not a century year
# year divided by 4 is a leap year
elif (year % 4 ==0) and (year % 100 != 0):
print("{0} is a leap year".format(year))
# if not divided by both 400 (century year) and 4 (not century year)
# year is not leap year
else:
print("{0} is not a leap year".format(year))
total = hindi+eng+maths+sci+sst
per = total/5
num = 10
for i in range (2,num):
if num % i == 0:
print("Not Prime")
break
else:
print("prime")
def is_prime(n):
if n <= 1:
return False
i=2
while i*i <= n:
if n % i == 0:
return False
i += 1
return True
Output: 2: True
3: True
4: False
5: True
num1 = 12
num2 = 14
for i in range(max(num1, num2), 1 + (num1 * num2), max(num1, num2)):
if i % num1 == i % num2 == 0:
lcm = i
break
print("LCM of", num1, "and", num2, "is", lcm)
Output
LCM of 12 and 14 is 84
for i in num:
sum = sum + int(i)
print(sum)
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
9. ) # Write a Python program to display all the permutations of given string (don’t use
python permutation function)
if i == len(string):
print("".join(string))
for j in range(i, len(string)):
# swap
words[i], words[j] = words[j], words[i]
get_permutation(words, i + 1)
print(get_permutation('yup'))
9) # Consider a scenario from SVU. Given below are two Sets representing the names
of students enrolled for a particular course: java course = {"Anmol", "Rahul",
"Priyanka", "Pratik"} python course = {"Rahul", "Ram", "Nazim", "Vishal"}
Write a Python program to list the number of students enrolled for:
1. Python course
2. Java course only
3. Python course only
4. Both Java and Python courses
5. Either Java or Python courses but not both
6. Either Java or Python
# Given Sets
java_course = {"Anmol", "Rahul", "Priyanka", "Pratik"}
python_course = {"Rahul", "Ram", "Nazim", "Vishal"}
# 5. Students enrolled for Either Java or Python courses but not both
either_course_not_both_students = (java_course | python_course) - both_courses_students
print("Students enrolled for Either Java or Python courses but not both:",
either_course_not_both_students)
Parameters:
- name: The name of the person to greet.
- greeting: The greeting message (default is "Hello").
- punctuation: The punctuation at the end of the greeting (default is "!").
Returns:
A formatted greeting message.
"""
return f"{greeting}, {name}{punctuation}"
if __name__ == "__main__":
main()
11.) #Write a python program to write few lines on a file, read it back and create a
dictionary having each word in file as keys in dictionary and occurrence of these
word as values and print the dictionary.
def create_word_occurrence_dictionary(file_path):
"""Read lines from a file, create a dictionary with word occurrences."""
word_occurrences = {}
with open(file_path, 'r') as file:
for line in file:
words = line.split()
for word in words:
# Remove punctuation and convert to lowercase for simplicity
word = word.strip('.,?!:;\'"()[]{}').lower()
word_occurrences[word] = word_occurrences.get(word, 0) + 1
return word_occurrences
def main():
# Write lines to a file
lines_to_write = [
"This is a sample line.",
"Another line with some words.",
"Sample line for word occurrence dictionary."
]
file_path = "sample_file.txt"
write_lines_to_file(file_path, lines_to_write)
# Read lines from the file and create a word occurrence dictionary
word_occurrences = create_word_occurrence_dictionary(file_path)
if __name__ == "__main__":
main()