Académique Documents
Professionnel Documents
Culture Documents
PYTHON
ORIGIN NETHERLANDS
YEAR 1989
CREATORS NAME GUIDO VAN
ROSSUM
PROGRAMMING PARADIGMS
class Hello:
cnt = 0
def display(self):
print hello world % Hello.cnt
4. Procedural: Tasks are treated as step-bystep iterations where common tasks are
placed in functions that are called as needed.
This coding style favors iteration, sequencing,
selection, and modularization.
PYTHON INTERPRETER-WITH
EXAMPLES
#!/usr/bin/python
counter = 100
# An integer assignment
miles = 1000.0
# A floating point
name
= "John"
# A string
#!/usr/bin/python
a=b=c=1
#Multiple Assignment
KEYWORDS
and
as
assert
break
class
continu def
e
del
elif
else
except
exec
finally
for
from
global
if
import
in
if
lambda not
or
pass
raise
try
while
with
return
yield
a+b
(-)
Subtraction
ab
(*)
Multiplicatio
n
a*b
(/) Division
b/a
(%) Modulus
b%a
(**)
Exponent
a**b
(//)Floor
division
a//b
Ex-
#!/usr/bin/python
a=10
b=2**2
c=3
print a*b + c a//c #prints the result 40
BOOLEAN OPERATIONS
These operators compare the values on either sides of them and decide the
relation among them. They are also called Relational operators.
Assume variable a holds 10 and variable b holds 20.
Operato
r
Description
Examp
le
==
(a ==
b) is
not
true.
!=
<>
(a <>
b) is
true.
This is
similar
to !=
operato
r.
>
(a > b)
is not
true.
<
(a < b)
(a != b)is
true
is true.
>=
(a >=
b) is
not
true.
<=
(a <=
b) is
true.
Ex-
#!/usr/bin/python
a=10
b=5
c=3
print a==b #prints False
print c<=b #prints True
print a>c #prints True
Result
if x is false, then y, else x
if x is false, then x, else y
if x is false, then True,
else False
Notes
(1)
(2)
(3)
#!/usr/bin/python
a=10
b=5
c=3
print a==b #prints False
print c<=b #prints True
print a>c #prints True
ASSIGNMENT OPERATIONS
Operator
Description
Example
c=a+b
assigns
value of a
+ b into c
+= Add AND
c += a is
equivalent
to c = c +
a
-= Subtract
AND
c -= a is
equivalent
to c = c a
*= Multiply
AND
c *= a is
equivalent
to c = c *
a
/= Divide AND
c /= a is
equivalent
to c = c /
ac /= a is
equivalent
to c = c /
a
%= Modulus
AND
c %= a is
equivalent
to c = c
%a
**= Exponent
AND
c **= a is
equivalent
to c = c
** a
//= Floor
Division
c //= a is
equivalent
to c = c //
a
Ex-
#!/usr/bin/python
a=10
b=5
c=1
b+=4
c*=a
print a #prints 10(a)
print b #prints 9(5 + 4)
print c #prints 10(1*10)