Python (2)
Python (2)
University Of Delhi
Mathematical Physics
Assignment
1
2
What is python?
Python is an easy to learn, powerful programming
language. It has efficient high level data structures
and a simple but effective approach to object
oriented programming. It was created by Guido
van Rossum and released in 1991.
Numeric type
Complex
Complex number are written with 'j' as imaginary part.
4
Lists
# Creating a list
list1 = [1, 2, 3, 4, 5]
# Appending to a list
list1.append(6)
print(list1)
# Sorting a list
list1.sort()
print(list1)
# Reversing a list
list1.reverse()
print(list1)
Tuples
5
# Creating a tuple
tuple1 = (1, 2, 3, 4, 5)
# Indexing a tuple
print(tuple1[0])
# Slicing a tuple
print(tuple1[1:4])
# Tuple Concatenation
tuple2 = (6, 7, 8)
print(tuple1 + tuple2)
Dictionaries
# Creating a dictionary
dict1 = {'name': 'John', 'age': 30, 'country':
'USA'}
String
String are character or word in python
surrounded by " ",' ' quotation marks.
8
Quadratic equation:
Numpy :
Numpy is python library used for working with
array. It also has function for work in domain
of linear algebra, Fourier transform, and
matrices.
Numpy was created in 2005 by Travis
Oliphant. It is an open-source projact and you
can see it freely .
Use of Numpy :
In python we have lists that serve the purpose
of array, but they are slow to process. Numpy
13
Import Numpy
Once Numpy is installed, import it in your
application by adding the "import" keyword
e.g. "import Numpy".
Now Numpy is imported and ready to use.
14
Dimensions in arrays:
1. 1-D array :
2. 2-D array:
3. 3-D array:
15
import numpy as np
a = np.array([1, 2, 3, 4])
print(a)
Output:
[1 2 3 4]
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
17
print(b)
Output:
[1 2 3 4]
[[1 2 3]
[4 5 6]]
NumPy Array Indexing:
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
print(a[2])
print(a[-1])
Output:
3
6
18
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
print(a[2:4])
print(a[:4])
print(a[2:])
Output:
[3 4]
[1 2 3 4]
[3 4 5 6]
NumPy Data Types:
import numpy as np
a = np.array([1, 2, 3, 4],
dtype='float32')
19
print(a.dtype)
Output:
float32
NumPy Copy vs View:
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
b = a[2:4]
b[0] = 10
print(a)
Output:
[ 1 2 10 4 5 6]
NumPy Array Shape:
20
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
print(a.shape)
Output:
(6,)
NumPy Array Reshape:
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
b = a.reshape((2, 3))
print(b)
Output:
[[1 2 3]
[4 5 6]]
NumPy Array Iterating:
21
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
for x in a:
print(x)
Output:
1
2
3
4
5
6
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
22
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
import numpy as np
a = np.array([3, 4, 1, 6, 2, 5])
Matplotlib:
Matplotlib is low level graph plotting library in
python that serves as a visualisation utility.
24