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

Basic Python

basic python things to learn

Uploaded by

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

Basic Python

basic python things to learn

Uploaded by

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

Experiment No.

1: Computation using Python


Programing

1. First Install the Anaconda


Anaconda is the most popular distribution of Python to implement scientific
computing, e.g., data science, machine learning applications, large-scale data
processing, predictive analytics, etc.

2. Numpy
Numpy is a Python library used for working with arrays and Numpy is short for
"Numerical Python."

3. How to Generate a 1-D, 2-D, and 3-D Array.


1. import numpy as np
2. arr1 = np.array([1, 2, 3, 4, 5])
3. arr2 = np.array([[1, 2, 3], [4, 5, 6]])
4. arr3= np.array([[[1, 2, 3], [4, 5, 6]], [[4, 5, 6], [7, 8, 9]]])
5. print(arr1)
6. print(arr2)
7. print(arr3)

4. Numpy Array Indexing:


1. import numpy as np
2. arr1 = np.array([1, 2, 3, 4, 5])
3. arr2 = np.array([[1, 2, 3], [4, 5, 6]])
4. arr3= np.array([[[1, 2, 3], [4, 5, 6]], [[4, 5, 6], [7, 8, 9]]])
5. print(arr1[0])
6. print(arr2[1,2])
7. print(arr3[0,1,2])

5. Remove Items from Array


1. import numpy as np
2. arr1 = np.array([1, 2, 3, 4, 5])
3. arr2 = np.array([[1, 2, 3], [4, 5, 6]])
4. print(np.delete(arr1, [2]))
5. print(np.delete(arr2, [0], axis=0)) ###delete row
6. print(np.delete(arr2, [1], axis=1) ) ###delete column
7. print(np.delete(arr2, [0,1], axis=1) ) ###delete multiple columns
6. Numpy Array Slicing: The result includes the start index,but excludes the
end index.
1. import numpy as np
2. arr1 = np.array([1, 2, 3, 4, 5])
3. arr2 = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
4. print(arr1[1:3])
5. print(arr1[1:])
6. print(arr1[:3])
7. print(arr2[1, 1:4])
8. print(arr2[0:2, 2])
9. print(arr2[0:2, 1:4])

7. Numpy Array Reshaping


1. import numpy as np
2. arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
3. arr1 = arr.reshape(4, 3)
4. arr2 = arr.reshape(2, 3, 2)
5. print(arr1)
6. print(arr2)
8.Step:
1. import numpy as np
2. arr1 = np.array([1, 2, 3, 4, 5, 6, 7])
3. print(arr1[1:5:2])
4. print(arr1[::2])
9. Joining Numpy Arrays:
1. import numpy as np
2. arr1 = np.array([1, 2, 3])
3. arr2 = np.array([4, 5, 6])
4. arr3 = np.array([[1, 2, 3],[4, 5, 6]])
5. arr4 = np.array([[7, 8, 9],[10, 11, 12]])
6. arr5 = np.concatenate((arr1, arr2))
7. arr6 = np.concatenate((arr3, arr4), axis=0) ##Along Column
8. arr7 = np.concatenate((arr3, arr4), axis=1) ##Along Row
9. print(arr5)
10.print(arr6)
11.print(arr7)
10. Simple Arithmetic :
11. import numpy as np
11.arr1 = np.array([10, -11, -12, 13, -14, 15])
12.arr2 = np.array([20, 21, 22, 23, 24, 25])
13.arr3 = np.add(arr1, arr2)
14.arr4 = np.subtract(arr1, arr2)
15.arr5= np.multiply(arr1, arr2)
16.arr6 = np.divide(arr1, arr2)
17.arr7 = np.power(arr1, arr2)
18.arr9 = np.absolute(arr1)
19.print(arr3)
20.print(arr4)
21.print(arr5)
22.print(arr6)
23.print(arr7)
24.print(arr8)
25.print(arr9)

11. Rounding Decimals:


1. import numpy as np
2. arr1 = np.trunc([-3.1666, 3.6667])##return the float number closest to zero
3. arr2 = np.floor([-3.1666, 3.6667])##rounds off decimal to nearest lower integer.
4. arr3= np.ceil([-3.1666, 3.6667]) ##rounds off decimal to nearest upper integer.
5. print(arr1)
6. print(arr2)
7. print(arr3)

12. Generate Random Numbers:


Random Integers:
1. from numpy import random
2. x1 = random.randint(100) ##Generate a random integer from 0 to 100
3. x2 = random.randint(100, size=(5)) ##1-D array containing 5 random
4. x3 = random.randint(100, size=(3, 5)) ##2-D array with 3 rows and 5 columns
5. print(x1)
6. print(x2)
7. print(x3)
Random floats:
1. from numpy import random
2. x1 = random.rand(5) ##Generate 5 random numbers between 0 to 1
3. x2 = random.rand(3, 5) ##Generate random numbers with 3 rows and 5 columns
4. print(x1)
5. print(x2)

Random Normal Distribution:


1. from numpy import random
2. x1 = random.normal(size=(2, 3)) ##Generate random normal distribution with 3
rows and 5 columns
3. x2 = random.normal(loc=1, scale=2, size=(2, 3)) ##random normal
distribution of size 2x3 with mean at 1 and standard deviation of 2
4. print(x1)
5. print(x2)
13. Matplotlib: Matplotlib is a low-level graph plotting library in python that
serves as a visualization utility.
1. import matplotlib.pyplot as plt
2. import numpy as np
3. xpoints = np.array([0, 2, 4, 6, 10])
4. ypoints = np.array([0, 5, 17, 37, 102])
5. plt.plot/scatter/bar(xpoints, ypoints) ##Draw a line/plot/bar
6. plt.xlabel("X-axis") ##Add label to the x-axis
7. plt.ylabel("Y-axis") ##Add label to the y-axis
8. plt.title("X vs Y") ##Add Title
9. plt.grid() ##Add Grid to the Plot
10.plt.show()

Change in the line no. 5 only


plt.plot(xpoints, ypoints, linestyle= ‘dotted’, linewidth= ‘10’, color= ‘r’,
marker= ‘o’) ##Draw a dotted line and Marker
plt.plot(xpoints, ypoints, linestyle= ‘dashed’, linewidth= ‘10’, color= ‘r’,
marker= ‘*’) ##Draw a dashed line
Marker: To emphasize each point with a specified marker:
1. plt.plot(xpoints, ypoints, marker= ‘o’)
2. plt.plot(xpoints, ypoints, marker= ‘*’)
Marker Description
s square
D Diamond
p pentagon
H Hexagon
^ Triangle Up
< Triangle Left
> Triangle Right

Multiple Lines
1. import matplotlib.pyplot as plt
2. import numpy as np
3. x1 = np.array([0, 1, 2, 3])
4. y1 = np.array([3, 8, 1, 10])
5. x2 = np.array([0, 1, 2, 3])
6. y2 = np.array([6, 2, 7, 11])
7. plt.plot(x1, y1, x2, y2)
8. plt.show()
Subplot
1. import Matplotlib.Pyplot as plt
2. import numpy as np
3. x = np.array([0, 1, 2, 3]) ##plot 1:
4. y = np.array([3, 8, 1, 10])
5. plt.subplot(1, 2, 1)
6. plt.plot(x,y)
7. plt.title(‘Plot 1’)

8. x = np.array([0, 1, 2, 3]) #plot 2:


9. y = np.array([10, 20, 30, 40])
10.plt.subplot(1, 2, 2)
11.plt.plot(x,y)
12.plt.title(‘Plot 2’)

13.plt.suptitle("My Plot") ##Add a title to the entire figure


14.plt.show()

14. Function: A function is a block of code which only runs when it


is called. You can pass data, known as parameters, into a function.
A function can return data as a result.

1. def function(x, y):


2. return 5*(x+y)
3. print(function(3,2))
4. print(function(5, 4))
5. print(function(9,7))

15. For loop:


1. for x in range(6):
2. print(x)
The range() function returns a sequence of numbers, starting from 0 by default,
and increments by 1 (by default), and ends at a specified number.

1. for x in range(2, 6):


2. print(x)
1. for x in range(2, 30, 3):
2. print(x)

16. If else loop:


1. a = 200
2. b = 33
3. if b > a:
4. print("b is greater than a")
5. else:
6. print("b is not greater than a")

1. a = 200
2. b = 33
3. if b > a:
4. print("b is greater than a")
5. elif a == b:
6. print("a and b are equal")
7. else:
8. print("a is greater than b")

You might also like