Python Language Basics
Python Language Basics
Python Language Basics
OperatorsPython Comments
Control Statement
Python IfPython If elsePython else ifPython nested ifPython for
loopPython while loopPython do whilePython breakPython
continuePython pass
Python OOPs
Python OOPs ConceptsPython Object ClassPython
ConstructorsPython InheritanceMultilevel InheritanceMultiple
Inheritance
Python Strings
Python Lists
Python Tuples
Python Dictionary
Python Functions
Python Files I/O
Python Modules
Python Exceptions
Python Date
Python Programs
INTRODUCTION
Python Features:
Python is easy to very easy to use and high level language. Thus
it is programmer-friendly language.
3) Interpreted Language:
6) Object-Oriented language:
7) Extensible:
9) GUI Proramming:
10) Integrated:
History of Python :
o ABC language.
o Modula-3
o Python Applications
o Python as a whole can be used in any sphere of
development.
o Let us see what are the major regions where Python proves
to be handy.
o 1) Console Based Application
o Python can be used to develop console based applications.
For example: IPython.
o 2) Audio or Video based Applications
o Python proves handy in multimedia section. Some of real
applications are: TimPlayer, cplay etc.
o 3) 3D CAD Applications
o Fandango is a real application which provides full features of
CAD.
o 4) Web Applications
o Python can also be used to develop web based application.
Some important developments are: PythonWikiEngines,
Pocoo, PythonBlogSoftware etc.
o 5) Enterprise Applications
o 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.
o 6) Applications for Images
o Using Python several application can be developed for
image. Applications developed are: VPython, Gogh, imgSeek
etc.
o There are several such applications which can be developed
using Python
Python Variables
Variable is a name of the memory location where data is stored.
Once a variable is stored that means a space is allocated in
memory.
Eg:
x=10
print x
name=’ravi’
print name
y=20000.67
print y;
Output:
1. >>>
2. 10
3. ravi
4. 20000.67
5. >>>
Multiple Assignment:
Multiple assignment can be done in Python at a time.
Eg:
1. x=y=z=50
2. print x
3. print y
4. print z
Output:
1. >>>
2. 50
3. 50
4. 50
5. >>>
Eg:
1. a,b,c=5,10,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 :
Python Literals
Literals can be defined as a data that is given in a variable or
constant.
I. String literals:
Eg:
"Aman" , '12345'
Types of Strings:
Eg:
1. >>> text1='hello'
Eg:
1. >>> text1='hello\
2. user'
3. >>> text1
4. 'hellouser'
5. >>>
Eg:
1. >>> str2='''welcome
2. to
3. SSSIT'''
4. >>> print str2
5. welcome
6. to
7. SSSIT
8. >>>
II.Numeric literals:
A Boolean literal can have any of the two values: True or False.
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 which operate on some values
and produce an output.
Eg:
1. 4 + 5 = 9
Here 4 and 5 are Operands and (+) , (=) signs are the operators.
They produce the output 9.
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:
Operator Description
s
// 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)
eg:
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:
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:
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
eg:
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:
Operator Description
s
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)
eg:
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:
Operator Description
s
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.
eg:
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:
Operator Description
s
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:
Eg:
1. # This is single line comment.
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
The if statement in python is same as c language which is used
test a condition. If condition is true, statement of if block is
executed otherwise it is skipped.
1. if(condition):
2. statements
Output:
Hello User
1. if(condition):
2. statements
3. else:
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 Lea
Syntax:
1. If statement:
2. Body
3. elif statement:
4. Body
5. else:
6. Body
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:
For Loop
for Loop is used to iterate a variable over a sequence(i.e., list or
string) in the order that they appear.
Syntax:
1. for <variable> in <sequence>:
Output:
1. 1
2.
3. 7
4.
5. 9
Explanation:
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
1. sum=0
2. for n in range(1,11):
3. sum+=n
print sum Nested Loops
Loops defined within another Loop is called Nested Loop.
Syntax:
1. for <expression>:
2. for <expression>:
3. Body
eg:
1. for i in range(1,6):
2. for j in range (1,i+1):
3. print (i, end=’\t’)
4. print
Output:
1. >>>
2. 1
3. 2 2
4. 3 3 3
5. 4 4 4 4
6. 5 5 5 5 5
7. >>>
Explanation:
For each value of Outer loop the whole inner loop is executed.
For each value of inner loop the Body is executed each time.
1. for i in range (1,6):
2. for j in range (5,i-1,-1):
3. print "*",
4. print
Output:
1. >>>
2. * * * * *
3. * * * *
4. * * *
5. * *
6. *
While Loop
while Loop is used to execute number of statements or body till
the condition passed in while is true. Once the condition is false,
the control will come out of the loop.
Syntax:
1. while <expression>:
2. Body
Here, body will execute multiple times till the expression passed
is true. The Body may be a single statement or multiple
statement.
Eg:
1. a=10
2. while a>0:
3. print "Value of a is",a
4. a=a-2
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. >>>
Explanation:
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
eg:
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. >>>
Continue Statement
continue Statement is a jump statement that is used to skip the
present iteration and forces next iteration of loop to take place. It
can be used in while as well as for loop statements.
eg:
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
>>> Python Pass
When you do not want any code to execute, pass Statement is
used. 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.
Syntax:
1. pass
eg:
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
6.