Professional Documents
Culture Documents
Object Oriented Programming Using Python
Object Oriented Programming Using Python
Object Oriented Programming Using Python
Unit-1
Introduction to Object-Oriented Programming:
Computer Programming and Programming Languages
Features of Object-Oriented Programming
Merits and Demerits of OOPs .
Applications of Object Oriented Programming,
Differences Between Popular Programming Languages
Basics of Python Programming:
Features, History, Future, Writing and Executing First Python Program,
Literal Constants,
Variables and Identifiers, Data Types, Input Operation, Comments, Reserved Words,
Indentation, Operators and Expressions,
Expressions in Python, Operations on Strings, Other Data Types, Type Conversion
IT CBIT
Computer programming and programming languages
Programming languages
High levelProgramming
Low level progrmming languages
languages
IT CBIT
First Generation: Machine Language
All the commands and data values are expressed using 0s and 1s, corresponding to the off and on electrical states in a
computer.
Fifth-generation programming languages (5GLs) are centered on solving problems using the constraints given to a
program rather than using an algorithm written by a programmer.
Typical examples of 5GLs include Prolog(programming with logic), OPS5(official production system), Mercury,
Used for creation of reliable, fast, large programs.
IT CBIT
Features of Oop
1. Class
2. Object
3. Data hiding
4. Data abstraction
5. Data encapsulation
6. Inheritance
7. Polymorphism
8. Containership or composition
9. Delegation
10. Method and message passing
IT CBIT
Classes, Objects, and Methods
A class is used to describe something in the world, such as occurrences, things, external
entities, and so on.
A class provides a template or a blueprint that describes the structure and behavior of a
set of similar objects.
Once we have the definition for a class, a specific instance of the class can be easily
created.
A class can have multiple instances or objects. Every object contains some data and
procedures. They are also called methods.
A method is a function associated with a class. It defines the operations that the object
can execute when it receives a message. In OOP language, only methods of the class can
access and manipulate the data stored in an instance of the class (or object).
Two objects can communicate with each other through messages. An object asks
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
19
another object to invoke one of its methods by sending it a message.
Inheritance
Inheritance is a concept of OOP in which a new class is created from an existing class. The new
class, often known as a sub-class, contains the attributes and methods of the parent class
The new class, known as sub-class or derived class, inherits the attributes and behavior of the
pre-existing class, which is referred to as super-class or parent class.
The inheritance relationship of sub- and super classes generates a hierarchy. Therefore,
inheritance relation is also called ‘is-a’ relation.
A sub-class not only has all the states and behaviors associated with the super-class but has
other specialized features (additional data or methods) as well.
The main advantage of inheritance is the ability to reuse the code. When we want a specialized
class, we do not have to write the entire code for that class from scratch. We can inherit a class
from a general class and add the specialized code for the sub-class.
Data abstraction refers to the process by which data and functions are defined in such a way that only essential details are
revealed and the implementation details are hidden. The main focus of data abstraction is to separate the interface and
the implementation of a program.
Data encapsulation, also called data hiding, is the technique of packing data and functions into a single component (class)
to hide implementation details of a class from the users. Users are allowed to execute only a restricted set of operations
(class methods) on the data members of the class. Therefore, encapsulation organizes the data and methods into a
structure that prevents data access by any function (or method) that is not specified in the class. This ensures the integrity
of the data contained in the object.
Programs written using object oriented languages have greater processing overhead as they demand more resources.
• Requires more skills to learn and implement the concepts.
• Beneficial only for large and complicated programs.
• Even an easy to use software when developed using OOP is hard to be build.
• OOP cannot work with existing systems.
• Programmers must have a good command in software engineering and programming methodology.
IT CBIT
1
Python Programming
Using Problem Solving Approach
Reema Thareja
www.python.org
Choose your OS
Download
Install
Once installation is over you will find
Python IDLE
When you install the Python language on your computer, one of the
items that is installed is the Python interpreter.
The Python interpreter is a program that can read Python programming
statements and execute them.
You can use the interpreter in two modes:
interactive mode and
script mode.
>>>
When you call the print function, you type the word
print, followed by a set of parentheses.
Inside the parentheses, you type an argument, which is
the data that you want displayed on the screen.
print('Kate Austen')
print('123 Full Circle Drive')
print('Asheville, NC 28899')
Program Output
Kate Austen
123 Full Circle Drive
Asheville, NC 28899
Examples:
Examples:
Example:
Example:
Example:
Examples:
Example:
Example:
Words which are defined by python has some specific meaning and
functionality are called reserved words or key words.
Python provides total 33 keywords
To find total keywords present in python run the following statements
import keyword
keyword.kwlist
IT CBIT
Indentation
Whitespace at the beginning of the line is called indentation. These whitespaces or the indentation are very important
in Python. In a Python program, the leading whitespace including spaces and tabs at the beginning of the logical line
determines the indentation level of that logical line.
Example:
IT CBIT
Arithmetic Operators
Examples:
Identity Operators
is Operator: Returns true if operands or values on both sides of the operator point to the same object and false
otherwise. For example, if a is b returns 1, if id(a) is same as id(b).
is not Operator: Returns true if operands or values on both sides of the operator does not point to the same object and
false otherwise. For example, if a is not b returns 1, if id(a) is not same as id(b).
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
63
Expressions
An expression is any legal combination of symbols (like variables, constants and operators) that represents a value. In
Python, an expression must have at least one operand (variable or constant) and can have one or more operators. On
evaluating an expression, we get a value. Operand is the value on which operator is applied.
Constant Expressions: One that involves only constants. Example: 8 + 9 – 2
Integral Expressions: One that produces an integer result after evaluating the expression. Example:
a = 10
• Floating Point Expressions: One that produces floating point results. Example: a * b / 2
• Relational Expressions: One that returns either true or false value. Example: c = a>b
• Logical Expressions: One that combines two or more relational expressions and returns a value as True or False.
Example: a>b && y! = 0
• Bitwise Expressions: One that manipulates data at bit level. Example: x = y&z
• Assignment Expressions: One that assigns a value to a variable. Example: c = a + b or c = 10
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
64
Operations on Strings
Examples:
Examples:
Example: