0% found this document useful (0 votes)
4 views

day3

python

Uploaded by

dasaf81046
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)
4 views

day3

python

Uploaded by

dasaf81046
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/ 73

Python Programming – III

- By Nimesh Kumar Dagur, CDAC Noida, India


Built-in String Methods

• capitalize(): It returns a copy of the string with only its first


character capitalized.
str.capitalize()
Built-in String Methods

• count(): The method count() returns the number of


occurrences of substring sub in the range [start, end].
Built-in String Methods

• find(): It determines if string str occurs in string, or in a


substring of string if starting index beg and ending index end
are given.

Index if found and -1 otherwise.


Built-in String Methods

• lower(): The method lower() returns a copy of the string in


which all case-based characters have been lowercased.
Built-in String Methods

• upper() :The method upper() returns a copy of the string in


which all case-based characters have been uppercased.
• swapcase(): The method swapcase() returns a copy of the string
in which all the case-based characters have had their case
swapped.
• title(): The method title() returns a copy of the string in which
first characters of all the words are capitalized.
• replace() : The method replace() returns a copy of the string in
which the occurrences of old have been replaced with new,
optionally restricting the number of replacements to max.
Data Type Conversion

• Sometimes, you may need to perform conversions between the


built-in types.
• To convert between types, you simply use the type name as a
function.
• There are several built-in functions to perform conversion from
one data type to another.
• These functions return a new object representing the converted
value.
Data Type Conversion
Data Type Conversion

bin()
Python Operators

• Operators are special symbols in Python that carry out


arithmetic or logical computation.
• The value that the operator operates on is called the operand.
Type of operators in Python

Python has a number of operators which are classified below:


• Arithmetic operators
• Comparison (Relational) operators
• Logical (Boolean) operators
• Assignment operators
• Special operators: Identity operators & membership operator
Arithmetic operators
Comparison operators
• Comparison operators are used to compare values.
• It either returns True or False according to the condition
Comparison operators
Logical operators

• True, False : True and False are truth values in Python. They
are the results of comparison operations or logical (Boolean)
operations in Python.
• True and False in python is same as 1 and 0
Logical operators
Logical operators
Logical operators
Assignment operators

• Assignment operators are used in Python to assign values to


variables.
• a = 5 is a simple assignment operator that assigns the value 5
on the right to the variable a on the left.
• There are various compound operators in Python like a += 5
that adds to the variable and later assigns the same. It is
equivalent to a = a + 5.
Assignment operators
Bitwise Operators
Special operators

• Python language offers some special type of operators like


the identity operator or the membership operator.
Identity operators

• is and is not are the identity operators in Python.


• They are used to check if two values (or variables) are located on
the same part of the memory.
• Two variables that are equal does not imply that they are
identical.
Identity operators

x1 5

y1

x3

y3
Membership operators
• in and not in are the membership operators in Python.
• They are used to test whether a value or variable is found in a
sequence (string, list, tuple and dictionary).
• In a dictionary we can only test for presence of key, not the value.
Membership operators
Order Of Operation
• First level of precedence: top to bottom
• Second level of precedence
– If there are multiple operations that are on the same level then
precedence goes from left to right.

() Brackets (inner before outer)

** Exponent

*, /, % Multiplication, division, modulo

+, - Addition, subtraction
Python Operators Precedence
Operators from highest precedence to lowest

Highest

lowest
Python Operators Precedence
Python Lists:
• The most basic data structure in Python is the sequence.
• Each element of a sequence is assigned a number - its
position or index.
• The first index is zero, the second index is one, and so forth.
• Most common sequences are lists and tuples.
• There are certain things you can do with all sequence types.
• These operations include indexing, slicing, adding,
multiplying, and checking for membership.
• In addition, Python has built-in functions for finding the
length of a sequence and for finding its largest and smallest
elements.
Python Lists:

• Lists are the most versatile of Python's compound data types.


• A list contains items separated by commas and enclosed within
square brackets ([]).
• To some extent, lists are similar to arrays in C. One difference
between them is that all the items belonging to a list can be of
different data type.
• The values stored in a list can be accessed using the slice operator (
[ ] and [ : ] ) with indexes starting at 0 in the beginning of the list
and working their way to end -1.
• The plus ( + ) sign is the list concatenation operator, and the
asterisk ( * ) is the repetition operator.
• A list can even have another list as an item. These are called nested
list.
Example
Accessing Values in Lists

• To access values in lists, use the square brackets for slicing along
with the index or indices to obtain value available at that index
Accessing Values in Lists

0 1
0 1 23 4 0 1 2 3
Updating Lists
• You can update single or multiple elements of lists by giving the
slice on the left-hand side of the assignment operator, and you
can add to elements in a list with the append() method.
Python List append() Method

• The method append() appends a passed obj into the existing list.
Delete List Elements
• To remove a list element, you can use either the del statement if
you know exactly which element(s) you are deleting or the
remove() method if you do not know
Python List remove() Method
Basic List Operations

• Lists respond to the + and * operators much like strings; they


mean concatenation and repetition here too, except that the
result is a new list, not a string.
Indexing & Slicing
• Because lists are sequences, indexing and slicing work the same
way for lists as they do for strings.
Built-in List Functions & Methods:

• len(): The method len() returns the number of elements in


the list.
Built-in List Functions & Methods:

• max(): The method max returns the elements from the list with maximum
value.
• Note: in Python3.x version max is used only for same kind of values at a time
Built-in List Functions & Methods:

• min() : The method min() returns the elements from the list with minimum value.
• Note: in Python3.x version min is used only for same kind of values at a time
Built-in List Functions & Methods:

• count() : The method count() returns count of how many times


obj occurs in list.
• Syntax:
Built-in List Functions & Methods:

• extend() : The method extend() appends the contents of seq


to list.
• seq is the list of elements.
Built-in List Functions & Methods:

• insert() : The method insert() inserts object obj into list at offset
index.
• Syntax:
Built-in List Functions & Methods:

• index() : The method index() returns the lowest index in list


that obj appears
Built-in List Functions & Methods:

• pop(): pop([i])Remove and return item at position i (last item


if i is not provided)
Built-in List Functions & Methods:

• sort(): Sort items in a list in ascending order


• reverse(): Reverse the order of items in a list
• clear(): Remove all items and empty the list
Python Tuples

• A tuple is another sequence data type that is similar to the


list.
• A tuple consists of a number of values separated by commas.
• Unlike lists, however, tuples are enclosed within parentheses.
• The main differences between lists and tuples are: Lists are
enclosed in brackets ( [ ] ) and their elements and size can be
changed, while tuples are enclosed in parentheses ( ( ) ) and
cannot be updated.
• Tuples can be thought of as read-only lists.
• The parentheses are optional but is a good practice to write
it.
• A tuple can have any number of items and they may be of
different types (integer, float, list, string etc.).
Python Tuples
Python Tuples
Python Tuples
• Creating a tuple with one element is a bit tricky.
• Having one element within parentheses is not enough.
• We will need a trailing comma to indicate that it is in fact a
tuple.
Accessing Elements in a Tuple

• There are various ways in which we can access the elements of a


tuple.
• Indexing: We can use the index operator [] to access an item in a
tuple.
• Index starts from 0. So, a tuple having 6 elements will have index
from 0 to 5.
• Trying to access an element other that this will raise an
IndexError.
• The index must be an integer. We can't use float or other types,
this will result into TypeError.
• Nested tuple are accessed using nested indexing.
Accessing Elements in a Tuple
• Negative Indexing: Python allows negative indexing for its
sequences.
• The index of -1 refers to the last item, -2 to the second last
item and so on
• Slicing: We can access a range of items in a tuple by using the
slicing operator (colon).
Changing or Deleting a Tuple
• Unlike lists, tuples are immutable.
• This means that elements of a tuple cannot be changed once it
has been assigned.
• But if the element is itself a mutable datatype like list, its nested
items can be changed.
• We can also assign a tuple to different values (reassignment).
Changing or Deleting a Tuple
• We can use + operator to combine two tuples. This is also
called concatenation.
• The * operator repeats a tuple for the given number of times.
These operations result into a new tuple.
• We cannot delete or remove items from a tuple.
• But deleting the tuple entirely is possible using the keyword del.
Python Tuple Methods
• Methods that add items or remove items are not available
with tuple.
Built-in Functions with Tuple
Built-in Functions with Tuple
Advantage of Tuple over List

• Tuples and list look quite similar except the fact that one is
immutable and the other is mutable.
• We generally use tuple for heterogeneous (different)
datatypes and list for homogeneous (similar) datatypes.
• There are some advantages of implementing a tuple than a
list. Here are a few of them:

1. Since tuple are immutable, iterating through tuple is faster


than with list. So there is a slight performance boost.
2. Tuples that contain immutable elements can be used as
key for a dictionary. With list, this is not possible.
3. If you have data that doesn't change, implementing it as
tuple will guarantee that it remains write-protected.

You might also like