SlideShare une entreprise Scribd logo
1  sur  16
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

Contenu connexe

Tendances

Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov
 
Dotnet programming concepts difference faqs- 2
Dotnet programming concepts difference faqs- 2Dotnet programming concepts difference faqs- 2
Dotnet programming concepts difference faqs- 2
Umar Ali
 

Tendances (20)

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

En vedette

I huvudet på en cloudarkitekt | cloudcache.io
I huvudet på en cloudarkitekt | cloudcache.ioI huvudet på en cloudarkitekt | cloudcache.io
I huvudet på en cloudarkitekt | cloudcache.io
welovecloud
 
ASAS 2013 - Agility and the essence of software architecture
ASAS 2013 - Agility and the essence of software architectureASAS 2013 - Agility and the essence of software architecture
ASAS 2013 - Agility and the essence of software architecture
Avisi B.V.
 

En vedette (14)

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

Similaire à The Awesome Python Class Part-3

Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
rohithprabhas1
 

Similaire à The Awesome Python Class Part-3 (20)

Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Python Interview Questions For Experienced
Python Interview Questions For ExperiencedPython Interview Questions For Experienced
Python Interview Questions For Experienced
 
Unit - 2 CAP.pptx
Unit - 2 CAP.pptxUnit - 2 CAP.pptx
Unit - 2 CAP.pptx
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
 
Java unit 3
Java unit 3Java unit 3
Java unit 3
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
Python programming
Python  programmingPython  programming
Python programming
 
1_Python Basics.pdf
1_Python Basics.pdf1_Python Basics.pdf
1_Python Basics.pdf
 
UNIT II_python Programming_aditya College
UNIT II_python Programming_aditya CollegeUNIT II_python Programming_aditya College
UNIT II_python Programming_aditya College
 
Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning
 
Python
PythonPython
Python
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golang
 
Introduction to Python Basics
Introduction to Python BasicsIntroduction to Python Basics
Introduction to Python Basics
 
Python
PythonPython
Python
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
 
Python-review1.ppt
Python-review1.pptPython-review1.ppt
Python-review1.ppt
 
Python-review1.pdf
Python-review1.pdfPython-review1.pdf
Python-review1.pdf
 
Python-review1.ppt
Python-review1.pptPython-review1.ppt
Python-review1.ppt
 

Dernier

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 

Dernier (20)

Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 

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