G10 Python 2
G10 Python 2
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
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!
46
• Click Create
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:
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)
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
• Let us assume the array is “ARR” and it has been initialized as:
• ARR = numpy.array([1,2,3,4,5])
56
57
58
Function
Code
60
61
62
63
64