[go: up one dir, main page]

0% found this document useful (0 votes)
19 views40 pages

Unit 2 Python B.SC IT

Uploaded by

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

Unit 2 Python B.SC IT

Uploaded by

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

Prepared by

Senthil Kumar S
Assistant Professor,
Department of Information Technology,
Sri Ramakrishna Mission Vidyalaya
College of Arts and Science
(Autonomous), Coimbatore
NumPy: Introduction to NumPy – The Basics
of NumPy arrays–Computation on NumPy
Arrays–Aggregations: Min. Max, and
Everything in Between – Computation on
Arrays.
Various Computational Operations using
Array Representations.*
 NumPy (short for Numerical Python)
 NumPy is a Python library used for working

with arrays.
 NumPy is a Python library and is written

partially in Python
 Once NumPy is installed, import it in your
applications by adding the import keyword:
 import numpy

 Now NumPy is imported and ready to use.

import numpy
arr = numpy.array([1, 2, 3, 4, 5])
print(arr)
 NumPy package can be referred to as np instead
of numpy.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
 NumPy is used to work with arrays. The array object in
NumPy is called ndarray
 type( ) : This built-in Python function tells us

the type of the object passed to it.


import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
Output:
[1 2 3 4 5]
<class 'numpy.ndarray'>
 Array indexing is the same as accessing an
array element.
 You can access an array element by

referring to its index number.


 List is an ordered  Items in the lists can
sequence of items. be of different data
Values in the list are types.
called elements /  Eg: a=[10, 20, 30,
items. 40]; b=[10, 20, “abc”,
 It can be written as a 4.5];
list of comma-
separated items
(values) between
square brackets[ ].
 Lists are used to store multiple items in a
single variable.
 Lists are one of 4 built-in data types in

Python used to store collections of data, the


other 3 are Tuple, Set, and Dictionary.
Example: Lists uses Square Bracket
thislist = ["apple", "banana", "cherry"]
print(thislist)
 List items are ordered(order will not
change), changeable(we can change, add,
and remove items in a list), and allow
duplicate values.
 List items are indexed, the first item has

index [0], the second item has index [1] etc.


Operations on list:
 1. Indexing

 2. Slicing

 3. Concatenation

 4. Repetitions

 5. Updating

 6. Membership

 7. Comparison
Python provides
methods that operate
on lists.
Syntax:
list
name.methodname(element/index/li
st)
 Tuples are used to store multiple items in a
single variable.
 A tuple is a collection which is ordered and

unchangeable.
 Tuples are written with round brackets.
 A tuple is same as list,
except that the set of
elements is enclosed in
parentheses instead of
square brackets.
 A tuple is an immutable list.
i.e. once a tuple has been
created, you can't add
elements to a tuple or remove
elements from the tuple.
 But tuple can be converted
into list and list can be
converted in to tuple.
thistuple = ("apple", "banana", "cherry")
print(thistuple)

 Tuple items are ordered, unchangeable, and


allow duplicate values.
 Tuple items are indexed, the first item has

index [0], the second item has index [1] etc.


Operations on
 Tuples are faster than Tuples:
lists.
 If the user wants to 1. Indexing
protect the data from 2. Slicing
accidental changes, 3. Concatenation
tuple can be used. 4. Repetitions
5. Membership
6. Comparison
 Dictionaries are used to store data values in key:value pairs.
 A dictionary is a collection which is ordered*, changeable and

does not allow duplicates.


 Dictionaries are written with curly brackets, and have keys and

values
 Example:

thisdict = {
"brand": "Ford",
"year": 2020
}
print(thisdict)
 Dictionary items are ordered, changeable, and does not allow

duplicates.
 Dictionary items are presented in key:value pairs, and can be

referred to by using the key name.


 Dictionary is an unordered Operations on dictionary:
collection of elements. An
element in dictionary has a key:
1. Accessing an element
value pair. 2. Update
 All elements in dictionary are 3. Add element
placed inside the curly braces
i.e. { }
4. Membership
 Elements in Dictionaries are
accessed via keys and not by
their position.
 The values of a dictionary can
be any data type.
 Keys must be immutable data
type (numbers, strings, tuple)
import numpy as np
x = np.arange(4)
print("x =", x)
print("x + 5 =", x + 5)
print("x - 5 =", x - 5)
print("x * 2 =", x * 2)
print("x / 2 =", x / 2)
print("x // 2 =", x // 2)
Output:
x = [0 1 2 3]
x + 5 = [5 6 7 8]
x - 5 = [-5 -4 -3 -2]
x * 2 = [0 2 4 6]
x / 2 = [0. 0.5 1. 1.5]
x // 2 = [0 0 1 1]
import numpy as np
x = np.array([-2, -1, 0, 1, 2])
print (abs(x))

Output:
[2 1 0 1 2]
#returns the sum of all elements in the array:
import numpy as np
x = np.arange(1, 6)
print(x)
print(np.add.reduce(x))

Output:
[1 2 3 4 5]
15
#product of all array elements
import numpy as np
x = np.arange(1, 6)
print(x)
print(np.multiply.reduce(x))

Output:
[1 2 3 4 5]
120
import numpy as np
x = np.arange(1, 6)
print(x)
print(np.add.accumulate(x))
Output:
[1 2 3 4 5]
[ 1 3 6 10 15]
Hint:
1+2 3+3 4+6 5+10
import numpy as np
x = np.arange(1, 6)
print(x)
print(np.multiply.accumulate(x))
Output:
[1 2 3 4 5]
[ 1 2 6 24 120]
Hint:
(1*2) (2*3) (4*6) 5*24
import numpy as np
x = np.arange(1, 6)
print(x)
print(np.multiply.outer(x, x))
Output:
[1 2 3 4 5]
[[ 1 2 3 4 5]
[ 2 4 6 8 10]
[ 3 6 9 12 15]
[ 4 8 12 16 20]
[ 5 10 15 20 25]]
#Python code to demonstrate the working of max()
# printing the maximum of 4,12,43.3,19,100
print("Maximum of 4,12,43.3,19 and 100 is : ",end="")
print (max( 4,12,43.3,19,100 ) )

Output :

Maximum of 4,12,43.3,19 and 100 is : 100


#Python code to demonstrate the working of min()
# printing the min of 4,12,43.3,19,100
print("Minimum of 4,12,43.3,19 and 100 is : ",end="")
print (min( 4,12,43.3,19,100 ) )

Output:
Minimum of 4,12,43.3,19 and 100 is : 4
# maximum element of array
import numpy as np
arr = np.array([[1, 5, 6],
[4, 7, 2],
[3, 1, 9]])
print ("Largest element is:", arr.max())
print ("Row-wise maximum elements:", arr.max(axis=1))

Output:
Largest element is: 9
Row-wise maximum elements: [6 7 9]
# minimum element of array
import numpy as np
arr = np.array([[1, 5, 6],
[4, 7, 2],
[3, 1, 9]])
print ("Column-wise minimum elements:",arr.min())
print ("Row-wise minimum elements:",arr.min(axis = 1))

Output:
Column-wise minimum elements: 1
Row-wise minimum elements: [1 2 1]
# sum of array elements
import numpy as np
arr = np.array([[1, 5, 6],
[4, 7, 2],
[3, 1, 9]])
print ("Sum of all array elements:",arr.sum())

Output:
Sum of all array elements: 38
# cumulative sum along each row
import numpy as np
arr = np.array([[1, 5, 6],
[4, 7, 2],
[3, 1, 9]])
print ("Cumulative sum along each row:\n",arr.cumsum(axis = 1))

Output:
Cumulative sum along each row:
[[ 1 6 12]
[ 4 11 13]
[ 3 4 13]]

You might also like