0% found this document useful (0 votes)
37 views29 pages

Oops

Object oriented programming
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)
37 views29 pages

Oops

Object oriented programming
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/ 29

OBJECT ORIENTED PROGRAMMING

(23EN1202)

UNIT-1: INTRODUCTION TO OOP AND PYTHON

Dr.Sridhar M Prof. Shilpa Sudheendran Prof. Priyanka S. Marellavar


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

Email-id: Email-id: Email-id: -


Faculty Cabin: Faculty Cabin: A539 Faculty Cabin: A539

3/13/2024 1.0001 LECTURE 1 1


UNIT – II 5 Hours
FUNCTIONS AND MODULES:
Need for functions, Function definition, Function call, Scope, Return
statement, Lambda functions, Recursive functions, Modules.
(Chapter 5: 5.1 to 5.11)
PYTHON STRINGS:
String operations, Immutable, string formatting operator, built-in string
methods, string slices, membership operator, comparing strings, Iterating
strings.
(Chapter 6: 6.1 to 6.9)

3/13/2024 1.0001 LECTURE 1 2


Unit-2
Python Strings
• Python string datatype is a sequence made up of one or more individual characters
where characters are letters, digits, whitespaces or any other symbol.
• Python treats strings as contiguous series of characters delimited by single,double
or triple quotes.
• Python has built in string class “str”.
• We can declare a string in several ways
• Eg: name=“India”
Country= name
graduate=‘N’
nationality=str(“Indian”)
3
Introduction
• Indexing:
• Individual characters in a string are accessed using subscript [ ] operator.
• The expression in brackets is called index.
• The index specifies a member of an ordered set and here it specifies the character
we want to access from given set of characters in a string.
• The index of first character is 0 and last is n-1 where n is number of characters in a
string.
• If you try tom exceed bounds that is below 0 and above n-1 the error occurs.

4
Traversing
• A string traversing is done by accessing characters from one index to another.

3/13/2024 1.0001 LECTURE 1 5


Concatenating, Appending and
Multiplying String
• The word concatenating means to join together.
• In python concatenation of two strings can be done using “+” operator.

• If you want to add one string at the end of another string


then we will use “+=“.

3/13/2024 1.0001 LECTURE 1 6


Contd..
• “*” operator can be used to repeat a string n number of times.

• The str() function is used to convert values of any other type into string type.
• This helps the programmer to concatenate a string with any other datatype data.

3/13/2024 1.0001 LECTURE 1 7


• Print statement prints one or more literals or values in a newline.
• If you don’t want in newline then we can use “end=‘ ‘ ”.

• A raw string literal which is prefixed by an “r” passes all the characters as it is.

3/13/2024 1.0001 LECTURE 1 8


Contd…
• ‘u’ is used to write Unicode string literals.
• Example:
• str = u"Hello, こんにちは, привет“
print(str)

3/13/2024 1.0001 LECTURE 1 9


Immutable
• Python strings are immutable which means that once created they cannot be changed.
• Whenever you try to modify an existing string variable a new string is created.
• Every object in python is stored in memory.
• We can check whether two object is referring to same object or not by using Id().
• Id() returns memory address of that object.

3/13/2024 1.0001 LECTURE 1 10


String Formatting Operator
• The ‘%’ operator takes a format string on left and the corresponding values in a tuple on the
right.
• It allows users to construct string replacing part of string with the data stored in variables.
• The syntax for string formatting operation is
• The statement begins with a format string
Consisting of a sequence of character and
Conversion specifications.

13-03-2024 23EN1202-OBJECT ORIENTED PROGRAMMING USING PYTHON 11


Contd…

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


Built in string Methods and Function
• String is an example of python objects.
• Python supports many built in methods to manipulate strings.
• A method is just like function.
• Method is invoked or called on an object.

3/13/2024 1.0001 LECTURE 1 13


3/13/2024 1.0001 LECTURE 1 14
3/13/2024 1.0001 LECTURE 1 15
3/13/2024 1.0001 LECTURE 1 16
3/13/2024 1.0001 LECTURE 1 17
Format()
• It is used for formatting strings
• Format strings have curly braces {} as placeholders or replacement fields which gets replaced.
• We can use positional arguments or keyword arguments to specify the order of fields that have
to be replaced.

3/13/2024 1.0001 LECTURE 1 18


Splitlines()
• It returns a list of lines in the string
• It uses the newline characters such as \r and \n to split lines.
• Linebreaks are not included unless keepends are True.
• Syntax :
• str.splitlines(Keepends)

3/13/2024 1.0001 LECTURE 1 19


Slice Operation
• A substring of a string is called a slice.
• Slice operation is used to refer to the sub-parts of sequences and strings.
• We can take a subset of a string from the original string by using [ ] operator also known as
slicing operator.
• Syntax of slice operation is : s[start: end] where start indicates beginning index of the substring
and end-1 is the index of the last character.
• Omitting either start or end index by default takes start or end of the string. Omitting both
means the entire string.
• Python gives flexibility to access the string either from the first or the last character.
• If we are accessing the string from first character then we can use zero as index value,when
doing it backward index starts with -1.

3/13/2024 1.0001 LECTURE 1 20


Contd….

3/13/2024 1.0001 LECTURE 1 21


Specifying stride/stepsize while
slicing the string
• In slice operation we have the option to specify stride as third argument, which refers to umber
of characters to move forward after the first character is retrieved from the string.
• The default value of the stride is 1.
• Even whitespace characters are also skipped because
they are part of a string.

3/13/2024 1.0001 LECTURE 1 22


Contd..
• Negative value can be also used as stride value.
• With -3.we have skipped every third letter of the string str.

3/13/2024 1.0001 LECTURE 1 23


ord() and chr() functions
• The ord( ) function returns the ASCII code of the character.
• chr( ) function returns character represented by the ASCII number.

3/13/2024 1.0001 LECTURE 1 24


In() and not in() operator –
Membership Operator
• In() and not in() are used in string to determine whether a string is present in another string.
• They are also known as membership operator.

3/13/2024 1.0001 LECTURE 1 25


Contd…
• In() and not in() can be used to check whether a character is present in a word.

3/13/2024 1.0001 LECTURE 1 26


Comparing Strings
• Python allows you to compare the strings using relational or comparison operators such as
<,>,<=,>= etc.

3/13/2024 1.0001 LECTURE 1 27


Contd…
• These operators compares the string by using lexicographical order i.e using ASCII value of the
character.
• The ASCII value of A-Z is 65-90 and ASCII code of a-z 97-122.

3/13/2024 1.0001 LECTURE 1 28


Iterating String
• String is a sequence type(sequence of characters).
• We can iterate through strings using for loop or while loop.

3/13/2024 1.0001 LECTURE 1 29

You might also like