PYTHON PROGRAMMING
COURSE MATERIAL
DEPARTMENT OF COMPUTER SCIENCE
(DS)
LearningObjectives
Describe the core syntax and semantics of Python programming language.
Discover the need for working with the strings and functions.
Illustrate the process of structuring the data using lists, dictionaries, tuples and
sets.
Understand the usage of packages and Dictionaries.
UNIT – I
Introduction: The essence of computational problem solving – Limits of computational
problem solving-Computer algorithms-Computer Hardware-Computer Software-The
process of computational problem solving-Python programming language - Literals -
Variables and Identifiers - Operators - Expressions and Data types.
UNIT – II
Control Structures: Boolean Expressions - Selection Control - If Statement- Indentation in
Python- Multi-Way Selection -- Iterative Control- While Statement- Infinite loops-
Definite vs. Indefinite Loops- Boolean Flags and Indefinite Loops. Lists: List Structures -
Lists in Python - Iterating over lists in Python.
UNIT - III
Functions: Program Routines- Defining Functions- More on Functions: Calling Value-
Returning Functions- Calling Non-Value-Returning Functions- Parameter Passing -
Keyword Arguments in Python - Default Arguments in Python-Variable Scope.
UNIT - IV
Objects and their use: Software Objects - Turtle Graphics – Turtle attributes-Modular
Design: Modules - Top-Down Design - Python Modules - Text Files: Opening, reading
and writing text files - String Processing - Exception Handling.
UNIT - V
Dictionaries and Sets: Dictionary type in Python - Set Data type. Object Oriented
Programming using Python: Encapsulation - Inheritance – Polymorphism. Recursion:
Recursive Functions.
TEXT BOOK: 1. Charles Dierbach, “Introduction to Computer Science using
Python - A computational Problem solving Focus”, Wiley India Edition, 2015
UNIT 1
The Essence of Computational Problem Solving:
To solve a problem computationally, two things are needed:
Representation
Algorithm
A representation that captures all the relevant aspects of the problem, and an algorithm that solves the
problem by use of the representation.
Limits of Computational Problem solving:
Define Algorithm
Algorithm: It is a sequence of instructions designed in such a way that if the instructions
are executed in the specified sequence, the desired results will be obtained. The instructions
in an algorithm should not be repeated infinitely. The algorithm should be written in
sequence.
What are the properties of algorithm?
It is written in simple English.
Each step of an algorithm is unique and should be self explanatory.
An algorithm must have at least one input
An algorithm must have at least one output.
An algorithm has finite number of steps.
What are the building block of algorithm?
The three building block of algorithm are :
Sequence
Selection
Iteration
What is meant by selection, iteration and sequence control structures?
Sequence– the order we want the computer to execute the instructions we provide as
programmers. For example, do this first, then do this, then do that, and so forth.
Selection– selecting which path of an algorithm to execute depending on some criteria.
For example, if you passed a class in school, then we execute the operations that clap
and cheer and play a song. But if you didn’t pass the class, then maybe we would say,
“Better luck next time, hang in there!”
Iteration– looping or repeating. Many times, we want to be able to repeat a set of
operations a specific number of times or until some condition occurs.
Define python
Python is an object-oriented, high level language, interpreted, dynamic and
multipurpose programming language
Give the features of python
Easy to Use:
Expressive Language
Interpreted Language
Cross-platform language
Free and Open Source
Object-Oriented language
Extensible
There are two modes for using the Python interpreter
Interactive Mode Script Mode
Cross-platform language
Free and Open Source
Object-Oriented language
Extensible
There are two modes for using the Python interpreter
Interactive Mode
Script Mode
Running Python in interactive mode:
Without passing python script file to the interpreter, directly execute code to Python
prompt. Once you’re inside the python interpreter, then you can start.
>>> print("hello world")
Running Python in script mode:
Alternatively, programmers can store Python script source code in a file with the .py
extension, and use the interpreter to execute the contents of the file. To execute the
script by the interpreter, you have to tell the interpreter the name of the file. For
example, if you have a script name [Link].
Computer Hardware
Computer hardware comprises the physical part of a computer system. It includes the all-important
components of the central processing unit (CPU) and main memory. It also includes peripheral
components such as a keyboard, monitor, mouse, and printer.
Digital Computing: It’s All about Switches
It is essential that computer hardware be reliable and error free. The key to developing
reliable systems is to keep the design as simple as possible.
In digital computing, all information is represented as a series of digits. We are used to representing
numbers using base 10 with digits 0–9.
All information within a computer system is represented using only two digits, 0 and 1, called binary
representation.
Computer hardware, therefore, is based on the use of simple electronic “on/off” switches called
transistors that switch at very high speed.
Integrated circuits (“chips”), the building blocks of computer hardware, are comprised of millions or
even billions of transistors.
Fundamental Hardware Components
Central Processing Unit (CPU) is the “brain” of a computer system, containing digital
logic circuitry able to interpret and execute instructions.
Main memory is where currently executing programs reside, which the CPU can directly
and very quickly access. Main memory is volatile; that is, the contents are lost when the
power is turned off.
Secondary memory is nonvolatile, and therefore provides long-term storage of
programs and data. This kind of storage, for example, can be magnetic (hard drive),
optical (CD or DVD), or nonvolatile fl ash memory (such as in a USB drive).
Input/output devices include anything that allows for input (such as the mouse and
keyboard) or output (such as a monitor or printer).
Buses transfer data between components within a computer system, such as between
the CPU and main memory.