Data science lab
Data science lab
OUTPUT:
30
10
200
2.0
10240000000000
True
False
False
True
True
False
10
20
False
True
True
False
22
19
57
28.5
0.5
0.0
0.0
30
-21
30
80
True
False
True
False
True
True
False
False
True
True
True
False
a is less than b
a is less than b
a is less than b
OUTPUT:
For loop:
4
While loop:
Nested loops:
i:0, j:0
i:0, j:1
i:1, j:0
i:1, j:1
i:2, j:0
i:2, j:1
OUTPUT:
Break statement
1
2
Continue statement
# sorting a list
fruits.sort()
print("sorted list:",fruits)
OUTPUT:
cherry
mango
apple
#TUPLES
#Creating a tuple
fruits = ("apple", "banana", "cherry")
print("original tuple:",fruits)
#accessing elements
print("first element:",fruits[0])
print("last element:",fruits[2])
# slicing a tuple
print("sliced tuple:",fruits[1:3])
# iterating through a tuple
print("iterating through a tuple:")
for fruit in fruits:
print(fruit)
# checking membership
print("is 'apple' present in the tuple? ", "apple" in fruits)
print("is 'banana' present in the tuple? ", "banana" in fruits)
# tuple length
print("length of the tuple:", len(fruits))
#concatenating tuples
vegetable = ("tomato", "potato")
combined_tuple = fruits + vegetable
print("concatenated tuple:", combined_tuple)
# repeating tuples
repeated = fruit*2
print("repeated tuple:", repeated)
#nested tuples
nested = (fruits,vegetable)
print("nested tuple:", nested)
# tuple unpacking
fruit1, fruit2, fruit3 = fruits
print("unpacked elements:", fruit1, fruit2, fruit3)
#converting a list to a tuple
fruit_list = ["apple", "banana", "cherry"]
fruit_tuple = tuple(fruit_list)
print("converted tuple:", fruit_tuple)
OUPUT:
apple
banana
cherry
#SETS
#Creating a set
fruits = {"apple", "banana", "cherry"}
print("original set:",fruits)
#adding elements
fruits.add("date")
print("set after add:",fruits)
#adding multiple elements
fruits.update(["mango", "grapes","orange"])
print("set after update:",fruits)
#removing elements
fruits.remove("banana")
print("set after remove:",fruits)
#discarding elements(no error if element is not found)
fruits.discard("banana")
print("set after discard:",fruits)
#popping an element(removes a random element)
popped_fruit = fruits.pop()
print("popped element:",popped_fruit)
print("set after pop:",fruits)
OUTPUT:
set after update: {'banana', 'cherry', 'orange', 'mango', 'date', 'apple', 'grapes'}
#DICTIONARY
#Creating a dictionary
student = {"name": "John", "age":20,"courses":["maths","science"]}
print("original dictionary:", student)
#accessing elements
print("name:",student["name"])
print("courses:",student["courses"])
#modify elements
student["age"] = 21
print("modified dictionary:",student)
#adding elements
student["grade"] = "A"
print("dictionary after adding an element:",student)
# removing elements
del student["age"]
print("dictionary after removing an element:",student)
# popping elements
grade = student.pop("grade")
print("popped element:",grade)
print("dictionary after pop:",student)
# iterating through dictionary
print("iterating through dictionary:")
for key, value in student.items():
print(f"{key}:{value}")
# dictionary length
print("length of the dictionary:", len(student))
#nested dictinaries
students = {"student1":{"name": "John", "age":20},"student2":{"name": "Johny", "age":21}}
print("nested dictionary:", students)
#dictionary comprehension
squares = {x:x**2 for x in range(5)}
print("dictionary comprehension:",squares)
OUTPUT:
name: John
dictionary after adding an element: {'name': 'John', 'age': 21, 'courses': ['maths', 'science'],
'grade': 'A'}
dictionary after removing an element: {'name': 'John', 'courses': ['maths', 'science'], 'grade':
'A'}
popped element: A
name:John
courses:['maths', 'science']
nested dictionary: {'student1': {'name': 'John', 'age': 20}, 'student2': {'name': 'Johny', 'age':
21}}
df = pd.DataFrame(data)
print("original dataframe:")
print(df)
#accessing elements
print(df["name"])
print(df[["name", "city"]])
print(df.iloc[1])
print(df.iloc[0:2])
print(df.iloc[1,2])
print(df.at[1,"city"])
print(df)
#removing a column
df = df.drop(columns = ["salary"])
print(df)
#filtered dataframe
filtered_df = df[df["age"]>25]
print(filtered_df)
OUTPUT:
original dataframe:
name age city
0 Alice 25 new york
1 Bob 30 texas
2 Charlie 35 chicago
filtered dataframe:
name age city
1 Bob 30 texas
2 Charlie 35 chicago
time functions
current time(in seconds in epoch): 1748810608.4571116
local time: 2025-06-01 20:43:28
sleeping for 2 seconds...
awake now!
random functions:
random integer between 1 and 10: 1
random float between 0 and 1: 0.8157975325119621
random choice from a list: banana
random sample of 2 elements from a list: [3, 2]
OUTPUT:
Hello Alice!
Hello Sita!
8
15
15
My name is Joe, Iam 26 years old, and I live in Pune.
16
factorial of 5 is: 120
9. Create a module with two functions one for finding Armstrong number, and
second is for testing whether the given string is palindrome or not. Write a
python application to import the above module some other application.
module.py file
# module.py
def armstrong(num):
sum = 0
temp = num
# Calculate the sum of cubes of digits
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# Check if the sum is equal to the original number
if num == sum:
print(f"{num} is an Armstrong number")
else:
print(f"{num} is NOT an Armstrong number")
def palindrome(st):
j = -1
flag = 0
# Loop to compare each character from start and end
for i in st:
if i != st[j]:
flag = 1
break
j -= 1 # Decrement j to check reverse position
if flag == 1:
print(f"{st} is NOT a palindrome")
else:
print(f"{st} is a palindrome")
main.py file
#create another file which imports the above module, let it be main.py
from module import *
num=int(input("enter the number to check it is Armstrong or not:"))
st=input("enter a string to check whether it is palindrome or not:")
armstrong(num)
palindrome(st)
#note: keep both these files in same directory and run main.py file to get the output.
OUTPUT:
enter the number to check it is Armstrong or not:153
enter a string to check whether it is palindrome or not:radar
153 is an Armstrong number
radar is a palindrome
#Other output:
enter the number to check it is Armstrong or not:141
enter a string to check whether it is palindrome or not: hello
141 is NOT an Armstrong number
hello is NOT a palindrome
11. Implement python script to copy file contents from one file to another.
Read the file names using command line arguments.