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
[Link].
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.
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.
A list of python versions with its released date is given below.
Python Version Released Date
Python 1.0 January 1994
Python 1.5 December 31, 1997
Python 1.6 September 5, 2000
Python 2.0 October 16, 2000
Python 2.1 April 17, 2001
Python 2.2 December 21, 2001
Python 2.3 July 29, 2003
Python 2.4 November 30, 2004
Python 2.5 September 19, 2006
Python 2.6 October 1, 2008
Python 2.7 July 3, 2010
Python 3.0 December 3, 2008
Python 3.1 June 27, 2009
Python 3.2 February 20, 2011
Python 3.3 September 29, 2012
Python 3.4 March 16, 2014
Python 3.5 September 13, 2015
Python 3.6 December 23, 2016
Python 3.6.4 December 19, 2017
Python Applications Area
Python is known for its general purpose nature that makes it applicable in almost each
domain of software development. Python as a whole can be used in any sphere of
development.
Here, we are specifing applications areas where python can be applied.
1) Web Applications
Some important developments are: PythonWikiEngines, Pocoo, PythonBlogSoftware etc.
2) Desktop GUI Applications
Python provides Tk GUI library to develop user interface in python based application. Some
other useful toolkits wxWidgets, Kivy, pyqt that are useable on several platforms. The Kivy is
popular for writing multitouch applications.
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.
4) Scientific and Numeric
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 Bussiness applications like ERP and e-commerce systems. Tryton is a
high level application platform.
6) Console Based Application
We can use Python to develop console based applications. For example: IPython.
7) Audio or Video based Applications
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.
10) Applications for Images
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 Example
Python is easy to learn and code and can be execute with python interpreter. We can also
use Python interactive shell to test python code immediately.
A simple hello world example is given below. Write below code in a file and save
with .py extension. Python source file has .pyextension.
[Link]
1. print("hello world by python!")
Execute this example by using following command.
1. Python3 [Link]
After executing, it produces the following output to the screen.
Output
hello world by python!
Python Example using Interactive Shell
Python interactive shell is used to test the code immediately and does not require to write
and save code in file.
Python code is simple and easy to run. Here is a simple Python code that will print "Welcome
to Python".
A simple python example is given below.
1. >>> a="Welcome To Python"
2. >>> print a
3. Welcome To Python
4. >>>
Python 3.4 Example
In python 3.4 version, you need to add parenthesis () in a string code to print it.
1. >>> a=("Welcome To Python Example")
2. >>> print a
3. Welcome To Python Example
4. >>>
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 recomended to use lowercase letters for variable name. Rahul and rahul both are two
different variables.
Note - Variable name should not be a keyword.
Declaring Variable and Assigning Values
Python does not bound us to declare variable before using in the application. It allows us to
create variable at required time.
We don't need to declare explicitly variable in Python. When we assign any value to the
variable that variable is declared automatically.
The equal (=) operator is used to assign value to a variable.
Eg:
Output:
1. >>>
2. 10
3. ravi
4. 20000.67
5. >>>
Multiple Assignment
Python allows us to assign a value to multiple variables in a single statement which is also
known as multiple assignment.
We can apply multiple assignments in two ways either by assigning a single value to
multiple variables or assigning multiple values to multiple variables. Lets see given
examples.
1. Assigning single value to multiple variables
Eg:
1. x=y=z=50
2. print x
3. print y
4. print z
Output:
1. >>>
2. 50
3. 50
4. 50
5. >>>
[Link] multiple values to multiple variables:
Eg:
1. a=5
b=10
c=15
2. print a
3. print b
4. print c
Output:
1. >>>
2. 5
3. 10
4. 15
5. >>>
The values will be assigned in the order in which variables appears.
Basic Fundamentals:
This section contains the basic fundamentals of Python like :
i)Tokens and their types.
ii) Comments
a)Tokens:
o Tokens can be defined as a punctuator mark, reserved words and each individual
word in a statement.
o Token is the smallest unit inside the given program.
There are following tokens in Python:
o Keywords.
o Identifiers.
o Literals.
o Operators.
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. Following is the List of Python Keywords.
True False None and as
asset def class continue break
else finally elif del except
global for if from import
raise try or return pass
nonloca in not is lambda
l
Identifiers
Identifiers are the names given to the fundamental building blocks in a program.
These can be variables ,class ,object ,functions , lists , dictionaries etc.
There are certain rules defined for naming i.e., Identifiers.
I. An identifier is a long sequence of characters and numbers.
[Link] special character except underscore ( _ ) can be used as an identifier.
[Link] should not be used as an identifier name.
[Link] is case sensitive. So using case is significant.
[Link] character of an identifier can be character, underscore ( _ ) but not digit.
Python Literals
Literals can be defined as a data that is given in a variable or constant.
Python support the following literals:
I. String literals:
String literals can be formed by enclosing a text in the quotes. We can use both single as
well as double quotes for a String.
Eg:
"Aman" , '12345'
Types of Strings:
There are two types of Strings supported in Python:
a).Single line String- Strings that are terminated within a single line are known as Single line
Strings.
Eg:
1. >>> text1='hello'
b).Multi line String- A piece of text that is spread along multiple lines is known as Multiple
line String.
There are two ways to create Multiline Strings:
1). Adding black slash at the end of each line.
Eg:
1. >>> text1='hello\
2. user'
3. >>> text1
4. 'hellouser'
5. >>>
2).Using triple quotation marks:-
Eg:
1. >>> str2='''''welcome
2. to
3. SSSIT'''
4. >>> print str2
5. welcome
6. to
7. SSSIT
8. >>>
[Link] literals:
Numeric Literals are immutable. Numeric literals can belong to following four different
numerical types.
Int(signed integers) Long(long integers) float(floating point) Complex(complex
Numbers( can be both Integers of unlimited Real numbers with In the form of a+
positive and negative) size followed by both integer and where a forms th
with no fractional lowercase or uppercase fractional part eg: - real part and b
[Link]: 100 L eg: 87032845L 26.2 forms the
imaginary part o
complex number
eg: 3.14j
III. Boolean literals:
A Boolean literal can have any of the two values: True or False.
IV. Special literals.
Python contains one special literal i.e., None.
None is used to specify to that field that is not created. It is also used for end of lists in
Python.
Eg:
1. >>> val1=10
2. >>> val2=None
3. >>> val1
4. 10
5. >>> val2
6. >>> print val2
7. None
8. >>>
Python Operators
Operators are particular symbols that are used to perform operations on operands. It returns
result that can be used in application.
Example
1. 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. Assignment Operators.
4. Logical Operators.
5. Membership Operators.
6. Identity Operators.
7. Bitwise Operators.
Arithmetic Operators
The following table contains the arithmetic operators that are used to perform arithmetic
operations.
Operators Description
// Perform Floor division(gives integer value after division)
+ To perform addition
- To perform subtraction
* To perform multiplication
/ To perform division
% To return remainder after division(Modulus)
** Perform exponent(raise to power)
Example
1. >>> 10+20
2. 30
3. >>> 20-10
4. 10
5. >>> 10*2
6. 20
7. >>> 10/2
8. 5
9. >>> 10%3
10. 1
11. >>> 2**3
12. 8
13. >>> 10//3
14. 3
15. >>>
Relational Operators
The following table contains the relational operators that are used to check relations.
Operators Description
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
<> Not equal to(similar to !=)
eg:
1. >>> 10<20
2. True
3. >>> 10>20
4. False
5. >>> 10<=10
6. True
7. >>> 20>=15
8. True
9. >>> 5==6
10. False
11. >>> 5!=6
12. True
13. >>> 10<>2
14. True
15. >>>
Assignment Operators
The following table contains the assignment operators that are used to assign values to the
variables.
Operators Description
= Assignment
/= Divide and Assign
+= Add and assign
-= Subtract and Assign
*= Multiply and assign
%= Modulus and assign
**= Exponent and assign
//= Floor division and assign
Example
1. >>> c=10
2. >>> c
3. 10
4. >>> c+=5
5. >>> c
6. 15
7. >>> c-=5
8. >>> c
9. 10
10. >>> c*=2
11. >>> c
12. 20
13. >>> c/=2
14. >>> c
15. 10
16. >>> c%=3
17. >>> c
18. 1
19. >>> c=5
20. >>> c**=2
21. >>> c
22. 25
23. >>> c//=2
24. >>> c
25. 12
26. >>>
Logical Operators
The following table contains the arithmetic operators that are used to perform arithmetic
operations.
Operators Description
and Logical AND(When both conditions are true output will be
true)
or Logical OR (If any one condition is true output will be true)
not Logical NOT(Compliment the condition i.e., reverse)
Example
1. a=5>4 and 3>2
2. print a
3. b=5>4 or 3<2
4. print b
5. c=not(5>4)
6. print c
Output:
1. >>>
2. True
3. True
4. False
5. >>>
Membership Operators
The following table contains the membership operators.
Operators Description
in Returns true if a variable is in sequence of another variable, else false.
not in Returns true if a variable is not in sequence of another variable, else false.
Example
1. a=10
2. b=20
3. list=[10,20,30,40,50];
4. if (a in list):
5. print "a is in given list"
6. else:
7. print "a is not in given list"
8. if(b not in list):
9. print "b is not given in list"
10. else:
11. print "b is given in list"
Output:
1. >>>
2. a is in given list
3. b is given in list
4. >>>
Identity Operators
The following table contains the identity operators.
Operators Description
is Returns true if identity of two operands are same, else false
is not Returns true if identity of two operands are not same, else false.
Example
1. a=20
2. b=20
3. if( a is b):
4. print a,b have same identity
5. else:
6. print a, b are different
7. b=10
8. if( a is not b):
9. print a,b have different identity
10. else:
11. print a,b have same identity
Output
1. >>>
2. a,b have same identity
3. a,b have different identity
4. >>>
Python Comments
Python supports two types of comments:
1) Single lined comment:
In case user wants to specify a single line comment, then comment must start with ?#?
Eg:
1. # This is single line comment.
2) Multi lined Comment:
Multi lined comment can be given inside triple quotes.
eg:
1. ''''' This
2. Is
3. Multipline comment'''
eg:
1. #single line comment
2. print "Hello Python"
3. '''''This is
4. multiline comment'''
Python If Statements
There are various types of if statements in Python.
o if statement
o if-else statement
o nested if statement
Python If Statement Syntax
1. if(condition):
2. statements
Python If statement flow chart
Python If Statement Example
1. a=10
2. if a==10:
3. print "Welcome to javatpoint"
Output:
Hello User
Python If Else Statements
The If statement is used to test specified condition and if the condition is true, if block
executes, otherwise else block executes.
The else statement executes when the if statement is false.
Python If Else Syntax
1. if(condition): False
2. statements
3. else: True
4. statements
Example-
1. year=2000
2. if year%4==0:
3. print "Year is Leap"
4. else:
5. print "Year is not Leap"
Output:
Year is Leap
Python Nested If Else Statement
In python, we can use nested If Else to check multiple conditions. Python
provides elif keyword to make nested If statement.
This statement is like executing a if statement inside a else statement.
Python Nested If Else Syntax
1. If statement:
2. Body
3. elif statement:
4. Body
5. else:
6. Body
Python Nested If Else Example
1. a=10
2. if a>=20:
3. print "Condition is True"
4. else:
5. if a>=15:
6. print "Checking second value"
7. else:
8. print "All Conditions are false"
Output:
All Conditions are false.
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).
Python For Loop Syntax
1. for <variable> in <sequence>:
Output:
1. 1
2. 7
3. 9
Python For Loop Simple Example
1. num=2
2. for a in range (1,6):
3. print num * a
Output:
1. 2
2.
3. 4
4.
5. 6
6.
7. 8
8.
9. 10
Python Example to Find Sum of 10 Numbers
1. sum=0
2. for n in range(1,11):
3. sum+=n
4. print sum
Output:
1. 55
Python While Loop
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
1. while <expression>:
2. Body
Here, loop Body will execute till the expression passed is true. The Body may be a single
statement or multiple statement.
Python While Loop Example 1
1. a=10
2. while a>0:
3. print "Value of a is",a
4. a=a-2
print "Loop is Completed"
Output:
1. >>>
2. Value of a is 10
3. Value of a is 8
4. Value of a is 6
5. Value of a is 4
6. Value of a is 2
7. Loop is Completed
8. >>>
Python While Loop Example 2
1. n=153
2. sum=0
3. while n>0:
4. r=n%10
5. sum+=r
6. n=n/10
7. print sum
Output:
1. >>>
2. 9
3. >>>
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.
When break statement is applied the control points to the line following the body of the loop,
hence applying break statement makes the loop to terminate and controls goes to next line
pointing after loop body.
Python Break Example 1
1. for i in [1,2,3,4,5]:
2. if i==4:
3. print "Element found"
4. break
5. print i,
Output:
1. >>>
2. 1 2 3 Element found
3. >>>
Python Break Example 2
1. for letter in 'Python3':
2. if letter == 'o':
3. break
4. print (letter)
Output:
1. P
2. y
3. t
4. h
Python Continue Statement
Python Continue Statement is a jump statement which is used to skip execution of current
iteration. After skipping, loop continue with next iteration.
We can use continue statement with for as well as while loop in Python.
Python Continue Statement Example
1. a=0
2. while a<=5:
3. a=a+1
4. if a%2==0:
5. continue
6. print a
7. print "End of Loop"
Output:
1. >>>
2. 1
3. 3
4. 5
5. End of Loop
6. >>>
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.
Python Pass Syntax
1. pass
Python Pass Example
1. for i in [1,2,3,4,5]:
2. if i==3:
3. pass
4. print "Pass when value is",i
5. print i,
Output:
1. >>>
2. 1 2 Pass when value is 3
3. 3 4 5
4. >>>