Input, Iterators/Generators, Range,
Operator, Control Flow.
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.
The basic step of any programming language, How to take input from the user.
In python 3.X raw_input is depreciated.
Basically you can say any objects in python that has a attribute __next__, is an
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
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
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,
In python 2.x the output of range is an
Xrange is even better than range.
Almost the same as range but it behaves as
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
In python 3.x there is no xrange but range
works like xrange and does not load
values to memory.
Comparison (Relational) Operators
There are several operators in
Arithmetic Operators Comparison
Addition (+), used for sum.
Subtraction (-), user for difference.
Multiplication (*), used for product.
Division (/), used to divide.
Exponent (**), use to add power to any
Modulus (%), used to get remainder from
Floor division (//), gives the floor value of the 8
Equality (==), check for equality.
Inequality (<>, !=), check for not equal.
Greater than (>), Lesser than (<), for
Greater than or equal to (>=), Less than or
equal to (<=), for comparison.
Assignment Operator Logical Operators
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.
There are 3 logical operators mainly available
You can directly use the key words for using
Beware &, | and ~ has different usage in
python. These will not work as logical
Basically two membership
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
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 Left Shift (&), a << = 240 (means 1111 0000).
Binary Right Shift (&), a >> = 15 (means 0000 1111).
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
Basically it means the flow of your written program
that you can control with your conditions.
breaking and whitespaces
else with loops
Unlike conventional for loops, for loop in
python uses membership operator for
for i in range(5):
print i * i
Output will be
0 1 4 9 16
A while loop repeats a sequence of
statements until some condition becomes
x = 5
while x > 0:
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
The awesomeness of python is you can use
else not only with if but also with loops
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.
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
if x > 0:
if x < 0:
"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:
elif x == 0:
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
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
ThanksStay Tuned for the next part. Enjoy !!
Parece que tem um bloqueador de anúncios ativo. Ao listar o SlideShare no seu bloqueador de anúncios, está a apoiar a nossa comunidade de criadores de conteúdo.
Atualizámos a nossa política de privacidade.
Atualizámos a nossa política de privacidade de modo a estarmos em conformidade com os regulamentos de privacidade em constante mutação a nível mundial e para lhe fornecer uma visão sobre as formas limitadas de utilização dos seus dados.
Pode ler os detalhes abaixo. Ao aceitar, está a concordar com a política de privacidade atualizada.