harshit python
harshit python
of
“Python Programming”
(Subject CODE:- - PMD 102)
Submitted To :- Submitted By :-
Dr. Jyoti Parsola Harshit Rawat
14
M.C.A. AI&DS
1st Sem
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
print(total_seconds_in_year())
Explanation:
total_seconds_in_year():
Calculates the total number of seconds in a non-leap year (365 days).
Performs the calculation by multiplying the number of seconds in a minute, minutes
in an hour, hours in a day, and days in a year.
Returns the total number of seconds, which is then displayed.
Output:
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
Q-2 Write a program to display all the numbers in the range (151, 180) whose first and
last digit are the same.
Source Code:
Python
def first_last_same():
for num in range(151, 181):
num_str = str(num)
if num_str[0] == num_str[-1]:
print(num)
first_last_same()
Explanation:
first_last_same():
Loops through numbers between 151 and 180.
Converts each number to a string and checks if the first and last digits match.
Prints numbers where the first and last digits are the same.
Output:
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
months_with_j()
Explanation:
months_with_j():
Iterates over a list of month names.
Uses startswith("J") to check if a month begins with 'J'.
Prints months that start with 'J' (January, June, July).
Output:
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
Q-4 Write a program to display the electricity bill based on the following conditions:
First 100 units = 100 Rs
101-200 units = 150 Rs
201-300 units = 200 Rs
Above 300 units = 250 Rs
Source Code:
Python
def electricity_bill(units):
if units <= 100:
return units * 1
elif units <= 200:
return 100 + (units - 100) * 1.5
elif units <= 300:
return 250 + (units - 200) * 2
else:
return 450 + (units - 300) * 2.5
Explanation:
electricity_bill(units):
Implements tiered pricing for electricity bills based on the number of units consumed.
Uses conditional statements to calculate the bill in different ranges.
Returns the total bill amount for the given units.
Output:
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
Q-5 Write a program that accepts a sequence of comma-separated numbers from the
console and generates a list and a tuple containing every number.
Source Code:
Python
def list_and_tuple():
numbers = input("Enter comma-separated numbers: ")
num_list = numbers.split(",")
num_tuple = tuple(num_list)
print("List:", num_list)
print("Tuple:", num_tuple)
list_and_tuple()
Explanation:
list_and_tuple():
Takes comma-separated user input and splits it into a list using split(",").
Converts the list into a tuple.
Prints both the list and tuple versions of the input.
Output:
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
Q-6 Write a program to create a list that stores all the characters from a string.
Source Code:
Python
def string_to_char_list(string):
return list(string)
Explanation:
string_to_char_list(string):
Converts a given string into a list of individual characters using list().
Returns the list of characters.
Useful for operations requiring character-level manipulation.
Output:
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
Q-7 Write a program to remove the duplicate elements from a tuple without using any
predefined function.
Source Code:
Python
def remove_duplicates(tup):
result = []
for item in tup:
if item not in result:
result.append(item)
return tuple(result)
tup = (1, 2, 2, 3, 4, 2, 3, 4, 4, 5, 4, 5)
print(remove_duplicates(tup))
Explanation:
remove_duplicates(tup):
Iterates through a tuple and removes duplicate elements.
Constructs a new list containing only unique items.
Converts the list back into a tuple and returns it.
Output:
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
Q-8 Write a program to create a dictionary named Employee from two lists, emp_id
and emp_name. Now perform the following actions:
Display all the employee IDs and names.
Display all the values of the keys that are 3 digits long.
Display all the values that are the same and also display their corresponding keys.
Display all the keys that are divisible by 5 but not by 7.
Source Code:
Python
def create_employee_dict(emp_id, emp_name):
return dict(zip(emp_id, emp_name))
def display_employees(employee):
print("Employee ID and Names:")
for emp_id, emp_name in employee.items():
print(f"{emp_id}: {emp_name}")
def values_of_3_digit_keys(employee):
print("\nEmployees with 3-digit IDs:") # Added newline for better readability
for emp_id, emp_name in employee.items():
if len(str(emp_id)) == 3:
print(f"{emp_id}: {emp_name}")
def same_values_and_keys(employee):
print("\nEmployees with same names:") # Added newline for better readability
name_count = {}
for emp_id, emp_name in employee.items():
name_count[emp_name] = name_count.get(emp_name, 0) + 1
for emp_id, emp_name in employee.items():
if name_count[emp_name] > 1:
print(f"{emp_id}: {emp_name}")
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
def divisible_by_5_not_7(employee):
print("\nEmployees with IDs divisible by 5 but not 7:") # Added newline for better readability
for emp_id in employee:
if emp_id % 5 == 0 and emp_id % 7 != 0:
print(emp_id)
emp_id = [101, 202, 303, 504, 600]
emp_name = ["Alice", "Bob", "Charlie", "Bob", "David"]
employee = create_employee_dict(emp_id, emp_name)
display_employees(employee)
values_of_3_digit_keys(employee)
same_values_and_keys(employee)
divisible_by_5_not_7(employee)
Explanation:
create_employee_dict() and related functions:
create_employee_dict() creates a dictionary mapping employee IDs to names using
zip().
The other functions filter or display the employee dictionary, e.g., filtering IDs with
specific patterns or names.
Demonstrates operations on dictionaries like filtering based on conditions.
Output:
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
Q-9 Write a program with a list containing string data type elements. Store these
strings as keys and their corresponding values as the number of vowels occurring in the
string.
Source Code:
Python
def count_vowels(string):
vowels = "aeiouAEIOU"
return sum(1 for char in string if char in vowels)
def string_vowel_count(strings):
return {string: count_vowels(string) for string in strings}
Explanation:
count_vowels() and string_vowel_count():
count_vowels() counts the vowels in a given string by checking each character.
string_vowel_count() creates a dictionary mapping strings to their vowel counts.
Useful for analyzing the vowel content of multiple strings.
Output:
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
Q-10 Write a program with a long string. Display all the words in the string that
contain the character "t".
Source Code:
Python
def words_with_t(sentence):
words = sentence.split()
return [word for word in words if 't' in word]
Explanation:
words_with_t(sentence):
Splits a sentence into individual words.
Filters and returns words that contain the letter 't'.
Highlights how to search for specific characters within words in a sentence.
Output:
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
Q-11 Write a program with a function view that takes a string parameter and displays
the abbreviation of the string.
Source Code:
Python
def abbreviate_string(string):
words = string.split()
abbreviation = '.'.join([word[0].upper() for word in words]) + '.'
return abbreviation
Explanation:
abbreviate_string(string):
o Splits a string into words and takes the first letter of each word.
o Joins the first letters with dots to form an abbreviation.
o Converts the first letters to uppercase for standard abbreviation formatting.
Output:
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
Q-12 Write a program with a function interest that calculates simple interest.
Source Code:
Python
def simple_interest(principal, rate, time):
return (principal * rate * time) / 100
Explanation:
simple_interest(principal, rate, time):
Calculates simple interest using the formula ((P × R × T) / 100).
Takes user inputs for principal, rate, and time.
Returns the calculated simple interest.
Output:
NAME: HARSHIT RAWAT MCA (AI&DS) 1ST SEM ROLL NO. - 2401524
Q-13 Write a program with a function create_dictionary(tuple, tuple) that creates a new
dictionary. Another function show_dictionary(dict) displays the items of the dictionary
that have float values.
Source Code:
Python
def create_dictionary(keys, values):
return dict(zip(keys, values))
def show_dictionary(dictionary):
print("Items with float values:")
for key, value in dictionary.items():
if isinstance(value, float):
print(f"{key}: {value}")
Explanation:
create_dictionary(keys, values) and show_dictionary(dictionary):
create_dictionary() forms a dictionary from two lists using zip().
show_dictionary() filters and prints dictionary items where the values are of type
float.
Useful for handling and displaying specific data types within a dictionary.
Output: