0% found this document useful (0 votes)
52 views90 pages

E-Note 16784 Content Document 20240312112644AM

Uploaded by

Anuraj mahur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views90 pages

E-Note 16784 Content Document 20240312112644AM

Uploaded by

Anuraj mahur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

OBJECT ORIENTED PROGRAMMING

(23EN1202)

UNIT-1: INTRODUCTION TO OOP AND PYTHON

Dr. SRIDHAR S K Prof. Shilpa Sudheendran Prof. Priyanka M


Associate Professor - CSE Assistant Professor - CSE Assistant Professor - CSE

Email-id: [email protected] Email-id: [email protected] Email-id: [email protected]


Faculty Cabin: A523 Faculty Cabin: A539 Faculty Cabin: A539

3/12/2024 1.0001 LECTURE 1 1


Pre-requisites
➢ Basics of

▪ ‘C’ Programming concepts

3/12/2024 1.0001 LECTURE 1 2


Course Outcomes
1. Write a python program using 4 conditionals, definite loop, indefinite loop with
jump statements.
2. Write an application using lambda, recursive functions, strings and files to store
and retrieve the data from the system.
3. Write python programs using Core data structures like Lists, Tuples, Sets and
Dictionaries.
4. Implement the concepts of object-oriented concept using class, objects,
methods. Polymorphism and different levels of inheritance.
5. Implement operator overloading, overriding, single and multiple exception
handling program capability in python

3/12/2024 1.0001 LECTURE 1 3


Syllabus

3/12/2024 1.0001 LECTURE 1 4


3/12/2024 1.0001 LECTURE 1 5
3/12/2024 1.0001 LECTURE 1 6
3/12/2024 1.0001 LECTURE 1 7
3/12/2024 1.0001 LECTURE 1 8
Unit-1
INTRODUCTION TO OBJECT ORIENTED
PROGRAMMING AND PYTHON
Programming paradigms:
◦ A programming paradigm is a fundamentals style of programming that defines the way of structuring code to solve
computer problems. It's a high-level approach that defines principles, techniques, and patterns for structuring code.

These paradigms, in sequence of their application, can be classified as follows:

• Monolithic Programming –emphasizes on finding a solution

• Procedural Programming – lays stress on algorithm

• Structured Programming – focuses on modules

• Object-Oriented Programming -emphasizes on classes and objects

• Logic-Oriented Programming – focuses on goals usually expressed in predicate calculus

• Rule-Oriented Programming – makes use of ‘if-then-else’ rules for computation

• Constraint-Oriented Programming – utilizes invariant relationships to solve a problem

3/12/2024 1.0001 LECTURE 1 9


Monolithic Programming
• Monolithic programming is defined as writing a whole program in a single function that is in the main function.
A single individual can write and maintain this style of programming.
• Monolithic programming are assembly language and Basic consists of global data and sequential code.

3/12/2024 1.0001 LECTURE 1 10


Procedural Programming
• A procedural language is a sort of computer programming language that has a set of functions, instructions, and
statements that must be executed in a certain order to accomplish a job or program.
• Computer procedural languages include BASIC, C, FORTRAN, Java, and Pascal, to name a few.
• To create programs, they use variables, conditional statements, and functions that permit a computer to process
and provide the desired output.

3/12/2024 1.0001 LECTURE 1 11


Structured Programming
• Structured programming languages facilitate or enforce structured programming practices.
• Unstructured languages can also support these practices, but that requires specific steps in program design and
implementation.
• Some examples of languages that support structured programming include: C, C++, Java, C#

3/12/2024 1.0001 LECTURE 1 12


Object-Oriented Programming
• Object-oriented programming (OOP) is a programming paradigm based on the concept of objects, which can
contain data and code: data in the form of fields (often known as attributes or properties), and code in the form
of procedures (often known as methods).
• OOP allows objects to interact with each other using four basic principles: encapsulation, inheritance,
polymorphism, and abstraction

3/12/2024 1.0001 LECTURE 1 13


3/12/2024 1.0001 LECTURE 1 14
Object oriented programming features
• Classes
• Object
• Encapsulation
• Inheritance
• Abstraction
• Polymorphism

3/12/2024 1.0001 LECTURE 1 15


CLASS In object-oriented programming
(OOP), a class is a blueprint for
creating objects. It defines the
structure and behavior of an
object.

Object • An object is an any real


world entity having state
and behavior.
• An object can be defined as a data
field that has unique attributes and
behavior.

3/12/2024 1.0001 LECTURE 1 16


Encapsulation Encapsulation in OOPs is the
concept of binding fields (object
state) and methods (behavior)
together as a single unit.

Inheritance In object-oriented programming


(OOP), inheritance is a mechanism
that allows a class to inherit
properties and behaviors from
another class. It is a fundamental
concept in OOP that promotes
code reuse and establishes
relationships between classes.

12-03-2024 23EN1202-OBJECT ORIENTED PROGRAMMING USING PYTHON 17


Abstraction Abstraction in OOPS is used to hide
unnecessary information and display
only necessary information to the users
interacting.

Polymorphism Polymorphism is a feature of object-


oriented programming languages
that allows a specific routine to use
variables of different types at different
times. Polymorphism in programming
gives a program the ability to redefine
methods for derived classes.

12-03-2024 23EN1202-OBJECT ORIENTED PROGRAMMING USING PYTHON 18


Applications of Object oriented Programming

3/12/2024 1.0001 LECTURE 1 19


Merits of Object oriented Programming

3/12/2024 1.0001 LECTURE 1 20


Demerits of Object oriented Programming

3/12/2024 1.0001 LECTURE 1 21


What is Python?
• Python is a general purpose, high level interpreted
language with easy syntax and dynamic semantics.

• Created by Guido Van Rossum in 1989.

3/12/2024 1.0001 LECTURE 1 22


Why is Python popular?
► Easy.
► Free.
► Application.
► Library and module for support.

3/12/2024 1.0001 LECTURE 1 23


Features of Python

3/12/2024 1.0001 LECTURE 1 24


Where is the Python is Used in Industry?

3/12/2024 1.0001 LECTURE 1 25


Career Opportunities

3/12/2024 1.0001 LECTURE 1 26


Learning Path

3/12/2024 1.0001 LECTURE 1 27


Variables
►A variable is a named place in the memory where a programmer can store data and later
retrieve the data using the variable name.
Example: X=100
Y=90.88
Python Variable Name Rules :
►Must start with a letter or underscore _
►Must consist of letters, numbers, and underscores
►Case Sensitive
Good: spam, abc, spam23, _speed
Bad: 23spam, #sign, var.12
Different: spam, Spam, SPAM

3/12/2024 1.0001 LECTURE 1 28


-> Reserved words cannot be used as a variables.

3/12/2024 1.0001 LECTURE 1 29


EXERCISE
▪ Which of the following statements assigns the value 100 to the variable x in python.
1) x:=100 2) let x=100 3) x=100

▪ What is the value of x after the following lines of code:


x=2
x=x+2
A) 4
B) 2
C) 5

3/12/2024 1.0001 LECTURE 1 30


Constants
► Fixed values such as numbers, letters and string are called as Constants.
► As you know that values of constant never change and therefore they are called as constants.
► Sting constants are always enclosed within single(‘’) or double quotes(“”).
Example:
>>> print(123)
123
>>> print(98.6)
98.6
>>> print('Hello world')
Hello world

3/12/2024 1.0001 LECTURE 1 31


Sentences or lines

3/12/2024 1.0001 LECTURE 1 32


Assignment Statements

3/12/2024 1.0001 LECTURE 1 33


Comments in Python

3/12/2024 1.0001 LECTURE 1 34


Multi – Line Comments in Python

3/12/2024 1.0001 LECTURE 1 35


Data Types
• Data types are the classification or categorization of data items.
• Data types represents the kind of value that tells what operations can be performed
on a particular data.
• Everything is an object in Python programming, data types are classes and variables
are instances (objects) of these classes.
• The following are the standard or built-in data types in Python:
• Numeric
• Sequence Type
• Boolean
• Set
• Dictionary
• Binary Types( memoryview, bytearray, bytes)

3/12/2024 1.0001 LECTURE 1 36


To define the values ​of various data types and check their data types we use the

type() function.

3/12/2024 1.0001 LECTURE 1 37


Input function
• In Python, the input() function to take input from the user.

• Whatever you enter as input, the input function converts it into a string.

• If you enter an integer value still input() function converts it into a string.

input() Function Syntax


Syntax: input(prompt)
Parameter:
Prompt: (optional) The string that is written to standard output(usually screen)
without newline.
Return: String object

3/12/2024 1.0001 LECTURE 1 38


Convert User Input to a integer number
• Python input() function which takes input from the user in string format converting it into an
integer.
• We are using the `int()` function to convert the string returned by `input()` into an integer.
Example

# Taking input from the user as integer


num = int(input("Enter a number:"))
add = num + 1

# Output
print(add)

3/12/2024 1.0001 LECTURE 1 39


Convert User Input to a float number
• We are using the `float()` function to convert the string returned by `input()` into a float number.
Example

# Taking input from the user as float

num =float(input("Enter number "))


add = num + 1

# output
print(add)

3/12/2024 1.0001 LECTURE 1 40


Python Reserve words or Keywords
Python has a set of keywords that are reserved words that cannot be used as
variable names, function names, or any other identifiers.

3/12/2024 1.0001 LECTURE 1 41


Python Indentation
❑Python indentation refers to adding white space before a statement to a particular block of code.

❑Python indentation is a way of telling a Python interpreter that the group of statements belongs to a
particular block of code.

❑Python uses indentation to indicate a block of code.

3/12/2024 1.0001 LECTURE 1 42


Example

3/12/2024 1.0001 LECTURE 1 43


Expressions
► Operators are special symbols that represent computations like addition and
multiplication.
► The values the operator is applied to are called operands.

► The combination of operators and operands are nothing but an expression.

► Because of the lack of mathematical symbols on computer keyboards - we use


“computer-speak” to express the classic math operations
► Asterisk is multiplication

► Exponentiation (raise to a power) looks different than in math.

3/12/2024 1.0001 LECTURE 1 44


Expressions

3/12/2024 1.0001 LECTURE 1 45


String operations
String Concatenation:
String concatenation means add strings together.
Use the ‘+’ character to add a variable to another variable.
Example
Output

3/12/2024 1.0001 LECTURE 1 46


String operations
String Repetition:
The repetition operator is denoted by a '*' symbol and is useful for repeating strings to a certain
length.
Example
Output

3/12/2024 1.0001 LECTURE 1 47


String operations
String Slicing:
Python slicing is about obtaining a sub-string from the given string by slicing it
respectively from start to end.
Python slicing can be done in two ways:
•Using a slice() method
•Using the array slicing [:: ] method

3/12/2024 1.0001 LECTURE 1 48


Using the slice() method
EXAMPLE:

3/12/2024 1.0001 LECTURE 1 49


Using the List/array slicing [ :: ] method

In Python, indexing syntax can be used as a substitute for the slice object.
This is an easy and convenient way to slice a string using list slicing and Array slicing
both syntax-wise and execution-wise.
A start, end, and step have the same mechanism as the slice() constructor.

3/12/2024 1.0001 LECTURE 1 50


Example :

3/12/2024 1.0001 LECTURE 1 51


Type Conversion
Python defines type conversion functions to directly convert one data type to another which is
useful in day-to-day and competitive programming.
We can achieve this by converting the type of data using a Built-in data type functions such as
int(), float(), and string(), and it should be placed before the input() function.

3/12/2024 1.0001 LECTURE 1 52


3/12/2024 1.0001 LECTURE 1 53
Checking the type of data

3/12/2024 1.0001 LECTURE 1 54


EXERCISE
1.Write a program that accepts data from a user. Your program should accept your name, age, and
height. A name should be a string, age should be an integer and the height should be float.

2.Write a program that accepts your name and five subjects mark.
Print an output where you will see your name and five subjects mark including your average mark.

3/12/2024 1.0001 LECTURE 1 55


Output Formatting in Python
The fancier way of printing an output is called as output formatting.
It gives more control while printing an output.

Four Ways of Output Formatting


►Manual string formatting
►Formatted string literals or f-strings
►The string format() method
►printf() style in C

3/12/2024 1.0001 LECTURE 1 56


1. Manual string formatting
To display objects to the output console, pass them as a comma-separated list of argument to
print() function.
Example:

3/12/2024 1.0001 LECTURE 1 57


1. Manual string formatting
➢The print() function takes two optional arguments that provide control over the output
we actually want, i.e. sep and end keyword.
➢sep keyword argument causes objects to be separated by a string passed as a value to it.
➢The end keyword argument causes output to be terminated by a string passed as a value
to it instead of the default newline.

3/12/2024 1.0001 LECTURE 1 58


2. Formatted string literals or f-strings

Formatted string literals (also called f-strings for short) let you include the value
of Python expressions inside a string by prefixing the string with f or F and writing
expressions as {expression}.
It works only in python version 3.6 and above.
Example:

3/12/2024 1.0001 LECTURE 1 59


3. The string format() method
We can also pass a number inside a bracket. A number in the brackets can be used to refer
to the position of the object passed into the format() method.

3/12/2024 1.0001 LECTURE 1 60


4. Old string formatting or printf() style in C
String objects have one unique built-in operation: the % operator (modulo). This is also
known as the string formatting or interpolation operator. Given format % values (where the
format is a string), % conversion specifications in format are replaced with zero or more
elements of values.
Example:

3/12/2024 1.0001 LECTURE 1 61


DECISION AND LOOP CONTROL STATEMENTS

Conditional branch statements


The conditional statements in programming languages decide the direction of the flow of
program execution. It is used for decision-making.
The decision is made based on the condition provided to the if and elif
statement(conditional statements).
However, if none of the conditions gets fulfilled, else part will be executed.
1. if - else statement
2. Nested if statement
3. elif statement
4. Multiple condition in if and elif statement
5. pass statement.

3/12/2024 1.0001 LECTURE 1 62


3/12/2024 1.0001 LECTURE 1 63
You should know comparison operators to write condition

3/12/2024 1.0001 LECTURE 1 64


3/12/2024 1.0001 LECTURE 1 65
3/12/2024 1.0001 LECTURE 1 66
3/12/2024 1.0001 LECTURE 1 67
3/12/2024 1.0001 LECTURE 1 68
3/12/2024 1.0001 LECTURE 1 69
3/12/2024 1.0001 LECTURE 1 70
3/12/2024 1.0001 LECTURE 1 71
3/12/2024 1.0001 LECTURE 1 72
3/12/2024 1.0001 LECTURE 1 73
3/12/2024 1.0001 LECTURE 1 74
Loops in python programming
➔ For
➔ While

❖ For loop
❖ Range function

❖ Break, continue, and Pass statement

❖ Booleans

❖ While loop

❖ Nested loop

3/12/2024 1.0001 LECTURE 1 75


3/12/2024 1.0001 LECTURE 1 76
3/12/2024 1.0001 LECTURE 1 77
Range function

3/12/2024 1.0001 LECTURE 1 78


3/12/2024 1.0001 LECTURE 1 79
Break, Continue, & Pass Statement
(Loop control statement)
➔ A break statement is used to immediately terminate a loop.
➔ A continue statement is used to skip out future commands inside a loop and
return to the top of the loop.
➔ A Pass is a null operation — when it is executed, nothing happens. It is useful
as a placeholder when a statement is required syntactically when no code
needs to be executed.
➔ These statements can be used with for or while loops.

3/12/2024 1.0001 LECTURE 1 80


3/12/2024 1.0001 LECTURE 1 81
3/12/2024 1.0001 LECTURE 1 82
Boolean
A boolean is a value that can be either True or False.
Example:
brought_food = True
brought_drink = False

- Boolean variables don't have quotation marks.


- The values must start with capital letters.

3/12/2024 1.0001 LECTURE 1 83


3/12/2024 1.0001 LECTURE 1 84
3/12/2024 1.0001 LECTURE 1 85
3/12/2024 1.0001 LECTURE 1 86
3/12/2024 1.0001 LECTURE 1 87
3/12/2024 1.0001 LECTURE 1 88
3/12/2024 1.0001 LECTURE 1 89
The else statement used with loops
The else keyword in a for loop specifies a block of code to be executed when the loop is finished.

3/12/2024 1.0001 LECTURE 1 90

You might also like