MC4103 - PYTHON PROGRAMMING
UNIT I BASICS OF PYTHON: Introduction to Python Programming – Python Interpreter and
Interactive Mode– Variables and Identifiers – Arithmetic Operators – Values and Types – Statements.
Operators – Boolean Values – Operator Precedence – Expression – Conditionals: If-Else Constructs –
Loop Structures/Iterative Statements – While Loop – For Loop – Break Statement-Continue
statement – Function Call and Returning Values – Parameter Passing – Local and Global Scope –
Recursive Functions.
UNIT II DATA TYPES IN PYTHON: Lists, Tuples, Sets, Strings, Dictionary, Modules: Module Loading
and Execution – Packages – Making Your Own Module – The Python Standard Libraries.
UNIT III FILE HANDLING AND EXCEPTION HANDLING: Files: Introduction – File Path – Opening
and Closing Files – Reading and Writing Files –File Position –Exception: Errors and Exceptions,
Exception Handling, Multiple Exceptions
UNIT IV MODULES, PACKAGES AND FRAMEWORKS: Modules: Introduction – Module Loading and
Execution – Packages – Making Your Own Module – The Python Libraries for data processing, data
mining and visualization- NUMPY, Pandas, Matplotlib, Plotly-Frameworks- -Django, Flask, Web2Py
UNIT V OBJECT ORIENTED PROGRAMMING IN PYTHON: Creating a Class, Class methods, Class
Inheritance, Encapsulation, Polymorphism, class method vs. static methods, Python object
persistence.
UNIT I BASICS OF PYTHON
INTRODUCTION TO PYTHON PROGRAMMING: (GENERAL)
Python is a popular programming language. Python is a general-purpose interpreted, interactive,
object-oriented, and high-level programming language.
It was created by Guido van Rossum, and released in 1991.
Many large companies use the Python programming language include NASA, Google, YouTube,
BitTorrent, etc. Python programming is widely used in Artificial Intelligence, Natural Language
Generation, Neural Networks and other advanced fields of Computer Science.
It is used for:
o Web development (server-side),
o Software development,
o Mathematics,
o System scripting
1
,WHY PYTHON? (2M)
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines than some other
programming languages.
WHY TO LEARN PYTHON?(2M)
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 a MUST for students and working professionals to become a great Software Engineer
specially when they are working in Web Development Domain.
KEY ADVANTAGES OF LEARNING PYTHON: (2M)
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.
CHARACTERISTICS OF PYTHON (2M)
It provides rich data types and easier to read syntax than any other programming languages.
It is a platform independent scripted language with full access to operating system API's.
Compared to other programming languages, it allows more run-time flexibility.
It includes the basic text manipulation facilities of Perl and Awk.
Libraries in Pythons are cross-platform compatible with Linux, Macintosh, and Windows.
For building large applications, Python can be compiled to byte-code.
Python supports functional and structured programming as well as OOP.
It supports interactive mode that allows interacting Testing and debugging of snippets of code
In Python, since there is no compilation step, editing, debugging and testing is fast.
2
, APPLICATIONS OF PYTHON (FEATURES) (2M)
Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This
allows the student to pick up the language quickly.
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.
The Python Command Line (2M)
To test a short amount of code in python sometimes it is quickest and easiest not to write the
code in a file. This is made possible because Python can be run as a command line itself. Which will
write "Hello, World!" in the command line:
Whenever you are done in the python command line, you can simply type the following to quit the
python command line interface:
3
, PYTHON SYNTAX (2M)
Python syntax can be executed by writing directly in the Command Line:
>>> print("Hello, World!")
Hello, World!
Or by creating a python file on the server, using the .py file extension, and running it in the Command
Line:
C:\Users\Your Name>python myfile.py
COMMENTS (2M)
Python has commenting capability for the purpose of in-code documentation.
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
Comments start with a #, and Python will render the rest of the line as a comment.
EXAMPLE:
print("Hello, World!") #This is a comment.
OUTPUT:
Hello, World!
PYTHON INDENTATION(2M)
Indentation refers to the spaces at the beginning of a code line. Where in other programming
languages the indentation in code is for readability only, the indentation in Python is very important.
EXAMPLE
if 5 > 2:
print("Five is greater than two!")
Python will give you an error if you skip the indentation:
File "demo_indentation_test.py", line 2
print("Five is greater than two!")
^
IndentationError: expected an indented block (Syntax Error)
4
UNIT I BASICS OF PYTHON: Introduction to Python Programming – Python Interpreter and
Interactive Mode– Variables and Identifiers – Arithmetic Operators – Values and Types – Statements.
Operators – Boolean Values – Operator Precedence – Expression – Conditionals: If-Else Constructs –
Loop Structures/Iterative Statements – While Loop – For Loop – Break Statement-Continue
statement – Function Call and Returning Values – Parameter Passing – Local and Global Scope –
Recursive Functions.
UNIT II DATA TYPES IN PYTHON: Lists, Tuples, Sets, Strings, Dictionary, Modules: Module Loading
and Execution – Packages – Making Your Own Module – The Python Standard Libraries.
UNIT III FILE HANDLING AND EXCEPTION HANDLING: Files: Introduction – File Path – Opening
and Closing Files – Reading and Writing Files –File Position –Exception: Errors and Exceptions,
Exception Handling, Multiple Exceptions
UNIT IV MODULES, PACKAGES AND FRAMEWORKS: Modules: Introduction – Module Loading and
Execution – Packages – Making Your Own Module – The Python Libraries for data processing, data
mining and visualization- NUMPY, Pandas, Matplotlib, Plotly-Frameworks- -Django, Flask, Web2Py
UNIT V OBJECT ORIENTED PROGRAMMING IN PYTHON: Creating a Class, Class methods, Class
Inheritance, Encapsulation, Polymorphism, class method vs. static methods, Python object
persistence.
UNIT I BASICS OF PYTHON
INTRODUCTION TO PYTHON PROGRAMMING: (GENERAL)
Python is a popular programming language. Python is a general-purpose interpreted, interactive,
object-oriented, and high-level programming language.
It was created by Guido van Rossum, and released in 1991.
Many large companies use the Python programming language include NASA, Google, YouTube,
BitTorrent, etc. Python programming is widely used in Artificial Intelligence, Natural Language
Generation, Neural Networks and other advanced fields of Computer Science.
It is used for:
o Web development (server-side),
o Software development,
o Mathematics,
o System scripting
1
,WHY PYTHON? (2M)
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines than some other
programming languages.
WHY TO LEARN PYTHON?(2M)
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 a MUST for students and working professionals to become a great Software Engineer
specially when they are working in Web Development Domain.
KEY ADVANTAGES OF LEARNING PYTHON: (2M)
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.
CHARACTERISTICS OF PYTHON (2M)
It provides rich data types and easier to read syntax than any other programming languages.
It is a platform independent scripted language with full access to operating system API's.
Compared to other programming languages, it allows more run-time flexibility.
It includes the basic text manipulation facilities of Perl and Awk.
Libraries in Pythons are cross-platform compatible with Linux, Macintosh, and Windows.
For building large applications, Python can be compiled to byte-code.
Python supports functional and structured programming as well as OOP.
It supports interactive mode that allows interacting Testing and debugging of snippets of code
In Python, since there is no compilation step, editing, debugging and testing is fast.
2
, APPLICATIONS OF PYTHON (FEATURES) (2M)
Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This
allows the student to pick up the language quickly.
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.
The Python Command Line (2M)
To test a short amount of code in python sometimes it is quickest and easiest not to write the
code in a file. This is made possible because Python can be run as a command line itself. Which will
write "Hello, World!" in the command line:
Whenever you are done in the python command line, you can simply type the following to quit the
python command line interface:
3
, PYTHON SYNTAX (2M)
Python syntax can be executed by writing directly in the Command Line:
>>> print("Hello, World!")
Hello, World!
Or by creating a python file on the server, using the .py file extension, and running it in the Command
Line:
C:\Users\Your Name>python myfile.py
COMMENTS (2M)
Python has commenting capability for the purpose of in-code documentation.
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
Comments start with a #, and Python will render the rest of the line as a comment.
EXAMPLE:
print("Hello, World!") #This is a comment.
OUTPUT:
Hello, World!
PYTHON INDENTATION(2M)
Indentation refers to the spaces at the beginning of a code line. Where in other programming
languages the indentation in code is for readability only, the indentation in Python is very important.
EXAMPLE
if 5 > 2:
print("Five is greater than two!")
Python will give you an error if you skip the indentation:
File "demo_indentation_test.py", line 2
print("Five is greater than two!")
^
IndentationError: expected an indented block (Syntax Error)
4