Professional Documents
Culture Documents
List 8
List 8
ASSIGNMENT 8
Practical-1
Aim: Write a program to get Current Date and Time using Python
Input:
import datetime
now = datetime.datetime.now()
print(now)
Output:
Practical-2
Aim: Python Program to Find yesterday’s, today’s and tomorrow’s date
Input:
import datetime
today = datetime.date.today()
1
CS194 Problem solving using python 22000423 Visha Jain
Output:
Practical-3
Aim: Python program to convert time from 12 hour to 24 hour format.
Input:
import datetime
time_24hour = datetime.datetime.strptime(
Output:
2
CS194 Problem solving using python 22000423 Visha Jain
Practical-4
Aim: WAP to Convert date string to timestamp and timestamp string to datetime in Python
Input:
import datetime
timestamp = datetime.datetime.strptime(
print("Timestamp:", timestamp)
timestamp_str = "1651634400.0"
date_time = datetime.datetime.fromtimestamp(float(timestamp_str))
print("Datetime:", date_time)
Output:
3
CS194 Problem solving using python 22000423 Visha Jain
Practical-5
Aim: python program to find day of the week for a user given date.
Input:
import datetime
date_str = "2023-05-03"
day_of_week = date_obj.weekday()
Output:
4
CS194 Problem solving using python 22000423 Visha Jain
RANDOM MODULE
Practical-1
Aim: WAP for Printing a random value from a list in Python
Input:
import random
my_list = [1, 2, 3, 4, 5]
random_value = random.choice(my_list)
Output:
Practical-2
Aim:Python program to generate random integers between the given range.
Input:
import random
5
CS194 Problem solving using python 22000423 Visha Jain
Output:
Practical-3
Aim: WAP to Randomly Select Elements from tuple in Python.
Input:
import random
my_tuple = (1, 2, 3, 4, 5)
random_element = random.choice(my_tuple)
Output:
Practical-4
Aim: WAP to Shuffle List in Python.
Input:
import random
my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list)
Output:
6
CS194 Problem solving using python 22000423 Visha Jain
LAMBDA FUNCTION
Practical-1
Aim: Write a Python program to create a lambda function that multiplies argument x with argument
y and prints the result.
Input:
print(multiply(4, 5))
Output:
Practical-2
Aim: Write a Python program to square and cube every number in each list of integers using Lambda.
Input:
my_list = [1, 2, 3, 4, 5]
Output:
Practical-3
Aim:Write a Python program to sort a list of tuples and dictionaries using Lambda.
Input:
Output:
Practical-4
Aim: Write a Python program to find if a given string starts with a given character using Lambda.
Input:
8
CS194 Problem solving using python 22000423 Visha Jain
Output:
PRACTICAL-5
Aim: Write a Python program to extract year, month, date and time using Lambda.
Input:
import datetimeHarsh
my_datetime = datetime.datetime.now()
get_year = lambda x: x.year
get_month = lambda x: x.month
get_day = lambda x: x.day
get_time = lambda x: x.time()
year = get_year(my_datetime)
month = get_month(my_datetime)
day = get_day(my_datetime)
time = get_time(my_datetime)
print("Datetime:", my_datetime)
print("Year:", year)
print("Month:", month)
print("Day:", day)
print("Time:", time)
Output:
9
CS194 Problem solving using python 22000423 Visha Jain
PRACTICAL-6
Output:
PRACTICAL-7
Aim: Write a Python program to find the common elements of 2 lists using Lambda.
Input:
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
common_elements = list(filter(lambda x: x in list1, list2))
print("Common elements:", common_elements)
Output:
10
CS194 Problem solving using python 22000423 Visha Jain
11