Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
61 views

Unit 3 Python

Uploaded by

ranjith
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Unit 3 Python

Uploaded by

ranjith
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

UNIT-3

FUNCTIONS,
LIST
TUPLE,
DICTIONARIES
Functions in PYTHON
A function is a group of related statements that
performs a specific task.
Functions are the most important aspect of an
application.
A function can be defined as the organized block
of reusable code, which can be called whenever
required
Pass data, known as parameters, into a function.
A function can return data as a result.
Functions in PYTHON
Creating a Function
In Python a function is defined using the
def keyword:
Syntax:
def my_function(parameters):
function_block
return expression
Functions in PYTHON
Example
Function definition
def hello_world():
print("hello world")

function calling
hello_world()
Functions in PYTHON
The return statement

The return statement is used at the end of the function


and returns the result of the function.
It terminates the function execution and transfers the
result where the function is called.
The return statement cannot be used outside of the
function.
Functions in PYTHON
The return statement

return [expression_list]

It can contain the expression which gets evaluated and


value is returned to the caller function.

If the return statement has no expression or does not


exist itself in the function then it returns the None
object.
Functions in PYTHON
The return statement

The return statement is used at the end of the function


and returns the result of the function.
It terminates the function execution and transfers the
result where the function is called.
The return statement cannot be used outside of the
function.
Functions in PYTHON
The return statement
def sum():
a = 10
b = 20
c = a+b
return c
print("The sum is:",sum())
Operations on Dictionaries
 Python provides a variety of operations applicable to
dictionaries.

Membership Operation ( in and not in)


The membership operator in checks if the key is present
in the dictionary and
returns True, else it returns False.
>>> dict1 = {'Mohan':95,'Ram':89,'Suhel':92,
'Sangeeta':85}

>>> 'Suhel' in dict1


True
Operations on Dictionaries
The not in operator returns True if the
key is not present in the dictionary, else it
returns False.
Operations on Dictionaries
Indexing a Dictionary
The value in a dictionary D that is currently
associated with key k is denoted by an
indexing: D[k].

Indexing with a key that is not present in


the dictionary raises an exception. For
example:
Operations on Dictionaries
Indexing a Dictionary
d = { 'x':42, 'y':3.14, 'z':7 }
d['x'] # 42
d['z'] # 7
d['a'] # raises KeyError exception
Operations on Dictionaries
The del statement, in the form del D[k],
removes from the dictionary the item
whose key is k.

If k is not a key in dictionary D, del D[k]


raises an exception
Operations on Dictionaries
Traversing A Dictionary
We can access each item of the dictionary or
traverse a dictionary using for loop.

>>> dict1 = {'Mohan':95,'Ram':89,'Suhel':92,


'Sangeeta':85}
Operations on Dictionaries
Method 1:
>>> for key in dict1:
print(key,':',dict1[key])

Mohan: 95
Ram: 89
Suhel: 92
Sangeeta: 85
Operations on Dictionaries
Method II:
>>> for key,value in dict1.items():
print(key,':',value)

Mohan: 95
Ram: 89
Suhel: 92
Sangeeta: 85
Dictionary Methods
clear()
Dictionary clear() method deletes all the
key:value pairs from the dictionary.

dictionary = {"a": 4, "b": 5, "c": 6}


dictionary.clear()
print(dictionary)
o/p : { }
Dictionary Methods
copy()
Dictionary copy() method returns a copy
of the original dictionary. Any changes
made to the items in the copied dictionary
does not affect the original dictionary.
Dictionary Methods
Example :
dict = {"a": 4, "b": 5, "c": 6}
dict1 = dict.copy()
dict["b"] = 2
print(dict)
print(dict1)
Dictionary Methods
dict()--Creates a dictionary from a
sequence of key-value pairs

fromkeys()
Dictionary fromkeys() method creates a
new dictionary from the keys of given
dictionary and an optional default value
for the key-value pairs.
Dictionary Methods
Ex:
d = {"a": 4, "b": 5, "c": 6}
d1 = dict.fromkeys(d, 2)
print(d1)

get()
Dictionary get() method returns the value
for the specified key. The key is specified
by passing as argument to get() method.
Dictionary Methods
items()
Dictionary items() method returns an
iterator of type dict_items.
We can iterate over each of the key, value
in the dictionary.
Also, the dict_items type support dynamic
update to the dictionary.
Dictionary Methods
d = {"a": 4, "b": 5, "c": 6}
for key, value in dictionary.items():
print(key, '-', value)

keys()--method returns an iterator of type


dict_keys.
d = {"a": 4, "b": 5, "c": 6}
for key in d.keys():
print(key)
Dictionary Methods
values()-method returns an iterator of type
dict_values.
d = {"a": 4, "b": 5, "c": 6}
for v in d.values():
print(v)
Dictionary Methods
pop()
Dictionary pop() method removes the key-value
pair of specified key, and returns just the value.
The key is passed as argument to pop() method.
If you do not pass any key to the pop() method,
it throws TypeError.
If a key that is not present is passed, pop()
throws KeyError.
Dictionary Methods

dictionary = {"a": 4, "b": 5, "c": 6}


x = dictionary.pop("b")
print(x)
print(dictionary)

popitem()
Dictionary popitem() method removes the
last inserted key-value pair of specified
key, and returns this key-value pair.
dictionary = {"a": 4, "b": 5, "c": 6}
x = dictionary.popitem()
print(x)
print(dictionary)
setdefault()
Dictionary setdefault() method returns the
value of the specified key if the key is
present.
If the key is not present, setdefault() method
inserts a key-value pair with the default
value and returns the default value.
d = {"a": 4, "b": 5, "c": 6}
x = d.setdefault("b")
print(x)
y = d.setdefault("m", 0)
print(y) print(dictionary)
update()
Dictionary update() method updates the
key-value pairs of this dictionary with the
key-value pairs from the dictionary passed
as argument to update() method.
Values for the keys that are present are
updated, and the for the keys that are not
present, those key-value pairs are inserted.
dictionary = {"a": 4, "b": 5, "c": 6}
dictionary_1 = {"a": 8, "m": 2, "v": 7}
dictionary.update(dictionary_1)
print(dictionary)

You might also like