You are on page 1of 11

CS194 Problem solving using python 22000423 Visha Jain

ASSIGNMENT 8
Practical-1
Aim: Write a program to get Current Date and Time using Python

Input:

import datetime

now = datetime.datetime.now()

print("Current date and time: ")

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()

yesterday = today - datetime.timedelta(days=1)

tomorrow = today + datetime.timedelta(days=1)

1
CS194 Problem solving using python 22000423 Visha Jain

print("Yesterday's date:", yesterday)

print("Today's date:", today)

print("Tomorrow's date:", tomorrow)

Output:

Practical-3
Aim: Python program to convert time from 12 hour to 24 hour format.

Input:

import datetime

time_12hour = "07:30 PM"

time_24hour = datetime.datetime.strptime(

time_12hour, '%I:%M %p').strftime('%H:%M')

print("Time in 24-hour format:", time_24hour)

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

date_str = "2023-05-03 14:00:00"

timestamp = datetime.datetime.strptime(

date_str, '%Y-%m-%d %H:%M:%S').timestamp()

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"

date_obj = datetime.datetime.strptime(date_str, '%Y-%m-%d')

day_of_week = date_obj.weekday()

days = ['Monday', 'Tuesday', 'Wednesday',

'Thursday', 'Friday', 'Saturday', 'Sunday']

print("Day of the week:", days[day_of_week])

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)

print("Random value:", random_value)

Output:

Practical-2
Aim:Python program to generate random integers between the given range.

Input:

import random

random_int = random.randint(1, 10)

print("Random integer between 1 and 10:", random_int)

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)

print("Random element:", random_element)

Output:

Practical-4
Aim: WAP to Shuffle List in Python.

Input:

import random

my_list = [1, 2, 3, 4, 5]

random.shuffle(my_list)

print("Shuffled list:", 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:

def multiply(x, y): return x * y

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]

def square_cube(x): return (x**2, x**3)

result = list(map(square_cube, my_list))

print("Squared and cubed list:", result)


7
CS194 Problem solving using python 22000423 Visha Jain

Output:

Practical-3
Aim:Write a Python program to sort a list of tuples and dictionaries using Lambda.

Input:

my_list = [(2, 'b'), (1, 'a'), (3, 'c')]

sorted_list = sorted(my_list, key=lambda x: x[0])

print("Sorted list of tuples:", sorted_list)

my_dict = {'d': 4, 'c': 3, 'a': 1, 'b': 2}

sorted_dict = dict(sorted(my_dict.items(), key=lambda x: x[1]))

print("Sorted dictionary:", sorted_dict)

Output:

Practical-4
Aim: Write a Python program to find if a given string starts with a given character using Lambda.

Input:

my_list = ['apple', 'banana', 'cherry', 'orange']

starts_with_c = filter(lambda x: x.startswith('c'), my_list)

print("Strings starting with 'c':", list(starts_with_c))

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

Aim: Write a Python program to create Fibonacci series up to n using Lambda.


Input:
fibonacci = lambda n: [0, 1] if n == 0 else [0, 1] + [fibonacci(i-1)[-1] + fibonacci(i-2)[-1] for i in
range(2, n+1)]
n = int(input("Enter a number: "))
fibonacci_series = fibonacci(n)
print("Fibonacci series up to", n, ":", fibonacci_series)

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

You might also like