1 - Basics
1 - Basics
Python is a popular programming language. It was created by Guido van Rossum, and released in
1991.
It is used for:
Python History
o Python laid its foundation in the late 1980s.
o The implementation of Python was started in the December 1989 by Guido Van
Rossum at CWI in Netherland.
o In February 1991, van Rossum published the code (labeled version 0.9.0) to alt.sources.
o In 1994, Python 1.0 was released with new features like: lambda, map, filter, and
reduce.
o Python 2.0 added new features like: list comprehensions, garbage collection system.
o On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was designed to
rectify fundamental flaw of the language.
o ABC programming language is said to be the predecessor of Python language which was
capable of Exception Handling and interfacing with Amoeba Operating System.
o Python is influenced by following programming languages:
o ABC language.
o Modula-3
Python Version
Python programming language is being updated regularly with new features and supports.
There are lots of updations in python versions, started from 1994 to current release.
1) Web Applications
We can use Python to develop web applications. It provides libraries to handle internet
protocols such as HTML and XML, JSON, Email processing, request, beautifulSoup, Feedparser
etc. It also provides Frameworks such as Django, Pyramid, Flask etc to design and delelop web
based applications. Some important developments are: PythonWikiEngines, Pocoo,
PythonBlogSoftware etc.
3) Software Development
Python is helpful for software development process. It works as a support language and can be
used for build control and management, testing etc.
Python is popular and widely used in scientific and numeric computing. Some useful library and
package are SciPy, Pandas, IPython etc. SciPy is group of packages of engineering, science and
mathematics.
5) Business Applications
Python is used to build Business applications like ERP and e-commerce systems. Tryton is a high
level application platform.
We can use Python to develop console based applications. For example: IPython.
Python is awesome to perform multiple tasks and can be used to develop multimedia
applications. Some of real applications are: TimPlayer, cplay etc.
8) 3D CAD Applications
To create CAD application Fandango is a real application which provides full features of CAD.
9) Enterprise Applications
Python can be used to create applications which can be used within an Enterprise or an
Organization. Some real time applications are: OpenErp, Tryton, Picalo etc.
Using Python several application can be developed for image. Applications developed are:
VPython, Gogh, imgSeek etc.
There are several such applications which can be developed using Python
Python Features
2) Expressive Language
Python language is more expressive means that it is more understandable and readable.
3) Interpreted Language
Python is an interpreted language i.e. interpreter executes the code line by line at a time. This
makes debugging easy and thus suitable for beginners.
4) Cross-platform Language
Python can run equally on different platforms such as Windows, Linux, Unix and Macintosh etc.
So, we can say that Python is a portable language.
Python language is freely available at official web address. The source-code is also available.
Therefore it is open source.
6) Object-Oriented Language
Python supports object oriented language and concepts of classes and objects come into
existence.
7) Extensible
It implies that other languages such as C/C++ can be used to compile the code and thus it can
be used further in our python code.
10) Integrated
Python Variables
Variable is a name which is used to refer memory location. Variable also known as
identifier and used to hold value.
In Python, we don't need to specify the type of variable because Python is a type infer
language and smart enough to get variable type.
Variable names can be a group of both letters and digits, but they have to begin with a
letter or an underscore.
It is recommended to use lowercase letters for variable name. Rahul and rahul both
are two different variables.
Multiple Assignment
Example:
x=y=z=50
print(x)
print (y)
print (z)
Output:
50
50
50
Example:
a,b,c=5,10,15
print( a )
print (b)
print (c)
Output:
5
10
15
Comments
Python supports two types of comments:
In case user wants to specify a single line comment, then comment must start with #
Example:
''' This
Is
Multiline comment'''
Example:
Python Keywords
Python Keywords are special reserved words which convey a special meaning to the
compiler/interpreter. Each keyword have a special meaning and a specific operation. These
keywords can't be used as variable.
Python Operators
Operators are particular symbols that are used to perform operations on operands. It returns
result that can be used in application.
Example:
4+5=9
Here 4 and 5 are Operands and (+) , (=) signs are the operators. This expression produces the
output 9.
Types of Operators
Python supports the following operators
1. Arithmetic Operators.
2. Relational Operators.
3. Logical Operators.
4. Assignment Operators.
5. Membership Operators.
6. Identity Operators.
1. Arithmetic Operators
The following table contains the arithmetic operators that are used to perform arithmetic
operations.
Operators Description
+ To perform addition
- To perform subtraction
* To perform multiplication
/ To perform division
// Perform Floor division(gives integer value after
division)
% To return remainder after division(Modulus)
** Perform exponent(raise to power)
Example:
a=10
b=3
c=a+b
d=a-b
e=a*b
f=a/b
g=a//b
h=a%b
i=a**b
print(c, d, e, f, g, h, i)
output:
13 7 30 3.3333333333333335 3 1 1000
2. Relational Operators
The following table contains the relational operators that are used to check relations.
Operators Description
Example:
print(10<20) # True
print( 10>20 ) # False
print(10<=10) # True
print( 20>=15) # True
print(5==6) #False
print(5!=6) # True
3. Logical Operators
The following table contains the arithmetic operators that are used to perform arithmetic
operations.
Operators Description
Example
Output:
True
True
False
4. Assignment Operators
The following table contains the assignment operators that are used to assign values to the
variables.
Operators Description
= Assignment a=5
/= Divide and Assign a=a/5 or a/=5
+= Add and assign a=a+5 or a+=5
-= Subtract and Assign a=a-5 or a-=5
*= Multiply and assign a=a*5 or a*=5
%= Modulus and assign a=a%5 or a%=5
**= Exponent and assign a=a**5 or a**=5
//= Floor division and assign a=a//5 or a//=5
Example:
c=10
print( c)
c+=5
print(c)
c-=5
print(c)
c*=2
print( c )
c/=2
print( c )
c%=3
print(c)
c=5
c**=2
print( c )
c//=2
print(c )
5. Membership Operators
Operators Description
Example:
a=10
b=20
list=[10,20,30,40,50]
if (a in list):
print ("a is in given list" )
else:
print ("a is not in given list" )
Output:
a is in given list
b is given in list
6. Identity Operators
Operators Description
Example:
a=20
b=20
if( a is b):
print ('a,b have same identity' )
else:
print ( 'a, b are different' )
b=10
if( a is not b):
print ('a,b have different identity' )
else:
print ('a,b have same identity')
Output
a,b have same identity
a,b have different identity
CONTROL STATEMENTS
Python If Statements
The Python if statement is a statement which is used to test specified condition. We can use if
statement to perform conditional operations in our Python application.
o if statement
o if-else statement
o if – elif statement
if(condition):
statements
Example
a=5
if(a<10):
print('a is less than 10')
The If statement is used to test specified condition and if the condition is true, if block
executes, otherwise else block executes.
if(condition):
true statements
else:
False statements
Example
a=5
if(a<10):
print('a is less than 10 ')
else:
print('a is not less than 10 ')
Python if elif statement
In python, we can use if elif to check multiple conditions. Python provides elif keyword to
make nested If statement.
If statement:
Body
elif statement:
Body
else:
Body
Example
a=5
if(a<10):
print('a is less than 10 ')
elif(a>10):
print('a is greater than 10 ')
else:
print('a is equal to 10')
# if and
m=30
p=40
c=50
if(m>=35 and p>=35 and c>=35):
print('pass')
else:
print('fail')
output:
fail
# if or
a=10
b=30
if(a<20 or b<20):
print("true")
else:
print('false')
output:
true
LOOPS
For Loop
Python for loop is used to iterate the elements of a collection in the order that they appear.
This collection can be a sequence(list or string).
for i in range(10):
print(i)
output:
0123456789
// step value
for i in range(1,20,2):
print(i, end=' ')
output:
1 3 5 7 9 11 13 15 17 19
for i in range(10,0,-1):
print(i, end=' ')
output:
10 9 8 7 6 5 4 3 2 1
When an outer loop contains an inner loop in its body it is called Nested Looping.
for <expression>:
for <expression>:
Body
Example
for i in range(1,6):
for j in range(1,6):
print(j,end=' ')
print()
Output:
12345
12345
12345
12345
12345
for i in range(1,6):
for j in range(5,0,-1):
print(j,end=' ')
print()
Output:
54321
54321
54321
54321
54321
In Python, while loop is used to execute number of statements or body till the specified
condition is true. Once the condition is false, the control will come out of the loop.
Python While Loop Syntax
while <expression>:
Body
Here, loop Body will execute till the expression passed is true. The Body may be a single
statement or multiple statement.
i=1
while(i<=10):
print(i,end=' ')
i=i+1
Output:
1 2 3 4 5 6 7 8 9 10
i=10
while(i>=1):
print(i,end=' ')
i=i-1
output:
10 9 8 7 6 5 4 3 2 1
Python Break
Break statement is a jump statement which is used to transfer execution control. It breaks
the current execution and in case of inner loop, inner loop terminates immediately.
Example
for i in range(10):
if (i==4):
print ("Element found")
break
print (i)
Output:
1 2 3 Element found
We can use continue statement with for as well as while loop in Python.
Example
for i in range(11):
if i==5 :
continue
print(i)
Output:
1
3
5
End of Loop
Python Pass
In Python, pass keyword is used to execute nothing; it means, when we don't want to
execute code, the pass can be used to execute empty. It is same as the name refers to. It just
makes the control to pass by without executing any code. If we want to bypass any code pass
statement can be used.
pass
for i in [1,2,3,4,5]:
if i==3:
pass
print (i)
Output:
1
2
3
4
5