SlideShare a Scribd company logo
The Awesome
Input, Iterators/Generators, Range,
Operator, Control Flow.
Part-3
Summary of Previous Parts
We are done with discussing the greatness of python, Installation, Editor
selection, Environment setup and package installation.
We have also discussed Data types in python. Basic Data structures, their
syntaxes and their usage.
We will move forward with lot many things,
I hope you enjoy the show.
2
Input
The basic step of any programming language, How to take input from the user.
raw_input, input
3
In python 3.X raw_input is depreciated.
Iterators
Basically you can say any objects in python that has a attribute __next__, is an
iterator.
Eg, [0, 1, 2, 3, 4] is an iterator. It has 5 elements starting from 0 and has next.
You can loop over an iterator as many times as possible, as it saves the iterable values in
memory.
4
Generators
Generators are iterators which never stores all the elements in memory.
Generator Expressions are generator version of list comprehensions. They
look like list comprehensions, but returns a generator back instead of a list.
numbers = (x for x in range(5))
here numbers is a generator, having value <generator object <genexpr> at 0x401f08>
The values are never stored in memory, and they are revoked when needed.
The value can be accessed once only.
They generate the values on the fly. 5
Range vs Xrange
Range is a really cool thing in python.
It’s mainly used for mathematical functions
or iteration.
range is basically a list of numbers. from first
index to n-1 index.
If first index is not given then it takes it as 0.
eg, numbers = range(5), numbers is [0, 1, 2, 3,
4]
In python 2.x the output of range is an
iterator. 6
Xrange is even better than range.
Almost the same as range but it behaves as
a generator.
The output starts from the first index iterates
to n-1 and saves in an xrange object.
It does not load all data to memory so it’s
faster.
In python 3.x there is no xrange but range
works like xrange and does not load
values to memory.
Operators
Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
7
There are several operators in
python
Arithmetic Operators Comparison
Operator
Addition (+), used for sum.
Subtraction (-), user for difference.
Multiplication (*), used for product.
Division (/), used to divide.
Exponent (**), use to add power to any
number.
Modulus (%), used to get remainder from
division.
Floor division (//), gives the floor value of the 8
Equality (==), check for equality.
Inequality (<>, !=), check for not equal.
Greater than (>), Lesser than (<), for
comparison.
Greater than or equal to (>=), Less than or
equal to (<=), for comparison.
Assignment Operator Logical Operators
Assignment (=).
Add AND (+=), sums and assigns.
Subtract AND (-=), subtracts and assigns.
Multiply AND (*=), multiplies and assigns.
Divide AND (+=), divides and assigns.
Modulus AND (%=), assigns the reminder.
Exponent AND (**=), Assigns the final value
after the exponent multiplication.
9
There are 3 logical operators mainly available
in python.
and
or
not
You can directly use the key words for using
them.
Beware &, | and ~ has different usage in
python. These will not work as logical
operators.
Membership Operator
Basically two membership
operators.
in and not in
in checks whether the var is a
member of the sequence or not.
not in check whether the var is not
a member of the sequence or
not.
10
Bitwise Operator
Assume a = 60 and b = 13. so binary format will be
a = 0011 1100 and b = 0000 1101
Binary AND (&), a & b is 0000 1100.
Binary OR (&), a & b is 0011 1101.
Binary XOR (&), a & b is 0011 0001.
Binary 1’s Compliment (&), (~a ) = -61 (means 1100
0011 in 2's complement form due to a signed
binary number..
Binary Left Shift (&), a << = 240 (means 1111 0000).
Binary Right Shift (&), a >> = 15 (means 0000 1111).
Identity Operator
Basically two identity operators.
is and is not
is checks whether the two
variables are pointing to the
same object or not.
is not check whether the two
Control Flow
Basically it means the flow of your written program
that you can control with your conditions.
Loops
for loop
while loop
breaking and whitespaces
else with loops
Branches 11
Loops
FOR Loop
Unlike conventional for loops, for loop in
python uses membership operator for
iteration.
Syntax:
for i in range(5):
print i * i
Output will be
0 1 4 9 16
12
While Loop
A while loop repeats a sequence of
statements until some condition becomes
false.
Syntax:
x = 5
while x > 0:
print (x)
x = x - 1
Output will be
5 4 3 2 1
Breaking loops Else with loops
With loops in python two things comes in
mind. breaking and white spaces.
we can use keyword break inside any loop
and the loop instantly stop iterating when
break is executed.
Similarly we have continue keyword, when
encountered the interpreter simply skips
every next steps inside the loop and
comes to the next iteration.
Whitespace plays a vital role in python.
every block that is indented inside the loop is
13
The awesomeness of python is you can use
else not only with if but also with loops
and exceptions.
Every loop in this world is driven by some
condition. If that condition is met for the
iteration then it goes inside of the loop
else it goes inside of else.
That makes sense right ?
You can simply reduce a bunch of code
simply using else with loops.
You can use it with both for and while.
Branches
There is basically only one kind of branch in
Python, the 'if' statement. The simplest
form of the if statement simple executes a
block of code only if a given predicate is
true, and skips over it if the predicate is
false.
if x > 0:
print "Positive"
if x < 0:
print "Negative"
14
"elif" branches onto the if statement.
Note, it will stop checking branches as soon
as it finds a true predicate, and skip the
rest of the if statement.
x = -6
if x > 0:
print "Positive"
elif x == 0:
print "Zero"
else:
print "Negative"
Order of operations
When more than one operator appears in an
expression, the order of evaluation
depends on the rules of precedence.
For mathematical operators, Python follows
mathematical convention.
The acronym PEMDAS is a useful way to
remember the rules:
1. P : Parenthesis
2. E : Exponential
3. M : Multiplication
4. D : Division
5. A : Addition
6. S : Subtraction
15
ThanksStay Tuned for the next part. Enjoy !!
16

More Related Content

PPTX
The Awesome Python Class Part-5
PPTX
The Awesome Python Class Part-4
PPTX
The Awesome Python Class Part-2
PPTX
This presentation is a great introduction to both fundamental programming con...
PPTX
Python Session - 3
PPTX
Python Session - 6
PDF
Python Interview Questions And Answers
PPTX
Introduction to Python programming Language
The Awesome Python Class Part-5
The Awesome Python Class Part-4
The Awesome Python Class Part-2
This presentation is a great introduction to both fundamental programming con...
Python Session - 3
Python Session - 6
Python Interview Questions And Answers
Introduction to Python programming Language

What's hot (20)

PDF
Anton Kasyanov, Introduction to Python, Lecture2
PPTX
Python Session - 5
PPTX
Python second ppt
PPTX
Python Session - 4
PPTX
Introduction To Programming with Python-3
PDF
Python interview questions and answers
PPTX
Programming in C sesion 2
DOCX
Notes on c++
PPTX
Introduction To Programming with Python-1
PPTX
Python advance
PDF
Python Advanced – Building on the foundation
PDF
Programming in C Session 1
DOCX
Python Interview Questions For Freshers
PPTX
11 Unit 1 Chapter 02 Python Fundamentals
PDF
Data types in python
PDF
Dotnet programming concepts difference faqs- 2
PPTX
Advance python
PPTX
Python-04| Fundamental data types vs immutability
PDF
Python-01| Fundamentals
PPTX
Introduction To Programming with Python Lecture 2
Anton Kasyanov, Introduction to Python, Lecture2
Python Session - 5
Python second ppt
Python Session - 4
Introduction To Programming with Python-3
Python interview questions and answers
Programming in C sesion 2
Notes on c++
Introduction To Programming with Python-1
Python advance
Python Advanced – Building on the foundation
Programming in C Session 1
Python Interview Questions For Freshers
11 Unit 1 Chapter 02 Python Fundamentals
Data types in python
Dotnet programming concepts difference faqs- 2
Advance python
Python-04| Fundamental data types vs immutability
Python-01| Fundamentals
Introduction To Programming with Python Lecture 2
Ad

Viewers also liked (13)

PDF
8 grejer som storföretag kan lära av startups
PDF
I huvudet på en cloudarkitekt | cloudcache.io
PPTX
Grunt och gulp
PDF
Learn python 1
PDF
ASAS 2013 - Agility and the essence of software architecture
PDF
Python 3.5: An agile, general-purpose development language.
PPT
Introduction to lambda expression & lambda calculus
PPTX
Getting started with ES6 : Future of javascript
PDF
The Zen of Python
PPTX
Support Vector Machine without tears
PDF
Linear regression without tears
PDF
Learn 90% of Python in 90 Minutes
PPT
Introduction to Python
8 grejer som storföretag kan lära av startups
I huvudet på en cloudarkitekt | cloudcache.io
Grunt och gulp
Learn python 1
ASAS 2013 - Agility and the essence of software architecture
Python 3.5: An agile, general-purpose development language.
Introduction to lambda expression & lambda calculus
Getting started with ES6 : Future of javascript
The Zen of Python
Support Vector Machine without tears
Linear regression without tears
Learn 90% of Python in 90 Minutes
Introduction to Python
Ad

Similar to The Awesome Python Class Part-3 (20)

PPTX
Welcome to python workshop
DOCX
Python interview questions and answers
PPTX
Chapter 2-Python and control flow statement.pptx
DOCX
Python Interview Questions For Experienced
PPTX
Unit - 2 CAP.pptx
PPTX
Python For Data Science.pptx
PDF
Java unit 3
PPTX
Lecture-02-JAVA, data type, token, variables.pptx
PDF
Introduction to Python
PPTX
Python.pptx
PPTX
Python programming
PDF
1_Python Basics.pdf
PPTX
Understanding All Types of Operators in Python with Examples"
PPT
UNIT II_python Programming_aditya College
PPTX
Introduction to Python for Data Science and Machine Learning
PPTX
PPTX
Introduction to golang
PDF
Python Programming Course Presentations
PPTX
Introduction to Python Basics
PPTX
Python
Welcome to python workshop
Python interview questions and answers
Chapter 2-Python and control flow statement.pptx
Python Interview Questions For Experienced
Unit - 2 CAP.pptx
Python For Data Science.pptx
Java unit 3
Lecture-02-JAVA, data type, token, variables.pptx
Introduction to Python
Python.pptx
Python programming
1_Python Basics.pdf
Understanding All Types of Operators in Python with Examples"
UNIT II_python Programming_aditya College
Introduction to Python for Data Science and Machine Learning
Introduction to golang
Python Programming Course Presentations
Introduction to Python Basics
Python

Recently uploaded (20)

PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
PPT on Performance Review to get promotions
PPT
Mechanical Engineering MATERIALS Selection
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPT
Project quality management in manufacturing
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Embodied AI: Ushering in the Next Era of Intelligent Systems
Lesson 3_Tessellation.pptx finite Mathematics
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
bas. eng. economics group 4 presentation 1.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPT on Performance Review to get promotions
Mechanical Engineering MATERIALS Selection
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Operating System & Kernel Study Guide-1 - converted.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Project quality management in manufacturing
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Foundation to blockchain - A guide to Blockchain Tech
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

The Awesome Python Class Part-3

  • 1. The Awesome Input, Iterators/Generators, Range, Operator, Control Flow. Part-3
  • 2. Summary of Previous Parts We are done with discussing the greatness of python, Installation, Editor selection, Environment setup and package installation. We have also discussed Data types in python. Basic Data structures, their syntaxes and their usage. We will move forward with lot many things, I hope you enjoy the show. 2
  • 3. Input The basic step of any programming language, How to take input from the user. raw_input, input 3 In python 3.X raw_input is depreciated.
  • 4. Iterators Basically you can say any objects in python that has a attribute __next__, is an iterator. Eg, [0, 1, 2, 3, 4] is an iterator. It has 5 elements starting from 0 and has next. You can loop over an iterator as many times as possible, as it saves the iterable values in memory. 4
  • 5. Generators Generators are iterators which never stores all the elements in memory. Generator Expressions are generator version of list comprehensions. They look like list comprehensions, but returns a generator back instead of a list. numbers = (x for x in range(5)) here numbers is a generator, having value <generator object <genexpr> at 0x401f08> The values are never stored in memory, and they are revoked when needed. The value can be accessed once only. They generate the values on the fly. 5
  • 6. Range vs Xrange Range is a really cool thing in python. It’s mainly used for mathematical functions or iteration. range is basically a list of numbers. from first index to n-1 index. If first index is not given then it takes it as 0. eg, numbers = range(5), numbers is [0, 1, 2, 3, 4] In python 2.x the output of range is an iterator. 6 Xrange is even better than range. Almost the same as range but it behaves as a generator. The output starts from the first index iterates to n-1 and saves in an xrange object. It does not load all data to memory so it’s faster. In python 3.x there is no xrange but range works like xrange and does not load values to memory.
  • 7. Operators Arithmetic Operators Comparison (Relational) Operators Assignment Operators Logical Operators Bitwise Operators Membership Operators Identity Operators 7 There are several operators in python
  • 8. Arithmetic Operators Comparison Operator Addition (+), used for sum. Subtraction (-), user for difference. Multiplication (*), used for product. Division (/), used to divide. Exponent (**), use to add power to any number. Modulus (%), used to get remainder from division. Floor division (//), gives the floor value of the 8 Equality (==), check for equality. Inequality (<>, !=), check for not equal. Greater than (>), Lesser than (<), for comparison. Greater than or equal to (>=), Less than or equal to (<=), for comparison.
  • 9. Assignment Operator Logical Operators Assignment (=). Add AND (+=), sums and assigns. Subtract AND (-=), subtracts and assigns. Multiply AND (*=), multiplies and assigns. Divide AND (+=), divides and assigns. Modulus AND (%=), assigns the reminder. Exponent AND (**=), Assigns the final value after the exponent multiplication. 9 There are 3 logical operators mainly available in python. and or not You can directly use the key words for using them. Beware &, | and ~ has different usage in python. These will not work as logical operators.
  • 10. Membership Operator Basically two membership operators. in and not in in checks whether the var is a member of the sequence or not. not in check whether the var is not a member of the sequence or not. 10 Bitwise Operator Assume a = 60 and b = 13. so binary format will be a = 0011 1100 and b = 0000 1101 Binary AND (&), a & b is 0000 1100. Binary OR (&), a & b is 0011 1101. Binary XOR (&), a & b is 0011 0001. Binary 1’s Compliment (&), (~a ) = -61 (means 1100 0011 in 2's complement form due to a signed binary number.. Binary Left Shift (&), a << = 240 (means 1111 0000). Binary Right Shift (&), a >> = 15 (means 0000 1111). Identity Operator Basically two identity operators. is and is not is checks whether the two variables are pointing to the same object or not. is not check whether the two
  • 11. Control Flow Basically it means the flow of your written program that you can control with your conditions. Loops for loop while loop breaking and whitespaces else with loops Branches 11
  • 12. Loops FOR Loop Unlike conventional for loops, for loop in python uses membership operator for iteration. Syntax: for i in range(5): print i * i Output will be 0 1 4 9 16 12 While Loop A while loop repeats a sequence of statements until some condition becomes false. Syntax: x = 5 while x > 0: print (x) x = x - 1 Output will be 5 4 3 2 1
  • 13. Breaking loops Else with loops With loops in python two things comes in mind. breaking and white spaces. we can use keyword break inside any loop and the loop instantly stop iterating when break is executed. Similarly we have continue keyword, when encountered the interpreter simply skips every next steps inside the loop and comes to the next iteration. Whitespace plays a vital role in python. every block that is indented inside the loop is 13 The awesomeness of python is you can use else not only with if but also with loops and exceptions. Every loop in this world is driven by some condition. If that condition is met for the iteration then it goes inside of the loop else it goes inside of else. That makes sense right ? You can simply reduce a bunch of code simply using else with loops. You can use it with both for and while.
  • 14. Branches There is basically only one kind of branch in Python, the 'if' statement. The simplest form of the if statement simple executes a block of code only if a given predicate is true, and skips over it if the predicate is false. if x > 0: print "Positive" if x < 0: print "Negative" 14 "elif" branches onto the if statement. Note, it will stop checking branches as soon as it finds a true predicate, and skip the rest of the if statement. x = -6 if x > 0: print "Positive" elif x == 0: print "Zero" else: print "Negative"
  • 15. Order of operations When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. For mathematical operators, Python follows mathematical convention. The acronym PEMDAS is a useful way to remember the rules: 1. P : Parenthesis 2. E : Exponential 3. M : Multiplication 4. D : Division 5. A : Addition 6. S : Subtraction 15
  • 16. ThanksStay Tuned for the next part. Enjoy !! 16