0% found this document useful (0 votes)
7 views18 pages

Python Programming Basics and Operations

The document outlines a Python programming course led by Dr. K. Kavitha, covering basic operations, indexing, slicing, and iterating with NumPy arrays. Key topics include arithmetic operators, matrix products, universal functions, and aggregate functions. The document also provides examples and explanations of how to manipulate and analyze data using Python's NumPy library.

Uploaded by

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

Python Programming Basics and Operations

The document outlines a Python programming course led by Dr. K. Kavitha, covering basic operations, indexing, slicing, and iterating with NumPy arrays. Key topics include arithmetic operators, matrix products, universal functions, and aggregate functions. The document also provides examples and explanations of how to manipulate and analyze data using Python's NumPy library.

Uploaded by

kavitha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Python Programming

Dr. [Link]
Associate Professor – Department of Computer
Applications
Dr. N.G.P. ARTS AND SCIENCE COLLEGE
Dr. N.G.P.-KALAPATTI ROAD
COIMBATORE-641 048
Tamil Nadu, India
Mobile: +91 9841158445, E-mail: kavitha.k@[Link]

Dr.N.G.P. Arts and Science College 1


Coimbatore,Tamil Nadu, India
CONTENT TO BE DISCUSSED (29.07.2020)
Revision of Previous topics – Question session
Unit II – Introduction to the Python’s World:
 Basic Operations
• Arithmetic Operator
• The Matrix Product
• Increment and Decrement Operators
• Universal Functions
• Aggregate Functions
 Indexing, Slicing and Iterating
• Indexing
• Slicing
• Iterating an Array
2
Unit IV
Basic Operations
Arithmetic Operator
These operators operate element-wise.
Eg:
>>> a = [Link](4)
>>> a
array([0, 1, 2, 3])
>>> a+4
array([4, 5, 6, 7])
>>> b = [Link](4,8)
>>> b
array([4, 5, 6, 7])
>>> a + b
array([ 4, 6, 8, 10]) 3
Arithmetic operators & functions:
Eg:
>>> a * [Link](b)
array([–0. , –0.95892427, –0.558831 , 1.9709598 ])
Arithmetic operators &Multidimensional Arrays:
Eg:
>>> A = [Link](0, 9).reshape(3, 3)
>>> A
array([[0, 1, 2],[3, 4, 5],[6, 7, 8]])
>>> B = [Link]((3, 3))
>>> B
array([[ 1., 1., 1.],[ 1., 1., 1.],[ 1., 1., 1.]])
>>> A * B
array([[ 0., 1., 2.],[ 3., 4., 5.],[ 6., 7., 8.]]) 4
The Matrix Product:
- This is done by using the dot() function
Eg:
i) >>> [Link](A,B)
array([[ 3., 3., 3.],[ 12., 12., 12.],[ 21., 21., 21.]])

ii) >>> [Link](B)


array([[ 3., 3., 3.],[ 12., 12., 12.],[ 21., 21., 21.]])

iii) Matrix product is not commutative(A * B is not equal to B * A)


>>> [Link](B,A)
array([[ 9., 12., 15.],[ 9., 12., 15.],[ 9., 12., 15.]])
5
Increment & Decrement Operators:
- To increment += is used.
- To decrement -= is used.
Eg:
>>> a = [Link](4)
>>> a
array([0, 1, 2, 3])
>>> a += 1
>>> a
array([1, 2, 3, 4])
>>> a –= 1
>>> a
array([0, 1, 2, 3])
>>> a *= 2
>>> a 6
Universal Functions:
- Generally called ufunc.
Eg:
>>> a = [Link](1, 5)
>>> a
array([1, 2, 3, 4])

>>> [Link](a)
array([ 1. , 1.41421356, 1.73205081, 2. ])

>>> [Link](a)
array([ 0. , 0.69314718, 1.09861229, 1.38629436])

>>> [Link](a)
array([ 0.84147098, 0.90929743, 0.14112001, –0.7568025 ]) 7
Aggregate Functions:
- Functions that perform an operation on a set of values & produce a
single result.
Eg:
>>> a = [Link]([3.3, 4.5, 1.2, 5.7, 0.3])
>>> [Link]()
15.0
>>> [Link]()
0.29999999999999999
>>> [Link]()
5.7000000000000002
>>> [Link]()
3.0
>>> [Link]()
2.0079840636817816 8
Indexing, Slicing and Iterating
- select certain elements through indexing
- To obtain views of values contained in the array through slicing
- To make iterations within them using for loop
Indexing:
To refer individual element(positive indexing):
>>> a = [Link](10, 16)
>>> a
array([10, 11, 12, 13, 14, 15])
>>> a[4]
14
To refer individual element(negative indexing):
>>> a[–1]
9
15
To select multiple items:
>>> a[[1, 3, 4]]
array([11, 13, 14])
Multidimensional Array - Indexing:
>>> A = [Link](10, 19).reshape((3, 3))
>>> A
array([[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
>>> A[1, 2]
15

10
Slicing:
Slicing is the operation which allows you to extract portions of an array
to generate new ones.
Syntax:
array[start index: end index : skip count]
Eg:
>>> a = [Link](10, 16)
>>> a
array([10, 11, 12, 13, 14, 15])
>>> a[1:5]
array([11, 12, 13, 14])
To skip & slice:
>>> a[1:5:2]
array([11, 13]) 11
Default Values:
Start index : 0
End index : maximum index value
Skip count : 1
>>> a[::2]
array([10, 12, 14])
>>> a[:5:2]
array([10, 12, 14])
>>> a[:5:]
array([10, 11, 12, 13, 14])
Slicing in 2D arrays:
>>> A = [Link](10, 19).reshape((3, 3))
>>> A
array([[10, 11, 12],
[13, 14, 15], 12
>>> A[0,:]
array([10, 11, 12])
>>> A[:,0]
array([10, 13, 16])
>>> A[0:2, 0:2]
array([[10, 11],
[13, 14]])

Skip rows or columns during slicing:


>>> A[[0,2], 0:2]
array([[10, 11],
[16, 17]])
13
Iterating an Array:
Iterating an array is done using for construct.
Eg:
>>> for i in a:
... print i
...
10
11
12
13
14
15
2D Array Iteration: 14
Eg:
>>> for i in A:
... print i
...
[10 11 12]
[13 14 15]
[16 17 18]

Making iteration element by element:


- Use .flat construct
>>> for i in [Link]:
... print i
...
10
11
12
13
14
15
16
17 15
To launch an aggregate function that returns a value calculated
for every single column or on every single row – use
apply_along_axis() function.
This function takes 3 arguments:
first argument – aggregate function
Second argument – axis value (0 – column & 1 – row)
Third argument – NumPy array
Eg:
>>> np.apply_along_axis([Link], axis=0, arr=A)
array([ 13., 14., 15.])
>>> np.apply_along_axis([Link], axis=1, arr=A)
array([ 11., 14., 17.])
16
User defined function can also be used:
Eg:
>>> def foo(x):
... return x/2
...
>>> np.apply_along_axis(foo, axis=1, arr=A)
array([[5, 5, 6],
[6, 7, 7],
[8, 8, 9]])
>>> np.apply_along_axis(foo, axis=0, arr=A)
array([[5, 5, 6],
[6, 7, 7],
[8, 8, 9]]) 17
18

You might also like