You are on page 1of 64

Advance Python

Grade 10

1
2
3
4
5
6
Python inputs
• In Python, not only can we display the output to the
user, but we can also collect data from the user and can
pass it on to the Python script for further processing.
• To collect the data from the user at the time of
execution, input() function is used.
• While using the input function, the datatype of the
expected input is required to be mentioned so that the
machine does not interpret the received data in an
incorrect manner as the data taken as input from the
user is considered to be a string (sequence of
characters) by default.

7
• For example:
• Str = input(<String>) # Python expects the input to
be of string datatype

• Number = int(input(<string>)) # Input string gets


converted to an integer value before assignment

• Value = float(input(<String>)) # Input string gets


converted to a decimal value before assignment
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Conditional Statements

• While coding in Python, a lot of times we need to take


decisions.
• For example, if a person needs to create a calculator with the
help of a Python code, he/she needs to take in 2 numbers
from the user and then ask the user about which function
he/she wishes to operate.
• Now, according to the user’s choice, the selection of function
would change.
• In this case, we need the machine to understand what should
happen when.
• This is where conditional statements help.
• Conditional statements help the machine in taking a decision
according to the condition which gets fulfilled.
27
different types of conditional statements in Python

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
NumPy, stands for Numerical Python
• NumPy also works with arrays, which is nothing but homogenous collection
of Data.
• An array is nothing but a set of multiple values which are of same datatype.
• They can be numbers, characters, Booleans, etc. but only one datatype can
be accessed through an array.
• In NumPy, the arrays used are known as ND-arrays (N-Dimensional Arrays)
as NumPy comes with a feature of creating n-dimensional arrays in python.
• An array can easily be compared to a list. Let us take a look how different
they are:

43
NumPy Arrays Lists
1. Homogenous collection of Data. 1. Heterogenous collection of Data.
2. Can contain only one type of data, 2. Can contain multiple types of data,
hence not flexible with datatypes. hence flexible with datatypes.
3. Cannot be directly initialized. Can 3. Can be directly initialized as it is the
be operated with Numpy package part of python syntax.
only. 4. Direct numerical operations are not
4. Direct numerical operations can be possible. For example, dividing the
done. For example, dividing the whole whole list by 3 cannot divide every
array by 3 divides every element by 3. element by 3.
5. Widely used for arithmetic operations. 5. Widely used for data management.
6. Arrays take less memory space. 6. Lists acquire more memory space.
7. Example: To create a Numpy array ‘A’: 7. Example: To create a list:
import numpy
A=numpy.array([1,2,3,4,5,6,7,8,9,0]) A = [1,2,3,4,5,6,7,8,9,0]

44
Exploring NumPy!

• NumPy package provides us with various features and


functions which helps us in arithmetic and logical
operations. Let us look at some of them:

• 1. NumPy Arrays: As discussed before, arrays are
homogenous collection of datatypes. With NumPy, we
can create n-dimensional arrays (where n can be any
integer) and operate on them using other mathematical
functions.
• Here are some ways by which you can create arrays
using NumPy package assuming the NumPy packages is
imported already. 45
Creating environment
• Open Anaconda Navigator
• Select environments

46
• Click Create

• Then enter a name for the environment

47
• Select the new environment
• Go to Environments tab just below the Home tab and from
there we can check what all packages are installed and what is
not.

• It is very easy
to install any package through anaconda navigator, simply
search the required package, select package and click on apply
to install it.

48
PACKAGES INSTALLED

49
• Using an environment
• In the environments list, click the environment name.
• Click the arrow button next to the name. The activation options
dialog appears.
• Select one of the following options for opening the
environment: terminal, Python interpreter, IPython Console, or
Jupyter Notebook.

50
Working with a package
To use a package, we need to import it in the script wherever
it is required. There are various versions of importing
a package in Python:

Meaning: Import numpy in the file to use its


functionalities in the file to which it has been
imported.

Meaning: Import numpy and refer to it as np


wherever it is used.

51
Function Code
Creating a Numpy Array numpy.array([1,2,3,4,5])
Creating a 2-Dimensional zero array numpy.zeros((4,3))
(4X3 –
4 rows and 3 columns)

Creating an array with 5 random numpy.random.random(5)


values
Creating a 2-Dimensional constant
value array (3X4 – 3 rows and 4
columns) having all 6s numpy.full((3,4),6)

Creating a sequential array from 0 to


30 with gaps of 5 numpy.arrange(0,30,5)

52
Let’s try
# Basic Stastistics with Numpy Arrays
Import numpy as np
a = np.array( [10, 20, 30, 40, 50] ) # Array 1
b = np.array( [1, 2, 3, 4, 5] ) # Array 2

print("Addition: ", a + b) # Printing addition result

print("Subtraction: ", a - b) # Printing subtraction result

print("Multiplication: ", a * b) # Printing multiplication result

print("Division: ", a / b) # Printing division result

print("Remainder: ", a % b) # Printing remainder result


53
54
• One of the features of array is that we can perform arithmetic
functions on the elements of the array directly by performing
them on the whole array.

• Let us assume the array is “ARR” and it has been initialized as:
• ARR = numpy.array([1,2,3,4,5])

• Now, let us look at various operations that could be implemented


on this array:
55
Function Code
Adding 5 to each element ARR + 5
Divide each element by 5 ARR / 5
Squaring each element ARR ** 2
Accessing 2nd element of
the array (element ARR[1]
count starts from 0)
Multiplying 2 arrays
{consider BRR = ARR * BRR
numpy.array([6,7,8,9,0]) }

56
57
58
Function
Code

Type of an array type(ARR)

Check the dimensions of ARR.ndim


an array
Shape of an array ARR.shape

Size of an array ARR.size

Datatype of elements ARR.dtype


stored in the array
59
Code
Function

Finding out maximum element of an ARR.max()


array

Finding out row-wise maximum ARR.max(axis = 1)


elements

Finding out column-wise minimum ARR.min(axis = 0)


elements

Sum of all array elements ARR.sum()

60
61
62
63
64

You might also like