0% found this document useful (0 votes)
2 views

Intro to Python - Day 1

The document provides an introduction to Python, covering essential topics such as Jupyter Notebook, variables, data types, functions, and operators. It emphasizes the importance of code commenting, variable assignment, and type conversion, including implicit and explicit conversions. Additionally, it explains the use of the print() function and highlights the need for type casting when concatenating strings and numbers.

Uploaded by

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

Intro to Python - Day 1

The document provides an introduction to Python, covering essential topics such as Jupyter Notebook, variables, data types, functions, and operators. It emphasizes the importance of code commenting, variable assignment, and type conversion, including implicit and explicit conversions. Additionally, it explains the use of the print() function and highlights the need for type casting when concatenating strings and numbers.

Uploaded by

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

Introduction to Python

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Agenda
● Jupyter Notebook
● Variables
● Data Types

○ Fundamental
[email protected] Data Types
VGNQ9IQTEJ
● Functions in Python – print()
● Operators
● Python Flow Control
● Pseudocodes

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Jupyter Environment

● The Jupyter Notebook is an open-source


web-based development application that
allows you to create documents that can
[email protected] have code, equations, visualizations
VGNQ9IQTEJ and narrative text.

● Mainly used for data cleaning and Data


transformation, statistical modeling,
data visualization, machine learning,
deep learning and much more.

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Jupyter Environment

● You can use Jupyter for over 40


programming languages. Within the
data science community, mainly used
for Python, R, Julia and Scala.
[email protected]
VGNQ9IQTEJ

● You can also use Jupyter with big data


tools, like Apache Spark from Python
(known as PySpark), R and Scala.
You can explore big data with pandas,
scikit-learn, ggplot2, TensorFlow and
more.
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Jupyter Editor

Notebook name. Click


here to change the name
of the notebook

[email protected] Menu bar


VGNQ9IQTEJ
Toolbar

Code cell. Write your


code here

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Commenting code

[email protected]
VGNQ9IQTEJ

#
Code commenting is required in order to describe the purpose of the code. This is
not only a good practice but also a mandatory practice in most organizations.

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Variables

● Variables are containers that store data. Python has no command for
declaring variable

● You simply assign a value to a variable to create it


[email protected]
● You use = to assign a value to a variable
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Print variables with texts

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Reassign variable

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Multiple variables assignment with same value

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Multiple variables assignment with different values

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Python data types

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
We learn the following in our session today

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Create integer & string variables

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Create float & boolean variables

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Checking data types with type() function

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Data type conversion

Python allows to convert one data type to another

Implicit Conversion: Conversion Type Conversion


done by Python interpreter with
[email protected]
VGNQ9IQTEJ
programmer’s intervention

Implicit Conversion Explicit Conversion


Explicit Conversion: Conversion
that is user-defined that forces an
expression to be of specific data type
Arithmetic
Casting Operation
Operation

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Implicit conversion

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Explicit conversion is required when you have mixed data types.

Numeric Data type


[email protected] String Data type
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Explicit conversion

String Data type. Needs to be


converted to integer type before
adding to another numeric
[email protected] variable
VGNQ9IQTEJ

Explicit type conversion


of string to integer type
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Type casting

Type Casting

[email protected]
VGNQ9IQTEJ

int() float() str() bool()

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Type casting

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Functions in Python
[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
What is a function?

Name of the Input


● A function is a block of code that runs when called function, sum() parameters

● A function has a
[email protected] name. You call the function by its
VGNQ9IQTEJname

● A function can take input(s), known as input


parameters

● A function can return data as a result


Return value of
the function

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The print() Function
[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The print() in python

Words or sentences separated by a comma


within a print() function get concatenated
when printed.

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The print() in python

The sep is an optional parameter. When


output is printed, each word is separated
by ^^^ characters

[email protected]
VGNQ9IQTEJ

print(‘\n’) gives a new blank line

The backslash “\” is known as escape character. It is used in representing certain


whitespaces.

For example ‘\t’ is a tab


This fileand ‘\n’for
is meant ispersonal
a new use
line.
by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Invalid use of opening and closing quotes

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Print with concatenation

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
You cannot concatenate string & number

[email protected]
VGNQ9IQTEJ

That’s wrong because adding numbers to strings doesn’t make any sense. You
need to explicitly convert the number to string first, in order to join them together.

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Concatenating with type casting

[email protected]
VGNQ9IQTEJ

Explicit conversion of a number


to string with str() function.

We learn more such type


conversions in our upcoming
sessions

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Python Operators
[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Python operators

Python
Operators

[email protected]
VGNQ9IQTEJ
Arithmetic Relational Logical Special
Operators Operators Operators Operators

Bitwise Assignment
Operators Operators

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Addition

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Addition

[email protected]
VGNQ9IQTEJ

Note that 22 is a string here because it


has been put within the quotes.

Here two strings are getting concatenated.

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Subtraction

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Subtraction

[email protected]
VGNQ9IQTEJ

When two strings are added, the operator basically concatenates the two strings.
Subtracting two strings does not make any sense.
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Multiplication

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Multiplication

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Multiplication

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Division

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Division

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Get quotient

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Get remainder

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Arithmetic operators: Get square & power output

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Runtime variable

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Runtime variable

The default return type of a runtime variable is string.

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Runtime variable

The default return type of a runtime variable is string. So in order to use


the entered value for computation we convert the values to integer.

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Relational operators

Relational operators are used to compare 2 values and take certain


decisions based on the outcome (outcome is is a boolean value)

[email protected]
< is less than
VGNQ9IQTEJ
<= is less than & equal to
> is greater than
>= is greater than & equal to
== is equal to
!= is not equal to
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Relational operators

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Logical operators

Logical operators in Python are used for conditional statements are either
True or False

[email protected]
AND Returns True if both the operands are True
VGNQ9IQTEJ

OR Returns True if either of the operands are True

NOT Returns True if the operand is False

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
AND operator

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
OR operator

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
NOT operator

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Membership operators

Membership operators tests whether a value is a member of a sequence. The


sequence may be a list, a string, a tuple, or a dictionary.

[email protected]
VGNQ9IQTEJ
The ‘in’ operator is used to check if a value exists in any sequence object or not.
in For example, if 2 exists in a list, say [4, 5, 7, 2]. This evaluates to True if it finds a
value in the specified sequence object. Otherwise it returns a False.

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Membership operators

[email protected]
VGNQ9IQTEJnot in A ‘not in’ works in an opposite way to an ‘in’ operator. A ‘not in’ evaluates to True if
a value is not found in the specified sequence object. Else it returns a False.

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Membership operators

Example:

in operator: not in operator:

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Some bitwise operators

Python Bitwise Operators take one to two operands, and operates on it/them bit by
bit, instead of whole

[email protected]
VGNQ9IQTEJ & The binary and (&) takes two values and performs an AND-ing on each pair of
(Bitwise bits.
and)

Compared to &, this one returns 1 even if one of the two corresponding bits from
| the two operands is 1.

(Bitwise or)
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The truth tables

& |
(Bitwise and) (Bitwise or)

[email protected] Value Value Value | Value


Value Value Value
VGNQ9IQTEJ & Value

True True True True True True

True False False True False True

False True False False True True

False False False False False False


This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Some bitwise operators

Example:

& operator: | operator:

[email protected]
VGNQ9IQTEJ

(9 % 3 == 0) & (9 % 5 == 0) (9 % 3 == 0) | (9 % 5 == 0)

Implies True & False Implies True | False

This results in Fasle. This results in True.


This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
More assignment operators

The assignment operators are used to store data into a variable.

a += b is same as a = a + b
[email protected]
VGNQ9IQTEJ a *= b is same as a = a * b
a /= b is same as a = a / b
a %= b is same as a = a % b
a **= b is same as a = a ** b
a //= b is same as a = a // b
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Data slicing

The basic syntax for a slice is square brackets with colons and integers
inside "[0:1:2]".
myStr[start : stop : step]
[email protected]
myStr[ : stop] # By using one colon and leaving the first argument blank
VGNQ9IQTEJ

we automatically start at index 0, stepping by 1


myStr[start : ] # By using one colon and leaving the last argument blank
we automatically go to the end, stepping by 1
myStr[ : : step] # by using two colons and leaving the first two arguments
blank we start at index 0, go to the end and step by 1
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Data slicing

[email protected]
VGNQ9IQTEJ

‘:’ is used to specify range of the


sequence to be sliced

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Data slicing

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Data slicing

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
String Handling

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
String Handling

[email protected]
VGNQ9IQTEJ

Returns a concatenated string where


each character of s1 is separated with
sep string which is ‘-’

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
String Handling

[email protected]
VGNQ9IQTEJ

Returns a list containing


elements as strings separated
by the space character

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
String Handling

Returns a copy of the string with both


leading and trailing characters removed
(based on the string argument passed)

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Python Flow Control
[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Python Flow Control

Any program has a flow. The flow is the order in which the program’s code
executes. The control flow of a Python program is controlled by:
[email protected]
VGNQ9IQTEJ 1. Conditional Statements

2. Loops

3. Function Calls

We cover the basics of conditional statements and loops in today’s session

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The if-statement

Sometimes we want to execute a code only if a certain condition is true.

The if statement is used in Python for decision making. An "if statement" is


written by using the if keyword.
[email protected]
VGNQ9IQTEJ

Syntax:
if test expression:
statement(s)

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The if-statement

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The if-else statement

The ‘if..else’ statement evaluates a test expression and will execute the code
that is part of the ‘if’ expression if the test expression True.

If the test expression is False, the code that is part of the ‘else’ expression is
executed. Note the indentation that is used to separate the ‘if’ and ‘else’ blocks.
[email protected]
VGNQ9IQTEJ

Syntax:
if test expression:
Body of if
else:
Body of else
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The if-else statement

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The while Loop

Loops are used to repeat the execution of a specific block of code.

The ‘while loop’ in Python is used to iterate over a block of code as long as the
test expression holds true.
[email protected]
VGNQ9IQTEJ
We generally use this loop when the number of times to iterate is not known to
us beforehand.
Syntax:

while test_expression:
Body of while
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The while Loop

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The for Loop

The ‘for loop’ in Python is used to iterate over the items of a sequence object like
list, tuple, string and other iterable objects.

The iteration continues until we reach the last item in the sequence object. Note
the indentation that is used in a ‘for loop’ to separate the rest of the code from the
[email protected]
VGNQ9IQTEJ
‘for loop’ syntax

Syntax:
for i in sequence:
Body of for

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
The for Loop

[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Pseudocode
[email protected]
VGNQ9IQTEJ

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
What is Pseudocode?

[email protected]
Pseudocode is a step-by-step written outline of your code that you can
VGNQ9IQTEJ

gradually transcribe into programming language

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Example: Pseudocode

Find the sum of 5 numbers:

1. Set sum=0, count=0


[email protected]
VGNQ9IQTEJ
2. Enter the number
3. Add number to sum and store it back to
sum
4. Increment count by 1
5. If count < 5, go to step 2 else print total
This file is meant for personal use by [email protected] only.
SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
Write pseudocode to calculate sum & average of 10 numbers that you
[email protected]
VGNQ9IQTEJ

input

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
There exists a word “Pythonic”? What does it mean?

There are many ways to accomplish the same task in Python, but
there is usually one preferred way to do it. This preferred way is
called "pythonic."

[email protected]
VGNQ9IQTEJ

Read The Hitchhiker’s Guide to Python (https://docs.python-


guide.org/writing/style/).

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.
[email protected]
VGNQ9IQTEJ Thank You

This file is meant for personal use by [email protected] only.


SharingProprietary
or publishing the
content. © Great contents
Learning. in partUnauthorized
All Rights Reserved. or full is useliable forprohibited.
or distribution legal action.

You might also like