Python

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

abhipreet-singh-01

April 12, 2024

[2]: import matplotlib.pyplot as plt


import numpy as np

# 1. Print the second item in the flowers list.


flowers = ["lily", "sunflower", "rose"]
print(flowers[1])

# 2. Use range of indexes to print the third, fourth, and fifth item in the␣
↪list.

flowers = ["lily", "sunflower", "rose", "lotus", "tulip", "hibiscus", "orchid"]


print(flowers[2:5])

# 3. Use the insert method to add "lotus" as the second item in the flowers␣
↪list.

flowers.insert(1, "lotus")
print(flowers)

# 4. Create three lists of numbers and combine them as one data set and create␣
↪a violin plot for them.

data1 = np.random.normal(loc=0, scale=1, size=100)


data2 = np.random.normal(loc=1, scale=1.5, size=100)
data3 = np.random.normal(loc=2, scale=0.5, size=100)
combined_data = [data1, data2, data3]

plt.violinplot(combined_data)
plt.xlabel('Dataset')
plt.ylabel('Value')
plt.title('Violin Plot of Combined Datasets')
plt.show()

# 5. Show the use of any four Matplotlib commands


x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

plt.figure(figsize=(8, 6)) # Command 1


plt.plot(x, y1, label='sin(x)') # Command 2

1
plt.plot(x, y2, label='cos(x)') # Command 3
plt.xlabel('x') # Command 4
plt.ylabel('y') # Set the label for y-axis
plt.legend() # Display legend
plt.title('Plot of sin(x) and cos(x)') # Set the title of the plot
plt.grid(True) # Show grid
plt.show()

sunflower
['rose', 'lotus', 'tulip']
['lily', 'lotus', 'sunflower', 'rose', 'lotus', 'tulip', 'hibiscus', 'orchid']

2
[ ]: while True:
print("\nMenu:")
print("1. Multiplication of a table")
print("2. Prime number check")
print("3. Sum of all numbers up to N using Recursion")
print("4. Exit")
choice = input("Enter your choice: ")

if choice == '1':
num = int(input("Enter a number: "))
print(f"Multiplication Table of {num}:")
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
elif choice == '2':
num = int(input("Enter a number: "))
if num <= 1:
print(f"{num} is not a prime number.")
else:
is_prime = True
for i in range(2, int(num**0.5) + 1):

3
if num % i == 0:
is_prime = False
break
if is_prime:
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
elif choice == '3':
n = int(input("Enter a number: "))
def sum_up_to_n_recursive(n):
if n == 1:
return 1
return n + sum_up_to_n_recursive(n - 1)
print(f"Sum of all numbers up to {n} using recursion:␣
↪{sum_up_to_n_recursive(n)}")

elif choice == '4':


print("Exiting program.")
break
else:
print("Invalid choice. Please enter a valid option.")

Menu:
1. Multiplication of a table
2. Prime number check
3. Sum of all numbers up to N using Recursion
4. Exit
Enter your choice: 1
Enter a number: 2
Multiplication Table of 2:
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

Menu:
1. Multiplication of a table
2. Prime number check
3. Sum of all numbers up to N using Recursion
4. Exit
Enter your choice: 2

4
Enter a number: 45
45 is not a prime number.

Menu:
1. Multiplication of a table
2. Prime number check
3. Sum of all numbers up to N using Recursion
4. Exit
Enter your choice: 3
Enter a number: 88
Sum of all numbers up to 88 using recursion: 3916

Menu:
1. Multiplication of a table
2. Prime number check
3. Sum of all numbers up to N using Recursion
4. Exit

[ ]:

You might also like