0% found this document useful (0 votes)
5 views6 pages

Day 2 Morning

Basic Python Solutions

Uploaded by

Sai Rupa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Day 2 Morning

Basic Python Solutions

Uploaded by

Sai Rupa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Program to print the numbers from 1 to n using range():


Solution:

n = int(input("Enter a number: "))


for i in range(1, n + 1): # range starts from 1 and ends at n
print(i)

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

2. Program to print the last 4 numbers using range():


Solution:

for i in range(6, 10): # Prints numbers 6, 7, 8, 9


print(i)

Or

x=[10,20,30,40,50,60,70,80,90]
for i in x[-4:]:
print(i)
Output: 60 70 80 90

3. Program to print the square pattern using a range of n:


Solution:

n = int(input("Enter a number: "))


for i in range(1, n + 1):
for j in range(i):
print("*", end=" ")
print() # Moves to the next line after each row

Or

n=6 #The n value is your choice


for i in range(n)
for i in range(n)
print(“*”, end=””)
print()

Output:
******
******
******
******
******
******

4. Python program calculates the product of all elements in a list:


Solution:

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

5. Write a program to demonstrate working with tuples in Python:


Solution:

# Create a tuple
my_tuple = (10, 20, 30, 40)

# Print the list of all items in tuple


print("All items:", my_tuple)

# Print the second item in the tuple


print("Second item:", my_tuple[1]) # Index 1 represents the second item

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

7. Add, remove, and insert elements into a list:


Solution:

my_list = [1, 2, 3]

# Add element to the list


my_list.append(4)
print("After adding 4:", my_list)

# Remove an element from the list


my_list.remove(2)
print("After removing 2:", my_list)

# Insert element at a specific position


my_list.insert(1, 5) # Insert 5 at index 1
print("After inserting 5 at index 1:", my_list)

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"}

# Print the dictionary items


print("Dictionary items:", my_dict)

# Access items using keys


print("Name:", my_dict["name"])

# Use get() to access items


print("Age:", my_dict.get("age"))
Output:
Dictionary items: {'name': 'Alice', 'age': 25, 'city': 'New York'}
Name: Alice
Age: 25

Or

dict={
"name":"Prabhas",
"age":40,
"city":"Hyderabad",
"job":"Film Actor"
}
#print the dictionary items
print("Dictionary Items: ")
print(dict)

#Accessing dictionary irems using keys


print("Access Items: ")
print("name: ",dict["name"])
print("age: ", dict["age"])

#use get method


print("Use get method: ")
print("city: ",dict.get("city"))
print("job: ",dict.get("job"))
Output:
Dictionary Items:
{'name': 'Prabhas', 'age': 40, 'city': 'Hyderabad', 'job': 'Film Actor'}
Access Items:
name: Prabhas
age: 40
Use get method:
city: Hyderabad
job: Film Actor

9. Write a Python program to generate the sequence [10, 9, 8, 7, 6] using


range() and a for loop:
Solution:

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

11. Write a program to convert the tuple into a list:


Solution:

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}

You might also like