Day 2 Morning
Day 2 Morning
Or
n=[1,2,3,4,5,6,7,8,9,10]
range=n
For in range[0:]
print(i)
Output: 1 2 3 4 5 6 7 8 9 10
Or
x=[10,20,30,40,50,60,70,80,90]
for i in x[-4:]:
print(i)
Output: 60 70 80 90
Or
Output:
******
******
******
******
******
******
numbers = [1, 2, 3, 4, 5]
product = 1
for num in numbers:
product *= num # Multiplies each number with the product
print("Product of all elements:", product)
Output:
Product of all elements: 120
# Create a tuple
my_tuple = (10, 20, 30, 40)
Output:
All items: (10, 20, 30, 40)
Second item: 20
6. Write a Python program to print each character of the string "Python"
using a for loop:
Solution:
string = "Python"
for char in string:
print(char)
Output:
P
y
t
h
o
n
my_list = [1, 2, 3]
Output:
After adding 4: [1, 2, 3, 4]
After removing 2: [1, 3, 4]
After inserting 5 at index 1: [1, 5, 3, 4]
8. Create a dictionary and apply the following methods:
● Print dictionary items
● Access items in the list
● Use get()
Solution:
# Create a dictionary
my_dict = {"name": "Alice", "age": 25, "city": "New York"}
Or
dict={
"name":"Prabhas",
"age":40,
"city":"Hyderabad",
"job":"Film Actor"
}
#print the dictionary items
print("Dictionary Items: ")
print(dict)
for i in range(10, 5, -1): # range starts from 10, ends at 6, and decrements by 1
print(i)
Output: 10 9 8 7 6
10. How can you use range() to print only even numbers between 1 and
10?:
Solution:
for i in range(2, 11, 2): # range starts from 2, ends at 10, and increments by 2
print(i)
Output: 2 4 6 8 10
my_tuple = (1, 2, 3, 4)
my_list = list(my_tuple) # Convert tuple to list
print(my_list)
Output: [1, 2, 3, 4]
12. Write a program to demonstrate the use of union (|), intersection (&),
and difference (-) on the sets:
Solution:
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# Union
union_set = set1 | set2
print("Union:", union_set)
# Intersection
intersection_set = set1 & set2
print("Intersection:", intersection_set)
# Difference
difference_set = set1 - set2
print("Difference:", difference_set)
Output:
Union: {1, 2, 3, 4, 5, 6}
Intersection: {3, 4}
Difference: {1, 2}