In [1]:
x = [12, 34, 56, 54, 83, 95, 34, 38, 39, 19, 17, 28]
x1 = min(x)
x2 = max(x)
print("min =", x1, "max =", x2)
min = 12 max = 95
In [2]:
# Import complex math module
import math
import cmath
a = 1
b = 5
c = 6
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
if (d < 0):
root1 = (-b + cmath.sqrt(d)) / (2*a)
root2 = (-b - cmath.sqrt(d)) / (2*a)
else:
root1 = (-b + math.sqrt(d)) / (2*a)
root2 = (-b - math.sqrt(d)) / (2*a)
print("The solutions are", root1, "and", root2)
# (ii) Factorial
num = 7
factorial = 1
# check if the number is negative or zero
if(num < 0):
print("Factorial does not exist for negative numbers")
elif(num == 0):
print("Factorial of 0 is 1")
else:
for i in range(1, num+1):
factorial *= i
print("The factorial of", num, "is", factorial)
The solutions are -2.0 and -3.0 The factorial of 7 is 5040
In [ ]:
import matplotlib.pyplot as plt
ages = [2, 5, 70, 40, 30, 45, 43, 40, 44, 60, 7, 13, 57, 18, 90, 77, 32, 21, 20, 40]
plt.hist(ages, bins = 10, range= (0, 100), color = "green", histtype='bar', rwidth=0.8)
plt.title("Histogram of Ages")
plt.xlabel("Age")
plt.ylabel("Frequency")
plt.show()
Out[ ]:
Text(0, 0.5, 'Frequency')
In [ ]:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = plt.axes()
x = np.arange(-20, 20, 0.01)
ax.plot(x, np.sin(x))
plt.xlabel("x")
plt.ylabel("$y = sin (x)$")
plt.show()
In [1]:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
plt1 = fig.add_subplot(211)
plt2 = fig.add_subplot(212)
x = np.arange(-10, 10, 0.01)
plt1.plot(x, np.sin(x), color = "red")
plt1.set_title("$y = sin(x)$")
plt2.plot(x, np.cos(x), color = "blue")
plt2.set_title("$y = cos(x)$")
fig.subplots_adjust(hspace=0.5)
plt.show()
In [8]:
# importing required modules
import matplotlib.pyplot as plt
import numpy as np
# function to generate coordinates
def create_plot(ptype):
# setting the x-axis values
x = np.arange(-10, 10, 0.01)
# setting the y-axis values
if ptype == "linear":
y = x
elif ptype == "quadratic":
y = x**2
elif ptype == "cubic":
y = x**3
elif ptype == "quartic":
y = x**4
return (x, y)
# setting a style to use
plt.style.use('fivethirtyeight')
# create a figure
fig = plt.figure()
# define subplots and their positions in figure
plt1 = fig.add_subplot(221)
plt2 = fig.add_subplot(222)
plt3 = fig.add_subplot(223)
plt4 = fig.add_subplot(224)
# plotting points on each subplot
x, y = create_plot("linear")
plt1.plot(x, y, color="r")
plt1.set_title("$y_1 = x$")
x, y = create_plot("quadratic")
plt2.plot(x, y, color="b")
plt2.set_title("$y_2 = x^2$")
x, y = create_plot("cubic")
plt3.plot(x, y, color="g")
plt3.set_title("$y_3 = x^3$")
x, y = create_plot("quartic")
plt4.plot(x, y, color="k")
plt4.set_title("$y_4 = x^4$")
# adjusting space between subplots
fig.subplots_adjust(hspace=0.5, wspace=0.5)
# function to show the plot
plt.show()