Professional Documents
Culture Documents
find() Searches the string for a specified value and returns the position of
where it was found
index() Searches the string for a specified value and returns the position of
where it was found
join() Converts the elements of an iterable into a string
⚫Example: OUTPUT:
True
as np instead of numpy.
⚫ Example
• OUTPUT:
NumPy
Package
Ndarray
(one object
of NumPy
package)
Array() in
ndarray
Faculty: Komal Waykole
21
Arrays: Creating NumPy ndarray object
⚫NumPy is used to work with arrays. The array object in NumPy
is called ndarray.
⚫We can create a NumPy ndarray object by using
the array() function.
⚫type(): This built-in Python function tells us the type of the
object passed to it. Like in above code it shows
that arr is numpy.ndarray type.
⚫Example OUTPUT:
⚫Example OUTPUT:
A tuple is passed
to the array()
⚫ OUTPUT:
⚫ 42
⚫ OUTPUT:
⚫ [1 2 3 4 5]
⚫OUTPUT:
⚫Indexing
⚫Slicing
⚫Iterating
⚫Search
⚫Sort
⚫Matrix multiplication
⚫ 2nd element
on
1st dim: 2
⚫OUTPUT: 6
⚫OUTPUT:
Last element from 2nd dim: 9
⚫STEP
⚫Use the step value to determine the step of the slicing:
⚫ OUTPUT:
⚫ [ 2 4]
⚫Example: OUTPUT:
⚫OUTPUT:
⚫Example:
# Output: 3
prime_numbers = [11, 3, 7, 5, 2]
# sort the list
prime_numbers.sort()
print(prime_numbers)
# Repeat
print(("Repeat",) * 3)
Output: ('Repeat', 'Repeat', 'Repeat')
Example:
my_tuple = ('a', 'p', 'p', 'l', 'e',)
print(my_tuple.count('p')) # Output: 2
print(my_tuple.index('l')) # Output: 3
Output:
[(20, 'Nihaarika', 5500.75),
(30, 'Vanaja', 8900.0),
(40, 'Karthik', 5000.5),
(100, 'Varun', 9000.9)]
82
Faculty: Komal Waykole
Dictionary:
• Python dictionary is an unordered collection of
items. Each item of a dictionary has a key/value
pair.
• Dictionaries are optimized to retrieve values
when the key is known.
83
Faculty: Komal Waykole
Dictionary: Creating Dictionary
• Creating a dictionary is as simple as placing
items inside curly braces {} separated by
commas.
• An item has a key and a corresponding value that
is expressed as a pair (key: value).
• While the values can be of any data type and can
repeat, keys must be of immutable type (string,
number or tuple with immutable elements) and
must be unique.
• Example:
# using dict()
my_dict = dict({1:'apple', 2:'ball'})
84
Faculty: Komal Waykole
Dictionary: Accessing Dictionary
• Indexing:
⚫ While indexing is used with other data types to
access values, a dictionary uses keys. Keys can be
used either inside square brackets [ ] or with the
get() method.
⚫ If we use the square brackets [ ], KeyError is
raised in case a key is not found in the dictionary.
On the other hand, the get() method returns None
if the key is not found.
⚫ Example:
85
Faculty: Komal Waykole
Dictionary: Changing values
• Example: update value
my_dict = {'name': 'Jack', 'age': 26}
my_dict['age'] = 27
print(my_dict)
Output: {'age': 27, 'name': 'Jack’}
• Example: Adding value
my_dict = {'name': 'Jack', 'age': 26}
my_dict['address'] = 'Downtown'
print(my_dict)
Output: {'address': 'Downtown', 'age': 27, 'name':
'Jack'}
86
Dictionary: deleting values
• pop() method. This method removes an item
with the provided key and returns the value.
• The popitem() method can be used to remove
and return an arbitrary (key, value) item pair from
the dictionary.
• All the items can be removed at once, using the
clear() method.
• We can also use the del keyword to remove
individual items or the entire dictionary itself.
87
Dictionary: deleting values
• Example:
squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print(squares.pop(4)) # Output: 16
squares.clear() # remove all items
del squares #deletes entire dictionary
88
Dictionary: iteration
• Example:
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
for i in squares:
print(squares[i])
OUTPUT:
1
9
25
49
81
89
Dictionary: membership operator
• if a key is in a dictionary or not using the
keyword in. the membership test is only for the
keys and not for the values.
• Example:
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
print(1 in squares)
print(2 not in squares)
OUTPUT:
True
true
90
Dictionary: Built in functions
91
Dictionary: Built in functions
• Example:
squares = {0: 0, 1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
print(all(squares))
print(any(squares))
print(len(squares))
print(sorted(squares))
OUTPUT:
False
True
6
[0, 1, 3, 5, 7, 9]
92
Dictionary: methods used
Method Description
update([ Updates the dictionary with the key/value pairs from other,
other]) overwriting existing keys.
93
Dictionary: Converting lists to dictionary
• There are two steps involved to convert the lists
into a dictionary.
1. The first step is to create a 'zip' class object by
passing the two lists to zip() function as:
z = zip(countries, cities)
⚫ The zip() function is useful to convert the
sequences into a zip class object.
2. The second step is to convert the zip object into
a dictionary by using dict() function.
d = dict(z)
94
Dictionary:
Convert two list into dictionary
countries = ["USA", "India", "Germany", "France"]
cities = ['Washington', 'New Delhi', 'Berlin', 'Paris']
z = zip(countries, cities)
d = dict(z)
print('{:15s} -- {:15s}'.format('COUNTRY', 'CAPITAL'))
for k in d:
print('{:15s} -- {:15s}'.format(k, d[k]))
95
Important questions
1. Difference between find() and index() in python string.
2. Write a python program to change the case of string to
uppercase and lowercase.
3. Write a python program to replace string” jungle” with
“forest” in string “welcome to the jungle”.
4. Write short note on numpy
5. With example explain any 4 functions used with
list/tuple/string/array.
6. Difference between extend() and append() in list.
7. What is list comprehension.
8. Write short note on dimensions of array.
9. Write the use of ndim()
10. Write methods used by dictionaries.
11. Write short note on sort() for list and arrays
12. Advantages of tuples over list.