1.
Program 1
import random
def roll_dice():
return [Link](1, 6)
# Example usage
print("Dice roll result:", roll_dice())
2. Program 2
3. Program 3
def rotate_list(lst):
if not lst:
return lst
return [lst[-1]] + lst[:-1]
# Example usage
example_list = [1, 2, 3, 4, 5]
print("Original List:", example_list)
print("Rotated List:", rotate_list(example_list))
4. Program 4
# Initialize the list of users
users = []
def add_user(name, user_id, city):
"""
Adds a user to the global users list.
:param name: Name of the user
:param user_id: Unique ID of the user
:param city: City of the user
"""
[Link]({"name": name, "id": user_id, "city": city})
def remove_user(user_id):
"""
Removes a user with the specified ID from the users list.
:param user_id: ID of the user to be removed
"""
global users
for user in users:
if user["id"] == user_id:
[Link](user)
print(f"User with ID {user_id} removed.")
return
print(f"User with ID {user_id} not found.")
def display_user(user_id=None):
"""
Displays user details. If no ID is passed, displays all users.
:param user_id: Optional, ID of the user to display
"""
if user_id:
for user in users:
if user["id"] == user_id:
print(user)
return
print(f"User with ID {user_id} not found.")
else:
if users:
for user in users:
print(user)
else:
print("No users to display.")
# Example usage
add_user("Virat Kohli", "18", "Delhi")
add_user("Rohit Sharma", "45", "Nagpur")
print("\nAll users:")
display_user()
print("\nDisplay specific user (ID: 18):")
display_user("18")
remove_user("18")
print("\nAfter removing user with ID 18:")
display_user()
5. Program 5
def convert_number(num):
print("Binary:", bin(num))
print("Octal:", oct(num))
print("Hexadecimal:", hex(num))
# Example usage
convert_number(42)
6. Program 6
def count_now(places):
for place in [Link]():
if len(place) > 5:
print([Link]())
# Example usage
places = {1: "Delhi", 2: "London", 3: "Paris", 4: "New York", 5: "Doha"}
count_now(places)
7. Program 7
def len_words(string):
return tuple(len(word) for word in [Link]())
# Example usage
example_string = "Come let us have some fun"
print("Lengths of words:", len_words(example_string))
8. Program 8
def index_list(lst):
return [i for i, value in enumerate(lst) if value != 0]
# Example usage
example_list = [12, 4, 0, 11, 0, 56]
print("Indices of non-zero elements:", index_list(example_list))
9. Program 9
def push_students_to_stack(student_dict):
stack = []
for name, marks in student_dict.items():
if marks > 75:
[Link](name)
return stack
def pop_students_from_stack(stack):
while stack:
print("Popped:", [Link]())
# Example usage
students = {"OM": 76, "JAI": 45, "BOB": 89, "ALI": 65, "ANU": 90, "TOM": 82}
stack = push_students_to_stack(students)
pop_students_from_stack(stack)