CoCalc Public FilesHandouts / practice.ipynbOpen with one click!
Author: Freya Grant
Views : 36
Compute Environment: Ubuntu 20.04 (Default)

Practice & Refresh

  1. Write a Python program to compute the distance between the points (x1, y1) and (x2, y2).
In [1]:
import math p1 = [4, 0] p2 = [6, 6] distance = math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) ) print(distance)
6.324555320336759
  1. Write a Python program to calculate the hypotenuse of a right angled triangle.
In [ ]:
import math p1 = [4, 0] p2 = [6, 6] distance = math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) ) print(distance)
  1. Write a Python program to plot on the same figure y1=sin(x) and y2=0.5, and configure the range of y-axis to (-2,2)
In [25]:
import numpy as np import matplotlib.pyplot as plt x=np.linspace(-2*np.pi, 2*np.pi,500) y1 = np.sin(x) plt.plot(x,y1) a = np.empty(500) a.fill(0.5) plt.plot(x,a)
[<matplotlib.lines.Line2D at 0x7f97293c2eb0>]
  1. Write a Python program to create an array of 10 zeros,10 ones, 10 fives.
In [30]:
x1 =np.zeros(10) print(x1) x2 = np.ones(10) print(x2) x3 = np.empty(10) x3.fill(5) print(x3)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [5. 5. 5. 5. 5. 5. 5. 5. 5. 5.]
  1. Write a Python program to create a 3x3 identity matrix, i.e. diagonal elements are 1, the rest are 0.
In [31]:
x = np.eye(3) print(x)
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
  1. Write a Python program to plot Gaussian distribution with mean=0 and standard deviation = 1.
In [36]:
import scipy.stats as stats import math mu = 0 sigma = 1 x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100) plt.plot(x, stats.norm.pdf(x, mu, sigma)) plt.show()
In [ ]: