DECLARATION
I, D GANESHA bearing USN 21BBTCS276, student of Bachelor of Technology,
Computer Science and Engineering, CMR University, Bengaluru, hereby declare that the
internship work entitled “Python Programming” submitted by me, for the award of the
Bachelor’s degree in Computer Science and Engineering to CMR University is a record of
bonafide work carried out independently by me under the supervision and guidance of Dr.
Parameswaran T, Asso. Prof, CSE Dept. CMR University.
I further declare that the work reported in this internship work has not been submitted and
will not be submitted, either in part or in full, for the award of any other degree in this university
or any other institute or University.
Place: Bengaluru
Date:
D GANESHA
(USN: 21BBTCS276)
i
ACKNOWLEDGEMENT
The satisfaction that accompanies the successful completion of this project would be
incomplete without the mention of the people who made it possible, without whose constant
guidance and encouragement would have made efforts go in vain.
I consider myself privileged to express gratitude and respect towards all those who guided
me through the completion of the project. I express my thanks to my Internal Internship Guide
Dr. Parameswaran Asso. Professor, Department of Computer Science and Engineering, School
of Engineering and Technology, CMR University for his constant support.
I express my sincere gratitude to my internship external guide Mr. Vempatapu Rakesh,
Certified Python trainer, InternPE. without whose constant guidance and support the internship
would not be successful.
I would like to express my thanks to Dr. P Rubini, Professor and Head Department of
Computer Science and Engineering, School of Engineering and Technology, CMR University,
Bangalore, for her encouragement that motivated me for the successful completion of internship
work.
I express my heartfelt sincere gratitude to Dr. N. Kannan, Dean, School of Engineering
and Technology, CMR University for his support.
I would like to express my sincere thanks and gratitude to our internship coordinator
Prof. Prabhakar K for his support, invaluable guidance and encouragement throughout the tenure
of this internship.
D GANESHA
ii
CONTENTS
Topics Page no.
1. Introduction to Python 1 TO 2
1.1 What is Python
1.2 History
1.3 Python Features
2. Operartor 3 TO 7
3. Collecyion In Python 8 TO 17
3.1 List
3.2 Tuple
3.3 Dictionary
4. Functions 16 TO 18
5. Python modules 19 TO 20
6. Python File input/output 21 TO 30
Python Programming
Chapter 1
INTRODUCTION
1.1 PYTHON
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is
designed to be highly readable. It uses English keywords frequently where as other languages use
punctuation, and it has fewer syntactical constructions than other languages.
Python is Interpreted: Python is processed at runtime by the interpreter. You do not need to
compile your program before executing it. This is similar to PERL and PHP.
Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter
directly to write your programs.
Python is Object-Oriented: Python supports Object-Oriented style or technique of programming
that encapsulates code within objects.
Python is a Beginner's Language: Python is a great language for the beginner-level
programmers and supports the development of a wide range of applications from simple text
processing to WWW browsers to games.
1.2 HISTORYOFPYTHON
Python was developed by Guido van Rossum in the late eighties and early nineties at the National
Research Institute for Mathematics and Computer Science in the Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68, SmallTalk,
and Unix shell and other scripting languages.
Python is copyrighted. Like Perl, Python source code is now available under the GNU General Public
License (GPL).
Python is now maintained by a core development team at the institute, although Guido van Rossum still
holds a vital role in directing its progress.
1.3 PYTHONFEATURES
Python's features include:
Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax. This
allows the student to pick up the language quickly.
1
Python Programming
Easy-to-read: Python code is more clearly defined and visible to the eyes.
Easy-to-maintain: Python's source code is fairly easy-to-maintain.
A broad standard library: Python's bulk of the library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.
Interactive Mode: Python has support for an interactive mode which allows interactive testing
and debugging of snippets of code.
Portable: Python can run on a wide variety of hardware platforms and has the same interface on
all platforms.
Extendable: You can add low-level modules to the Python interpreter. These modules enable
programmers to add to or customize their tools to be more efficient.
Databases: Python provides interfaces to all major commercial databases.
GUI Programming: Python supports GUI applications that can be created and ported to many
system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X
Window system of Unix.
Scalable: Python provides a better structure and support for large programs than shell scripting.
Python has a big list of good features:
It supports functional and structured programming methods as well as OOP.
It can be used as a scripting language or can be compiled to byte-code for building large
applications.
It provides very high-level dynamic data types and supports dynamic type checking.
IT supports automatic garbage collection.
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
2
Python Programming
Chapter 2
OPERATORS
2.1 ARIT METIC OPERATORS
Operator Description Example
+ Addition Adds values on either side of the operator. a + b = 30
- Subtraction Subtracts right hand operand from left hand a – b = -10
operand.
* Multiplication Multiplies values on either side of the operator a * b = 200
/ Division Divides left hand operand by right hand operand b/a=2
% Modulus Divides left hand operand by right hand operand b% a= 0
and returns remainder
3
Python Programming
** Exponent Performs exponential (power) calculation on a**b =10 to the power 20
operators
// Floor Division - The division of operands where 9//2 = 4 and 9.0//2.0 = 4.0,
the result is the quotient in which the digits after -11//3 = -4, -11.0//3 = -4.0
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):
2.2 ASSIGNMENT OPERATOR
Operator Description Example
= Assigns values from right side operands to left side c = a + b assigns value of a
operand + b into c
+= Add AND It adds right operand to the left operand and assign c += a is equivalent to c =
the result to left operand c+a
-= Subtract AND It subtracts right operand from the left operand and c -= a is equivalent to c = c
assign the result to left operand -a
*= Multiply AND It multiplies right operand with the left operand c *= a is equivalent to c =
and assign the result to left operand c*a
/= Divide AND It divides left operand with the right operand and c /= a is equivalent to c = c
assign the result to left operand / ac /= a is equivalent to c
=c/a
%= Modulus It takes modulus using two operands and assign the c %= a is equivalent to c =
AND result to left operand c%a
**= Exponent Performs exponential (power) calculation on c **= a is equivalent to c
AND operators and assign value to the left operand = c ** a
4
Python Programming
//= Floor Division It performs floor division on operators and assign c //= a is equivalent to c =
value to the left operand c // a
2.3 IDENTITY OPERATOR
Operator Description Example
is Evaluates to true if the variables on either side of the operator point x is y,
to the same object and false otherwise. here is results in 1
if id(x) equals
id(y).
is not Evaluates to false if the variables on either side of the operator point x is not y, here is
to the same object and true otherwise. not results in 1 if
id(x) is not equal
to id(y
2.4 COMPARISON OPERATOR
Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in (a & b) (means 0000
both operands 1100)
| Binary OR It copies a bit if it exists in either operand. (a | b) = 61 (means 0011
1101)
^ Binary XOR It copies the bit if it is set in one operand but (a ^ b) = 49 (means 0011
not both. 0001)
~ Binary Ones It is unary and has the effect of 'flipping' bits. (~a ) = -61 (means 1100
Complement 0011 in 2's complement
form due to a signed
binary number.
<< Binary Left Shift The left operands value is moved left by the a << 2 = 240 (means 1111
number of bits specified by the right operand. 0000)
5
DECLARATION
I, HIRAL MUDIGAL SRI HARI PRASAD bearing USN 21BBTCS279, student of
Bachelor of Technology, Computer Science and Engineering, CMR University, Bengaluru,
hereby declare that the internship work entitled “Python Programming” submitted by me, for the
award of the Bachelor’s degree in Computer Science and Engineering to CMR University is a
record of bonafide work carried out independently by me under the supervision and guidance of
Dr. Parameswaran T, Asso. Prof, CSE Dept. CMR University.
I further declare that the work reported in this internship work has not been submitted and
will not be submitted, either in part or in full, for the award of any other degree in this university
or any other institute or University.
Place: Bengaluru
Date:
HIRAL MUDIGAL SRI HARI PRASAD
(USN: 21BBTCS279)
i
ACKNOWLEDGEMENT
The satisfaction that accompanies the successful completion of this project would be
incomplete without the mention of the people who made it possible, without whose constant
guidance and encouragement would have made efforts go in vain.
I consider myself privileged to express gratitude and respect towards all those who guided
me through the completion of the project. I express my thanks to my Internal Internship Guide
Dr. Parameswaran Asso. Professor, Department of Computer Science and Engineering, School
of Engineering and Technology, CMR University for his constant support.
I express my sincere gratitude to my internship external guide Mr. Vempatapu Rakesh,
Certified Python trainer, InternPE. without whose constant guidance and support the internship
would not be successful.
I would like to express my thanks to Dr. P Rubini, Professor and Head Department of
Computer Science and Engineering, School of Engineering and Technology, CMR University,
Bangalore, for her encouragement that motivated me for the successful completion of internship
work.
I express my heartfelt sincere gratitude to Dr. N. Kannan, Dean, School of Engineering
and Technology, CMR University for his support.
I would like to express my sincere thanks and gratitude to our internship coordinator
Prof. Prabhakar K for his support, invaluable guidance and encouragement throughout the tenure
of this internship.
HIRAL MUDIGAL SRI HARI PRASAD
ii
CONTENTS
Topics Page no.
1. Introduction to Python 1 TO 2
1.1 What is Python
1.2 History
1.3 Python Features
2. Operartor 3 TO 7
3. Collecyion In Python 8 TO 17
3.1 List
3.2 Tuple
3.3 Dictionary
4. Functions 16 TO 18
5. Python modules 19 TO 20
6. Python File input/output 21 TO 30
Python Programming
Chapter 1
INTRODUCTION
1.1 PYTHON
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is
designed to be highly readable. It uses English keywords frequently where as other languages use
punctuation, and it has fewer syntactical constructions than other languages.
Python is Interpreted: Python is processed at runtime by the interpreter. You do not need to
compile your program before executing it. This is similar to PERL and PHP.
Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter
directly to write your programs.
Python is Object-Oriented: Python supports Object-Oriented style or technique of programming
that encapsulates code within objects.
Python is a Beginner's Language: Python is a great language for the beginner-level
programmers and supports the development of a wide range of applications from simple text
processing to WWW browsers to games.
1.2 HISTORYOFPYTHON
Python was developed by Guido van Rossum in the late eighties and early nineties at the National
Research Institute for Mathematics and Computer Science in the Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++, Algol-68, SmallTalk,
and Unix shell and other scripting languages.
Python is copyrighted. Like Perl, Python source code is now available under the GNU General Public
License (GPL).
Python is now maintained by a core development team at the institute, although Guido van Rossum still
holds a vital role in directing its progress.
1.3 PYTHONFEATURES
Python's features include:
Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax. This
allows the student to pick up the language quickly.
1
Python Programming
Easy-to-read: Python code is more clearly defined and visible to the eyes.
Easy-to-maintain: Python's source code is fairly easy-to-maintain.
A broad standard library: Python's bulk of the library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.
Interactive Mode: Python has support for an interactive mode which allows interactive testing
and debugging of snippets of code.
Portable: Python can run on a wide variety of hardware platforms and has the same interface on
all platforms.
Extendable: You can add low-level modules to the Python interpreter. These modules enable
programmers to add to or customize their tools to be more efficient.
Databases: Python provides interfaces to all major commercial databases.
GUI Programming: Python supports GUI applications that can be created and ported to many
system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X
Window system of Unix.
Scalable: Python provides a better structure and support for large programs than shell scripting.
Python has a big list of good features:
It supports functional and structured programming methods as well as OOP.
It can be used as a scripting language or can be compiled to byte-code for building large
applications.
It provides very high-level dynamic data types and supports dynamic type checking.
IT supports automatic garbage collection.
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
2
Python Programming
Chapter 2
OPERATORS
2.1 ARIT METIC OPERATORS
Operator Description Example
+ Addition Adds values on either side of the operator. a + b = 30
- Subtraction Subtracts right hand operand from left hand a – b = -10
operand.
* Multiplication Multiplies values on either side of the operator a * b = 200
/ Division Divides left hand operand by right hand operand b/a=2
% Modulus Divides left hand operand by right hand operand b% a= 0
and returns remainder
3
Python Programming
** Exponent Performs exponential (power) calculation on a**b =10 to the power 20
operators
// Floor Division - The division of operands where 9//2 = 4 and 9.0//2.0 = 4.0,
the result is the quotient in which the digits after -11//3 = -4, -11.0//3 = -4.0
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):
2.2 ASSIGNMENT OPERATOR
Operator Description Example
= Assigns values from right side operands to left side c = a + b assigns value of a
operand + b into c
+= Add AND It adds right operand to the left operand and assign c += a is equivalent to c =
the result to left operand c+a
-= Subtract AND It subtracts right operand from the left operand and c -= a is equivalent to c = c
assign the result to left operand -a
*= Multiply AND It multiplies right operand with the left operand c *= a is equivalent to c =
and assign the result to left operand c*a
/= Divide AND It divides left operand with the right operand and c /= a is equivalent to c = c
assign the result to left operand / ac /= a is equivalent to c
=c/a
%= Modulus It takes modulus using two operands and assign the c %= a is equivalent to c =
AND result to left operand c%a
**= Exponent Performs exponential (power) calculation on c **= a is equivalent to c
AND operators and assign value to the left operand = c ** a
4
Python Programming
//= Floor Division It performs floor division on operators and assign c //= a is equivalent to c =
value to the left operand c // a
2.3 IDENTITY OPERATOR
Operator Description Example
is Evaluates to true if the variables on either side of the operator point x is y,
to the same object and false otherwise. here is results in 1
if id(x) equals
id(y).
is not Evaluates to false if the variables on either side of the operator point x is not y, here is
to the same object and true otherwise. not results in 1 if
id(x) is not equal
to id(y
2.4 COMPARISON OPERATOR
Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in (a & b) (means 0000
both operands 1100)
| Binary OR It copies a bit if it exists in either operand. (a | b) = 61 (means 0011
1101)
^ Binary XOR It copies the bit if it is set in one operand but (a ^ b) = 49 (means 0011
not both. 0001)
~ Binary Ones It is unary and has the effect of 'flipping' bits. (~a ) = -61 (means 1100
Complement 0011 in 2's complement
form due to a signed
binary number.
<< Binary Left Shift The left operands value is moved left by the a << 2 = 240 (means 1111
number of bits specified by the right operand. 0000)