Vous êtes sur la page 1sur 18

PROGRAMMING LANGUAGE

PYTHON
ORIGIN NETHERLANDS
YEAR 1989
CREATORS NAME GUIDO VAN
ROSSUM

Python was conceived in the late 1980s,


and its implementation began in
December 1989 By GUIDO VAN ROSSUM
at Centrum Wiskunde & Informatica(CWI)
in the Netherlands.
In December 1989,Rossum decided to
write an interpreter for the new scripting
language which was a descendent of ABC
that would appeal to UNIX/C hackers.

He chose the working title as Python


arbitrarily being a big fan of Monty
Pythons Flying Circus.
Python 2.0 was released on 16 October
2000 and had many major new features,
including a cycle-detecting garbage
collector and support for Unicode. With
this release the development process was
changed and became more transparent
and community-backed.
Python 3.0 (which early in its development
was commonly referred to as Python 3000
or py3k), a major, backwards-incompatible
release, was released on 3 December
2008 after a long period of testing. Many
of its major features have been
backported to the backwards-compatible
Python 2.6.x and 2.7.x version series.

PROGRAMMING PARADIGMS

Python supports multiple programming


paradigms, including object-oriented,
imperative and functional programming or
procedural styles.

Programming Paradigms that Python follows:


1. Object Oriented Programming: Python
follows OOP. It includes creation of an object
using class, defining class variable, function
overloading, instantiation of a class object.
A class can be defined as:

class Hello:
cnt = 0
def display(self):
print hello world % Hello.cnt

In the above example,


A class called Hello has been defined, and a
class variable called cnt has been created.
Even a function has been defined inside it.
However, Python doesn't use OOP
completely, as it cannot completely hide data.
But it is mainly used because it is reusable
and allows polymorphism.

2. Functional: Every statement is treated as a


mathematical equation and any form of state
or mutable data are avoided. The main
advantage of this approach is that there
arent any side effects to consider. In addition,
this coding style lends itself well to parallel

processing because there is no state to


consider. Many developers prefer this coding
style for recursion.
3. Imperative: Computation is performed as a
direct change to program state. This style is
especially useful when manipulating data
structures and produces elegant, but simple,
code. It changes state information as needed
in order to achieve a goal.
Example:
MyList = [1, 2, 3, 4, 5]
Sum = 0
for X in MyList:
Sum += X
print(Sum)

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

Python uses interpreter, simply named


python-interpreter. It is not a property of
the language but a property of the
implementation. Python is not compiled to
machine code ahead of time but "only"
compiled to bytecode.
Python script is first compiled to bytecode and
then it is implemented by an interpreter like
Cpython.
Cpython is both the compiler and vm.
After installing python on the machine, we
write a python code and save it with .py
extension, say first.py
Then simply open cmd and run >python
first.py
That is > python (path to the file we
created)
Now lets see how to write a basic Hello
World python program and execute *.py
program

For Example Lets see how to write and


execute a simple python code

In the shell type


>vim hellopython.py
Writing a hello world python program
>#!/usr/bin/python
#hello world program
print hello world
Execute Python program
>python helloworld.py

VARIABLES AND KEYWORDS


HOW TO CONSTRUCT A VARIABLE
Construction of a variable is very
simpe in Python.
We just have to write name of the
variable on the left hand side and give

a value to it with an = sign on the


right hand side.
Ex>name = Rajiv
Variable name must start with a letter
or an underscore_.
Ex- _name, units etc.
Keywords must not be used as
variables .
Python variables do not need explicit
declaration to reserve memory space.
The declaration happens automatically
when you assign a value to a variable
Example

#!/usr/bin/python
counter = 100

# An integer assignment

miles = 1000.0

# A floating point

name

= "John"

# A string

Python also allows to assign a single


value to several variables
simultaneously.

#!/usr/bin/python
a=b=c=1

#Multiple Assignment

KEYWORDS

Keywords are the reserved words in


Python.

We cant use a keyword as variable


name, function name or any other
identifier.

They are used to define the syntax


and structure of the Python language.

Keywords are also case sensitive.

The list of keywords are given below.

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

print

raise

try

while

with

return

yield

PYTHON ARITHMETIC OPERATIONS


(+) Addition

Adds values on either side of the operator.

a+b

(-)
Subtraction

Subtracts right hand operand from left hand operand.

ab

(*)
Multiplicatio
n

Multiplies values on either side of the operator

a*b

(/) Division

Divides left hand operand by right hand operand

b/a

(%) Modulus

Divides left hand operand by right hand operand and returns


remainder

b%a

(**)
Exponent

Performs exponential (power) calculation on operators

a**b

(//)Floor
division

Floor Division - The division of operands where the result is


the quotient in which the digits after the decimal point are
removed. But if one of the operands is negative, the result is
floored, i.e., rounded away from zero (towards negative
infinity):

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

==

If the values of two operands are equal, then the


condition becomes true.

(a ==
b) is
not
true.

!=

If values of two operands are not equal, then condition


becomes true.

<>

If values of two operands are not equal, then condition


becomes true.

(a <>
b) is
true.
This is
similar
to !=
operato
r.

>

If the value of left operand is greater than the value of


right operand, then condition becomes true.

(a > b)
is not
true.

<

If the value of left operand is less than the value of right

(a < b)

(a != b)is
true

operand, then condition becomes true.

is true.

>=

If the value of left operand is greater than or equal to the


value of right operand, then condition becomes true.

(a >=
b) is
not
true.

<=

If the value of left operand is less than or equal to the


value of right operand, then condition becomes 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

These are the Boolean operations, ordered by ascending priority:


Operation
x or y
x and y
not x
Notes:

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)

1. This is a short-circuit operator, so it only evaluates the second argument if


the first one is False.
2. This is a short-circuit operator, so it only evaluates the second argument if
the first one is True.
3. not has a lower priority than non-Boolean operators, so not a == b is
interpreted as not (a == b), and a == not b is a syntax error.

#!/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

Assigns values from right side operands to left side


operand

c=a+b
assigns
value of a
+ b into c

+= Add AND

It adds right operand to the left operand and assign


the result to left operand

c += a is
equivalent
to c = c +
a

-= Subtract
AND

It subtracts right operand from the left operand and


assign the result to left operand

c -= a is
equivalent
to c = c a

*= Multiply
AND

It multiplies right operand with the left operand and


assign the result to left operand

c *= a is
equivalent
to c = c *
a

/= Divide AND

It divides left operand with the right operand and


assign the result to left operand

c /= a is
equivalent
to c = c /
ac /= a is
equivalent
to c = c /
a

%= Modulus
AND

It takes modulus using two operands and assign the


result to left operand

c %= a is
equivalent
to c = c
%a

**= Exponent
AND

Performs exponential (power) calculation on


operators and assign value to the left operand

c **= a is
equivalent
to c = c
** a

//= Floor
Division

It performs floor division on operators and assign


value to the left operand

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)

Vous aimerez peut-être aussi