0% found this document useful (0 votes)
8 views

Python Practical Practice Word

Uploaded by

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

Python Practical Practice Word

Uploaded by

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

Q1.

Write a program that finds the sum of all multiples of 3 and 5 below 1000 using a
for loop.
Q2. Write a program that asks the user to guess a number between 1 and 10. The pro-
gram should repeatedly ask the user to guess the number until they get it right, and
keep track of how many guesses it took.
Q3. Write a program that creates a NumPy array of random integers and calculates
the mean, median, and mode.
Q4. Write a program that uses a for loop to iterate over a NumPy array and print the
square of each element.
Q5. Plot a line graph for the function y = 2x + 1 for x values between -10 and 10.
Q6. Write a program to generate a scatter plot of random points. Use 50 random x and
y points.
Q7. Generate 1000 random numbers from a normal distribution using NumPy and plot
a histogram.
Q8. Create a bar graph that shows the number of students in different classes (e.g.,
Class A: 30, Class B: 25, Class C: 35, Class D: 20).
Q9.Write a program to plot a box plot of 3 sets of random data using Matplotlib.
Q10. Write a program to perform element-wise addition, subtraction, multiplication, di-
vision, floor division, and remainder on the following two NumPy arrays

array1 = np.array([10, 20, 30, 40, 50])


array2 = np.array([2, 4, 6, 8, 10])
Solutions:
1.
total_sum = 0
for i in range(1000):
if i % 3 == 0 or i % 5 == 0:
total_sum += i
print(f"Sum of multiples of 3 and 5 below 1000: {total_sum}”)

2.
import random

secret_number = random.randint(1, 10)


guess = None
attempts = 0

while guess != secret_number:


guess = int(input("Guess the number (between 1 and 10): "))
attempts += 1
if guess < secret_number:
print("Too low!")
elif guess > secret_number:
print("Too high!")

print(f"Congratulations! You guessed the number in {attempts} attempts.”)

3.
import numpy as np
from scipy import stats

data = np.random.randint(1, 100, size=20)

mean_value = np.mean(data)
median_value = np.median(data)
mode_value = stats.mode(data)[0][0]

print(f"Array: {data}")
print(f"Mean: {mean_value}, Median: {median_value}, Mode: {mode_value}”)

4.
import numpy as np

data = np.array([1, 2, 3, 4, 5])

for element in data:


print(f"The square of {element} is {element ** 2}”)

5.
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-10, 10, 100)


y=2*x+1

plt.plot(x, y, label="y = 2x + 1")


plt.title("Line Graph")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
plt.grid(True)
plt.show()

6.
import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(50)
y = np.random.rand(50)

plt.scatter(x, y)
plt.title("Random Scatter Plot")
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.show()

7.
import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(1000)

plt.hist(data, bins=30, alpha=0.75)


plt.title("Histogram of Normally Distributed Data")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

8.
import matplotlib.pyplot as plt

classes = ['Class A', 'Class B', 'Class C', 'Class D']


students = [30, 25, 35, 20]

plt.bar(classes, students)
plt.title("Number of Students in Each Class")
plt.xlabel("Classes")
plt.ylabel("Number of Students")
plt.show()

9.
import numpy as np
import matplotlib.pyplot as plt
data1 = np.random.normal(0, 1, 100)
data2 = np.random.normal(5, 2, 100)
data3 = np.random.normal(-2, 0.5, 100)

plt.boxplot([data1, data2, data3], labels=["Data1", "Data2", "Data3"])


plt.title("Box Plot of Random Data")
plt.ylabel("Values")
plt.show()

10.
import numpy as np

# Define the arrays


array1 = np.array([10, 20, 30, 40, 50])
array2 = np.array([2, 4, 6, 8, 10])

# Element-wise addition
add_result = array1 + array2
print(f"Addition: {add_result}")

# Element-wise subtraction
sub_result = array1 - array2
print(f"Subtraction: {sub_result}")

# Element-wise multiplication
mul_result = array1 * array2
print(f"Multiplication: {mul_result}")

# Element-wise division
div_result = array1 / array2
print(f"Division: {div_result}")

# Element-wise floor division


floor_div_result = array1 // array2
print(f"Floor Division: {floor_div_result}")

# Element-wise remainder
remainder_result = array1 % array2
print(f"Remainder: {remainder_result}")

You might also like