SlideShare a Scribd company logo
1 of 99
Python
Programming
Introduction to Python
02-10-2022 Dr. Poonam Panwar, Associate Professor, CUIET 1
Agenda
In this session, you will learn about:
• Introduction to Python
• Variables
• Functions
• Python Operators
• Python Flow Controls
• Conditional Statements
• Loops
Introduction to Python
Dr. Poonam Panwar, Associate
Professor, CUIET
3
Some Common Languages in use
Dr. Poonam Panwar, Associate
Professor, CUIET
4
Languages History
Dr. Poonam Panwar, Associate
Professor, CUIET
5
History of Python
• First public release in 1991
• Open source from beginning
• Managed by Python Software
Foundation
Invented in December 1989
Guido Van Rossum
Dr. Poonam Panwar, Associate
Professor, CUIET
6
Python
Python drew inspiration from other programming languages:
Python is powerful... and fast;
plays well with others;
runs everywhere;
is friendly & easy to learn;
is Open.
Python is an interpreted language, do not need to be compiled to run.
Python is a high-level language, which
means a programmer can focus on
what to do instead of how to do it.
Writing programs in Python takes less
time than in another language.
Dr. Poonam Panwar, Associate
Professor, CUIET
7
Python Used For
Dr. Poonam Panwar, Associate
Professor, CUIET
8
Compiling and Interpreting
Many languages are required to compile the program into a form (ByteCode) that
the machine understands.
Python is instead directly interpreted into machine instructions.
compile execute
output
source code
Hello.java
byte code
Hello.class
interpret
output
source code
Hello.py
Dr. Poonam Panwar, Associate
Professor, CUIET
9
Development Environment
Terminal /
Shell based
Three most common options:
iPython notebook
IDLE (Spyder
IDE)
Dr. Poonam Panwar, Associate
Professor, CUIET
10
Anaconda
Dr. Poonam Panwar, Associate
Professor, CUIET
11
Anaconda Navigator
Dr. Poonam Panwar, Associate
Professor, CUIET
12
Jupyter IDE
Click here to change the name of
the notebook
Write your code
here
Menu bar
Running Python
Kernel
Execute the
code using Run
button or
shift+enter
Jupyter IDE
Dr. Poonam Panwar, Associate
Professor, CUIET
14
Spyder IDE
Dr. Poonam Panwar, Associate
Professor, CUIET
15
A Python Code Sample
Assignment uses = and comparison uses ==
For numbers +-*/% are as expected
Logical operators are words (and, or, not)
The basic printing command is print
Start comments with #: the rest of line is ignored
Dr. Poonam Panwar, Associate
Professor, CUIET
16
Variables
Dr. Poonam Panwar, Associate
Professor, CUIET
17
Variables
Variables are reserved memory that store data
Unlike languages like C,
C++ and Java, Python
has no command for
declaring variable
= operator is used to
assign a value to a
variable
We would only receive the second assigned value as the
output since that was the most recent assignment
Reassigning Variables
Reassigning Variables
You can connect a different value with a previously
assigned variable very easily through simple reassignment
Multiple Variable Assignment
With same value
Multiple Variable Assignment
With different values
Data Types
String Numeric Boolean None Collection
Integer Float
List Tuple Dictionary Set
Data Types in Python
Data Types
String Numeric Boolean None Collection
Integer Float
List Tuple Dictionary Set
We will learn the following data types today
String variables are variables that hold zero or more characters such as letters,
numbers, spaces, commas and many more
Creating String Variables
Use type(variable_name) to check the data type of variable declared
Creating Numeric Variables
As seen earlier, there are two types of
numeric data types:
Integer Float
A numeric variable is one that may take on any value within a finite or infinite interval
Creating Boolean Variables
Boolean variables are variables that
can have only two possible values:
True False
Data Type Conversion
Type Conversion
Implicit
Conversion
Explicit
Conversion
Python supports conversion of one data type to another
Data Type Conversion
Type Conversion
Implicit
Conversion
Explicit
Conversion
Arithmetic
Operation
Casting
Operation
Implicit Conversion: Conversion done by Python interpreter without
programmer’s intervention
Explicit Conversion: Conversion that is user-defined that forces an expression
to be of specific data type
Implicit Conversion
In Implicit Conversion, the conversion is done by Python interpreter without
programmer’s intervention
Explicit Conversion
In Explicit Conversion, conversion that is user-defined forces an expression to be
of specific data type
Example
Explicit Conversion
Explicit data type
conversion using float()
function
Type Casting
Type Casting
float() bool() str()
int()
Type Casting
Functions
Dr. Poonam Panwar, Associate
Professor, CUIET
35
What are Functions?
A function is a block of reusable code that runs when called
Function name: sum()
Input
parameters
Return value
of the function
• A function has a name. You call the function by its name
• A function can take input(s), known as input parameters
• A function can return data as a result
What are Functions?
There are three types of functions:
Built-in
Functions
User-defined
Functions
Lambda
Functions
Some of the built-in functions are:
print() - to print the output
input() - to take input from user
the type casting functions like int(), float(), str(), bool(), etc.
We will study the user-defined functions and lambda
functions in detail in our upcoming sessions
The print() function prints the specified message to the screen
The message can be a string, or any other object
The object will be converted into a string before written to the screen
The print() in Python
strings separated by a comma within a
print() function get concatenated.
strings separated by a
comma within a print()
function get concatenated.
The print() in Python
The sep is an optional
parameter. When output is
printed, each word is
separated by comma and
space character
print(‘n’) gives a new
blank line
The backslash “” is a special character that represents whitespaces. For
example ‘t’ is a tab and ‘n’ is a new line.
Invalid use of opening and closing quotes is not allowed.
Concatenation using print()
Use + operator to
concatenate two
strings
Adding numbers to strings does not make any sense. Please consider explicit
conversion to convert the number to string first, in order to join them together.
You cannot concatenate string & number.
Concatenation with Type Casting
Explicit conversion of a number to
string with str() function.
We learn more such type conversions in our upcoming sessions
Python Operators
Dr. Poonam Panwar, Associate
Professor, CUIET
44
Python Operators
The Python Operators are:
Arithmetic
Operators
Relational Operators Logical Operators
Membership
Operators
Bitwise Operators
Assignment
Operators
The Python Operators are:
Python Operators
The most common Arithmetic Operators are:
Addition Subtraction Multiplication
Division Modulus Floor Division
exponentiation
+ - *
/ % //
**
Arithmetic Operators
Addition
Arithmetic Operators
Addition
Note that 22 is a string
Here two strings are getting
concatenated
Arithmetic Operators
Subtraction
Arithmetic Operators
Subtraction
When two strings are added, the + (addition) operator basically
concatenates the two strings. Subtracting two strings does not make
any sense.
Arithmetic Operators
Multiplication
Arithmetic Operators
Multiplication
Arithmetic Operators
Multiplication
Arithmetic Operators
Division
Arithmetic Operators
Division
Arithmetic Operators
Modulus
Arithmetic Operators
Floor Division
Arithmetic Operators
Exponentiation
Runtime Variable Assignment
Runtime Variable Assignment
Runtime Variable Assignment
Relational operators are used to compare values and take certain decisions based
on the outcome
Relational Operators
Following are some of the relational operators:
Operators Meaning
< Is less than
<= Is less than & equal to
> Is greater than
>= Is greater than & equal to
== Is equal to
!= Is not equal to
Relational Operators
Logical operators in Python allow a program to make a decision based
on multiple conditions
Each operand is considered a condition that can be evaluated to a true
or false value
Logical Operators
Returns True if
both the
operands are
true
AND
Returns True if
either of the
operands are
True
OR
Returns true if
the operand is
false
NOT
Logical Operators
AND
If both the operands are true then it returns true
Logical Operators
OR
If one of the operands are true then it returns true
Logical Operators
NOT
Reverses the result
Membership Operators
Membership operators checks whether a value is a member of a sequence.
The sequence may be a list, a string, a tuple, or a dictionary
in
• The ‘in’ operator is
used to check if a
value exists in any
sequence object or
not
not 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.
Membership Operators
in
Membership Operators
not in
Python Bitwise Operators take one to two operands, and operates on it/them
bit by bit, instead of whole
Some Bitwise Operators
& (Bitwise and)
• The binary and (&) takes two values and performs
an AND-ing on each pair of bits.
| (Bitwise or)
• Compared to &, this one returns 1 even if one of the
two corresponding bits from the two operands is 1.
Truth Tables
| (Bitwise or)
& (Bitwise and)
02-10-2022 Dr. Poonam Panwar, Associate Professor, CUIET 72
Some Bitwise Operators
| operator
& operator
Example
(10 % 2 == 0) & (10 % 3 == 0) (10 % 2 == 0) | (10 % 3 == 0)
Implies True & False. This results in False. Implies True | False. This results in True.
02-10-2022 Dr. Poonam Panwar, Associate Professor, CUIET 73
The assignment operators are used to store data into a variable
More Assignment Operators
Python Flow Controls
Dr. Poonam Panwar, Associate
Professor, CUIET
75
Python Flow Control
Any program has a flow and the flow is the order in which the program’s
code executes
The control flow of a Python program is controlled by:
Conditional
Statements
Loops
Function
Calls
We cover the basics of conditional statements and
loops in today’s session
02-10-2022 Dr. Poonam Panwar, Associate Professor, CUIET 76
Conditional Statements
Dr. Poonam Panwar, Associate
Professor, CUIET
77
● The if statement is used in Python for decision making
● It is written by using the if keyword
● The if keyword is followed by condition later followed by
indented block of code which will be executed only if the
condition is true
Syntax:
if
(condition):
statement(s)
The if-statement
Dr. Poonam Panwar, Associate
Professor, CUIET
78
The if-statement
Example
Dr. Poonam Panwar, Associate
Professor, CUIET
79
● The ‘if..else’ statement evaluates a test expression and will
execute:
o the indented if block of code if the condition is True
o the indented else block of code if the condition in the if
statement is False
The if-else statement
Syntax:
if (condition):
Body of if
else:
Body of else
Dr. Poonam Panwar, Associate
Professor, CUIET
80
The if-else statement
Example
Dr. Poonam Panwar, Associate
Professor, CUIET
81
Elif statement is used to control multiple conditions only if the given if
condition is false
The if elif else statement
Syntax:
if (condition1):
Body of if
elif (condition2):
Body of elif
elif (condition3):
Body of elif
.
.
.
else:
Body of
else
Dr. Poonam Panwar, Associate
Professor, CUIET
82
The if elif else statement
Example
Dr. Poonam Panwar, Associate
Professor, CUIET
83
● Nesting means using an if statement within another if
statement
● If the first ‘if’ condition is satisfied then the program executes
the commands within that ‘if’
Nested if-else statement
Syntax:
if (condition):
statement(s)
if (condition):
statement(s)
else:
statement(s)
else:
statement(s)
Dr. Poonam Panwar, Associate
Professor, CUIET
84
Example
Nested if-else statement
Dr. Poonam Panwar, Associate
Professor, CUIET
85
Example
Nested if statement
Dr. Poonam Panwar, Associate
Professor, CUIET
86
Loops
Dr. Poonam Panwar, Associate
Professor, CUIET
87
● Loops are used to execute of a specific block of code repetitively
● The ‘while loop’ in Python is used to iterate over a block of code as long as
the test expression holds true
● This loop is used when the number of times to iterate is not known to us
beforehand
While loop
Syntax:
while (condition):
Body of while
Dr. Poonam Panwar, Associate
Professor, CUIET
88
Example
While loop
Dr. Poonam Panwar, Associate
Professor, CUIET
89
● 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
The for Loop
Syntax:
for i in sequence:
Body of for
Dr. Poonam Panwar, Associate
Professor, CUIET
90
Example
The for Loop
Iteration is a general term for taking each item from a sequence one after
another
Dr. Poonam Panwar, Associate
Professor, CUIET
91
Loops in Python allows us to repeat tasks
Getting out of a Loop
Exit a loop completely when a certain condition is triggered
But at times, you may want to:
Skip part of a loop and start next execution
Dr. Poonam Panwar, Associate
Professor, CUIET
92
• The ‘break’ statement ends the loop
and resumes execution at the next
statement
• The break statement can be used in
both ‘while’ loop and ‘for’ loop
• It is always used with conditional
statements
Getting out with Break Statement
Dr. Poonam Panwar, Associate
Professor, CUIET
93
Getting out with Break Statement
Example
Dr. Poonam Panwar, Associate
Professor, CUIET
94
• The ‘continue’ statement in Python
ignores all the remaining
statements in the iteration of the
current loop and moves the control
back to the beginning of the loop
• The continue statement can be used
in both ‘while’ loop and the ‘for’
loop
• Like break, continue is also always
used with conditional statements
Skip Loop Step with Continue Statement
Dr. Poonam Panwar, Associate
Professor, CUIET
95
Skip Loop Step with Continue Statement
Example
Dr. Poonam Panwar, Associate
Professor, CUIET
96
Nested for Loop
for [first iterating variable] in [outer loop]:
[code line 1]
[code line 2]
for [second iterating variable] in [nested loop]:
[code line 1]
1st level loop or outer
loop
Nested loop
1st indentation
2nd indentation
Dr. Poonam Panwar, Associate
Professor, CUIET
97
Nested for Loop
Outer loop
Inner loop
Dr. Poonam Panwar, Associate
Professor, CUIET
98
Thank you
Dr. Poonam Panwar, Associate
Professor, CUIET
99

More Related Content

Similar to 1. PGA2.0-Python Programming-Intro to Python.pptx

Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
IruolagbePius
 

Similar to 1. PGA2.0-Python Programming-Intro to Python.pptx (20)

Unit-I-PPT-1.ppt
Unit-I-PPT-1.pptUnit-I-PPT-1.ppt
Unit-I-PPT-1.ppt
 
UNIT II_python Programming_aditya College
UNIT II_python Programming_aditya CollegeUNIT II_python Programming_aditya College
UNIT II_python Programming_aditya College
 
Python PPT.pptx
Python PPT.pptxPython PPT.pptx
Python PPT.pptx
 
Basic concept of Python.pptx includes design tool, identifier, variables.
Basic concept of Python.pptx includes design tool, identifier, variables.Basic concept of Python.pptx includes design tool, identifier, variables.
Basic concept of Python.pptx includes design tool, identifier, variables.
 
Python 01.pptx
Python 01.pptxPython 01.pptx
Python 01.pptx
 
Python 1&2.pptx
Python 1&2.pptxPython 1&2.pptx
Python 1&2.pptx
 
Python 1&2.pptx
Python 1&2.pptxPython 1&2.pptx
Python 1&2.pptx
 
Python - Module 1.ppt
Python - Module 1.pptPython - Module 1.ppt
Python - Module 1.ppt
 
Python intro
Python introPython intro
Python intro
 
Python Basics by Akanksha Bali
Python Basics by Akanksha BaliPython Basics by Akanksha Bali
Python Basics by Akanksha Bali
 
17575602.ppt
17575602.ppt17575602.ppt
17575602.ppt
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
prakash ppt (2).pdf
prakash ppt (2).pdfprakash ppt (2).pdf
prakash ppt (2).pdf
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Review Python
Review PythonReview Python
Review Python
 
Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
 
Introduction of Python
Introduction of PythonIntroduction of Python
Introduction of Python
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
 

Recently uploaded

Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
pritamlangde
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 

Recently uploaded (20)

Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 

1. PGA2.0-Python Programming-Intro to Python.pptx

  • 1. Python Programming Introduction to Python 02-10-2022 Dr. Poonam Panwar, Associate Professor, CUIET 1
  • 2. Agenda In this session, you will learn about: • Introduction to Python • Variables • Functions • Python Operators • Python Flow Controls • Conditional Statements • Loops
  • 3. Introduction to Python Dr. Poonam Panwar, Associate Professor, CUIET 3
  • 4. Some Common Languages in use Dr. Poonam Panwar, Associate Professor, CUIET 4
  • 5. Languages History Dr. Poonam Panwar, Associate Professor, CUIET 5
  • 6. History of Python • First public release in 1991 • Open source from beginning • Managed by Python Software Foundation Invented in December 1989 Guido Van Rossum Dr. Poonam Panwar, Associate Professor, CUIET 6
  • 7. Python Python drew inspiration from other programming languages: Python is powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open. Python is an interpreted language, do not need to be compiled to run. Python is a high-level language, which means a programmer can focus on what to do instead of how to do it. Writing programs in Python takes less time than in another language. Dr. Poonam Panwar, Associate Professor, CUIET 7
  • 8. Python Used For Dr. Poonam Panwar, Associate Professor, CUIET 8
  • 9. Compiling and Interpreting Many languages are required to compile the program into a form (ByteCode) that the machine understands. Python is instead directly interpreted into machine instructions. compile execute output source code Hello.java byte code Hello.class interpret output source code Hello.py Dr. Poonam Panwar, Associate Professor, CUIET 9
  • 10. Development Environment Terminal / Shell based Three most common options: iPython notebook IDLE (Spyder IDE) Dr. Poonam Panwar, Associate Professor, CUIET 10
  • 11. Anaconda Dr. Poonam Panwar, Associate Professor, CUIET 11
  • 12. Anaconda Navigator Dr. Poonam Panwar, Associate Professor, CUIET 12
  • 13. Jupyter IDE Click here to change the name of the notebook Write your code here Menu bar Running Python Kernel Execute the code using Run button or shift+enter
  • 14. Jupyter IDE Dr. Poonam Panwar, Associate Professor, CUIET 14
  • 15. Spyder IDE Dr. Poonam Panwar, Associate Professor, CUIET 15
  • 16. A Python Code Sample Assignment uses = and comparison uses == For numbers +-*/% are as expected Logical operators are words (and, or, not) The basic printing command is print Start comments with #: the rest of line is ignored Dr. Poonam Panwar, Associate Professor, CUIET 16
  • 17. Variables Dr. Poonam Panwar, Associate Professor, CUIET 17
  • 18. Variables Variables are reserved memory that store data Unlike languages like C, C++ and Java, Python has no command for declaring variable = operator is used to assign a value to a variable
  • 19. We would only receive the second assigned value as the output since that was the most recent assignment Reassigning Variables
  • 20. Reassigning Variables You can connect a different value with a previously assigned variable very easily through simple reassignment
  • 23. Data Types String Numeric Boolean None Collection Integer Float List Tuple Dictionary Set Data Types in Python
  • 24. Data Types String Numeric Boolean None Collection Integer Float List Tuple Dictionary Set We will learn the following data types today
  • 25. String variables are variables that hold zero or more characters such as letters, numbers, spaces, commas and many more Creating String Variables Use type(variable_name) to check the data type of variable declared
  • 26. Creating Numeric Variables As seen earlier, there are two types of numeric data types: Integer Float A numeric variable is one that may take on any value within a finite or infinite interval
  • 27. Creating Boolean Variables Boolean variables are variables that can have only two possible values: True False
  • 28. Data Type Conversion Type Conversion Implicit Conversion Explicit Conversion Python supports conversion of one data type to another
  • 29. Data Type Conversion Type Conversion Implicit Conversion Explicit Conversion Arithmetic Operation Casting Operation Implicit Conversion: Conversion done by Python interpreter without programmer’s intervention Explicit Conversion: Conversion that is user-defined that forces an expression to be of specific data type
  • 30. Implicit Conversion In Implicit Conversion, the conversion is done by Python interpreter without programmer’s intervention
  • 31. Explicit Conversion In Explicit Conversion, conversion that is user-defined forces an expression to be of specific data type Example
  • 32. Explicit Conversion Explicit data type conversion using float() function
  • 33. Type Casting Type Casting float() bool() str() int()
  • 35. Functions Dr. Poonam Panwar, Associate Professor, CUIET 35
  • 36. What are Functions? A function is a block of reusable code that runs when called Function name: sum() Input parameters Return value of the function • A function has a name. You call the function by its name • A function can take input(s), known as input parameters • A function can return data as a result
  • 37. What are Functions? There are three types of functions: Built-in Functions User-defined Functions Lambda Functions Some of the built-in functions are: print() - to print the output input() - to take input from user the type casting functions like int(), float(), str(), bool(), etc. We will study the user-defined functions and lambda functions in detail in our upcoming sessions
  • 38. The print() function prints the specified message to the screen The message can be a string, or any other object The object will be converted into a string before written to the screen The print() in Python strings separated by a comma within a print() function get concatenated. strings separated by a comma within a print() function get concatenated.
  • 39. The print() in Python The sep is an optional parameter. When output is printed, each word is separated by comma and space character print(‘n’) gives a new blank line The backslash “” is a special character that represents whitespaces. For example ‘t’ is a tab and ‘n’ is a new line.
  • 40. Invalid use of opening and closing quotes is not allowed.
  • 41. Concatenation using print() Use + operator to concatenate two strings
  • 42. Adding numbers to strings does not make any sense. Please consider explicit conversion to convert the number to string first, in order to join them together. You cannot concatenate string & number.
  • 43. Concatenation with Type Casting Explicit conversion of a number to string with str() function. We learn more such type conversions in our upcoming sessions
  • 44. Python Operators Dr. Poonam Panwar, Associate Professor, CUIET 44
  • 45. Python Operators The Python Operators are: Arithmetic Operators Relational Operators Logical Operators Membership Operators Bitwise Operators Assignment Operators The Python Operators are:
  • 46. Python Operators The most common Arithmetic Operators are: Addition Subtraction Multiplication Division Modulus Floor Division exponentiation + - * / % // **
  • 48. Arithmetic Operators Addition Note that 22 is a string Here two strings are getting concatenated
  • 50. Arithmetic Operators Subtraction When two strings are added, the + (addition) operator basically concatenates the two strings. Subtracting two strings does not make any sense.
  • 62. Relational operators are used to compare values and take certain decisions based on the outcome Relational Operators Following are some of the relational operators: Operators Meaning < Is less than <= Is less than & equal to > Is greater than >= Is greater than & equal to == Is equal to != Is not equal to
  • 64. Logical operators in Python allow a program to make a decision based on multiple conditions Each operand is considered a condition that can be evaluated to a true or false value Logical Operators Returns True if both the operands are true AND Returns True if either of the operands are True OR Returns true if the operand is false NOT
  • 65. Logical Operators AND If both the operands are true then it returns true
  • 66. Logical Operators OR If one of the operands are true then it returns true
  • 68. Membership Operators Membership operators checks whether a value is a member of a sequence. The sequence may be a list, a string, a tuple, or a dictionary in • The ‘in’ operator is used to check if a value exists in any sequence object or not not 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.
  • 71. Python Bitwise Operators take one to two operands, and operates on it/them bit by bit, instead of whole Some Bitwise Operators & (Bitwise and) • The binary and (&) takes two values and performs an AND-ing on each pair of bits. | (Bitwise or) • Compared to &, this one returns 1 even if one of the two corresponding bits from the two operands is 1.
  • 72. Truth Tables | (Bitwise or) & (Bitwise and) 02-10-2022 Dr. Poonam Panwar, Associate Professor, CUIET 72
  • 73. Some Bitwise Operators | operator & operator Example (10 % 2 == 0) & (10 % 3 == 0) (10 % 2 == 0) | (10 % 3 == 0) Implies True & False. This results in False. Implies True | False. This results in True. 02-10-2022 Dr. Poonam Panwar, Associate Professor, CUIET 73
  • 74. The assignment operators are used to store data into a variable More Assignment Operators
  • 75. Python Flow Controls Dr. Poonam Panwar, Associate Professor, CUIET 75
  • 76. Python Flow Control Any program has a flow and the flow is the order in which the program’s code executes The control flow of a Python program is controlled by: Conditional Statements Loops Function Calls We cover the basics of conditional statements and loops in today’s session 02-10-2022 Dr. Poonam Panwar, Associate Professor, CUIET 76
  • 77. Conditional Statements Dr. Poonam Panwar, Associate Professor, CUIET 77
  • 78. ● The if statement is used in Python for decision making ● It is written by using the if keyword ● The if keyword is followed by condition later followed by indented block of code which will be executed only if the condition is true Syntax: if (condition): statement(s) The if-statement Dr. Poonam Panwar, Associate Professor, CUIET 78
  • 79. The if-statement Example Dr. Poonam Panwar, Associate Professor, CUIET 79
  • 80. ● The ‘if..else’ statement evaluates a test expression and will execute: o the indented if block of code if the condition is True o the indented else block of code if the condition in the if statement is False The if-else statement Syntax: if (condition): Body of if else: Body of else Dr. Poonam Panwar, Associate Professor, CUIET 80
  • 81. The if-else statement Example Dr. Poonam Panwar, Associate Professor, CUIET 81
  • 82. Elif statement is used to control multiple conditions only if the given if condition is false The if elif else statement Syntax: if (condition1): Body of if elif (condition2): Body of elif elif (condition3): Body of elif . . . else: Body of else Dr. Poonam Panwar, Associate Professor, CUIET 82
  • 83. The if elif else statement Example Dr. Poonam Panwar, Associate Professor, CUIET 83
  • 84. ● Nesting means using an if statement within another if statement ● If the first ‘if’ condition is satisfied then the program executes the commands within that ‘if’ Nested if-else statement Syntax: if (condition): statement(s) if (condition): statement(s) else: statement(s) else: statement(s) Dr. Poonam Panwar, Associate Professor, CUIET 84
  • 85. Example Nested if-else statement Dr. Poonam Panwar, Associate Professor, CUIET 85
  • 86. Example Nested if statement Dr. Poonam Panwar, Associate Professor, CUIET 86
  • 87. Loops Dr. Poonam Panwar, Associate Professor, CUIET 87
  • 88. ● Loops are used to execute of a specific block of code repetitively ● The ‘while loop’ in Python is used to iterate over a block of code as long as the test expression holds true ● This loop is used when the number of times to iterate is not known to us beforehand While loop Syntax: while (condition): Body of while Dr. Poonam Panwar, Associate Professor, CUIET 88
  • 89. Example While loop Dr. Poonam Panwar, Associate Professor, CUIET 89
  • 90. ● 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 The for Loop Syntax: for i in sequence: Body of for Dr. Poonam Panwar, Associate Professor, CUIET 90
  • 91. Example The for Loop Iteration is a general term for taking each item from a sequence one after another Dr. Poonam Panwar, Associate Professor, CUIET 91
  • 92. Loops in Python allows us to repeat tasks Getting out of a Loop Exit a loop completely when a certain condition is triggered But at times, you may want to: Skip part of a loop and start next execution Dr. Poonam Panwar, Associate Professor, CUIET 92
  • 93. • The ‘break’ statement ends the loop and resumes execution at the next statement • The break statement can be used in both ‘while’ loop and ‘for’ loop • It is always used with conditional statements Getting out with Break Statement Dr. Poonam Panwar, Associate Professor, CUIET 93
  • 94. Getting out with Break Statement Example Dr. Poonam Panwar, Associate Professor, CUIET 94
  • 95. • The ‘continue’ statement in Python ignores all the remaining statements in the iteration of the current loop and moves the control back to the beginning of the loop • The continue statement can be used in both ‘while’ loop and the ‘for’ loop • Like break, continue is also always used with conditional statements Skip Loop Step with Continue Statement Dr. Poonam Panwar, Associate Professor, CUIET 95
  • 96. Skip Loop Step with Continue Statement Example Dr. Poonam Panwar, Associate Professor, CUIET 96
  • 97. Nested for Loop for [first iterating variable] in [outer loop]: [code line 1] [code line 2] for [second iterating variable] in [nested loop]: [code line 1] 1st level loop or outer loop Nested loop 1st indentation 2nd indentation Dr. Poonam Panwar, Associate Professor, CUIET 97
  • 98. Nested for Loop Outer loop Inner loop Dr. Poonam Panwar, Associate Professor, CUIET 98
  • 99. Thank you Dr. Poonam Panwar, Associate Professor, CUIET 99