Post on 14-Apr-2017
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
InputThe basic step of any programming language, How to take input from the user.raw_input, input
3In 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.
It’s the same as iterator except [] is (). But you can not process them twice.
All generators are iterators but all iterators are not generators.
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.
You can also apply step to range,like range(0, 4, 2) is [0, 2]
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.
Xrange also takes step.
Operators
Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators7
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 division result.
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.
Floor Division AND (//=), Assigns the floor value of the division.
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 OperatorAssume a = 60 and b = 13. so binary format
will bea = 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 variables are not pointing to the same object or not.
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
if
elif
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 = 5while 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 a part of the loop. if it’s just a white space it is considered outside of the loop.
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 = -6if 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