INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
List,tuple,dictionary
1. Ms.MhaskeN.R.(PPS) 1
List,Tuple, set,dictionary
There are four collection data types in the Python programming language:
● List is a collection which is ordered and changeable. Allows duplicate members.
● Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
● Set is a collection which is unordered and unindexed. No duplicate members.
● Dictionary is a collection which is unordered, changeable and indexed. No duplicate
members.
When choosing a collection type, it is useful to understand the properties of that type.
Choosing the right type for a particular data set could mean retention of meaning, and, it
could mean an increase in efficiency or security.
2. Ms.MhaskeN.R.(PPS) 2
List
A list is a collection which is ordered and changeable. In Python
lists are written with square brackets.
Example
Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist)
Output:['apple', 'banana', 'cherry']
3. Ms.MhaskeN.R.(PPS) 3
● Lists are just like the arrays, declared in other languages.
● Lists need not be homogeneous always which makes it a most powerful tool in
Python.
● A single list may contain DataTypes like Integers, Strings, as well as Objects.
● Lists are mutable, and hence, they can be altered even after their creation.
● List in Python are ordered and have a definite count.
● The elements in a list are indexed according to a definite sequence and the indexing
of a list is done with 0 being the first index.
● Each element in the list has its definite place in the list, which allows duplicating of
elements in the list, with each element having its own distinct place and credibility.
● Note- Lists are a useful tool for preserving a sequence of
data and further iterating over it.
4. Ms.MhaskeN.R.(PPS) 4
Creating a List● Lists in Python can be created by just placing the sequence inside the square
brackets[].
● # Creating a List
List = []
print("Intial blank List: ")
print(List)
Output: Intial blank List:
[]
● # Creating a List with # the use of a String
List = ['itisfirstprogram']
print("nList with the use of String: ")
print(List)
Output :List with the use of String:
['itisfirstprogram']
5. Ms.MhaskeN.R.(PPS) 5
● # Creating a List with # the use of multiple values
List = [“it”,”is”,”first”,program”]
print("nList containing multiple values: ")
print(List[0])
print(List[2])
Output :it
first
● # Creating a Multi-Dimensional List # (By Nesting a list inside a List)
List = [['it','is','first'] , ['program']]
print("nMulti-Dimensional List: ")
print(List)List containing multiple values:
Output:
Multi-Dimensional List:
[['it','is','first']], ['program']]
6. Ms.MhaskeN.R.(PPS) 6
Creating a list with multiple distinct or duplicate elements
A list may contain duplicate values with their distinct positions and hence, multiple distinct or duplicate
values can be passed as a sequence at the time of list creation.
# Creating a List with
# the use of Numbers
# (Having duplicate values)
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print("nList with the use of Numbers: ")
print(List)
Output :List with the use of Numbers:
[1, 2, 4, 4, 3, 3, 3, 6, 5]
# Creating a List with
# mixed type of values
# (Having numbers and strings)
List = [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
print("nList with the use of Mixed Values: ")
print(List)
Output:
List with the use of Mixed Values:
[1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
7. Ms.MhaskeN.R.(PPS) 7
List Index
● We can use the index operator [] to access an item in a list. Index starts from
0. So, a list having 5 elements will have index from 0 to 4.
8. Ms.MhaskeN.R.(PPS) 8
Nested list are accessed using nested indexing.
● my_list = ['p','r','o','b','e']
● print(my_list[0])
# Output: p
● print(my_list[2])
# Output: o
● print(my_list[4])
# Output: e
# Error! Only integer can be used for indexing
# my_list[4.0]
# Nested List
n_list = ["Happy", [2,0,1,5]]
# Nested indexing
● print(n_list[0][1])
# Output: a
● print(n_list[1][3])
# Output: 5
9. Ms.MhaskeN.R.(PPS) 9
● 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.
● my_list = ['p','r','o','b','e']
● print(my_list[-1])
# Output: e
● print(my_list[-5])
# Output: p
10. Ms.MhaskeN.R.(PPS) 10
How to slice lists in Python?
● Slicing can be best visualized by considering the index to be between the
elements as shown below. So if we want to access a range, we need two
indices that will slice that portion from the list.
11. Ms.MhaskeN.R.(PPS) 11
● my_list = ['p','r','o','g','r','a','m','i','z']
● # elements 3rd to 5th
print(my_list[2:5])
● # elements beginning to 4th
print(my_list[:-5])
● # elements 6th to end
print(my_list[5:])
● # elements beginning to end
print(my_list[:])
12. Ms.MhaskeN.R.(PPS) 12
How to change or add elements to a
list?
● List are mutable, meaning, their elements can be changed unlike string or tuple.
● We can use assignment operator (=) to change an item or a range of items.
# mistake values
odd = [2, 4, 6, 8]
# change the 1st item
odd[0] = 1
# Output: [1, 4, 6, 8]
print(odd)
# change 2nd to 4th items
odd[1:4] = [3, 5, 7]
# Output: [1, 3, 5, 7]
print(odd)
13. Ms.MhaskeN.R.(PPS) 13
By using append()and extend()
● We can add one item to a list using append() method or add several
items using extend() method.
odd = [1, 3, 5]
odd.append(7)
# Output: [1, 3, 5, 7]
print(odd)
odd.extend([9, 11, 13])
# Output: [1, 3, 5, 7, 9, 11, 13]
print(odd)
14. Ms.MhaskeN.R.(PPS) 14
● We can also use + operator to combine two lists.
This is also called concatenation.
Odd = [1, 3, 5]
print(odd + [9, 7, 5])
# Output: [1, 3, 5, 9, 7, 5]
● The * operator repeats a list for the given number
of times.
print(["re"] * 3)
#Output: ["re", "re", "re"]
15. Ms.MhaskeN.R.(PPS) 15
By usinng insert(index, element)
● we can insert one item at a desired location by using the method insert()
or insert multiple items by squeezing it into an empty slice of a list.
odd = [1, 9]
odd.insert(1,3)
print(odd)
# Output: [1, 3, 9]
Odd[2:2] = [5, 7]
print(odd)
# Output: [1, 3, 5, 7, 9]
16. Ms.MhaskeN.R.(PPS) 16
How to delete or remove elements from a list?
● We can delete one or more items from a list using the keyword del. It can even
delete the list entirely.
my_list = ['p','r','o','b','l','e','m']
# delete one item
del my_list[2]
# Output: ['p', 'r', 'b', 'l', 'e', 'm']
print(my_list)
# delete multiple items
del my_list[1:5]
# Output: ['p', 'm']
print(my_list)
# delete entire list
del my_list
# Error: List not defined
print(my_list)
17. Ms.MhaskeN.R.(PPS) 17
Remove(),pop()
● We can use remove() method to remove the given item or pop() method to remove an item at the given index.
● The pop() method removes and returns the last item if index is not provided. This helps us implement lists as
stacks (first in, last out data structure).
● We can also use the clear() method to empty a list.
my_list = ['p','r','o','b','l','e','m']
my_list.remove('p')
# Output: ['r', 'o', 'b', 'l', 'e', 'm']
print(my_list)
# Output: 'o'
print(my_list.pop(1))
# Output: ['r', 'b', 'l', 'e', 'm']
print(my_list)
# Output: 'm'
print(my_list.pop())
# Output: ['r', 'b', 'l', 'e']
print(my_list)
my_list.clear()
# Output: []
print(my_list)
18. Ms.MhaskeN.R.(PPS) 18
Python List Methods
● append() - Add an element to the end of the list
● extend() - Add all elements of a list to the another list
● insert() - Insert an item at the defined index
● remove() - Removes an item from the list
● pop() - Removes and returns an element at the given index
● clear() - Removes all items from the list
● index() - Returns the index of the first matched item
● count() - Returns the count of number of items passed as an
argument
● sort() - Sort items in a list in ascending order
● reverse() - Reverse the order of items in the list
● copy() - Returns a shallow copy of the list
19. Ms.MhaskeN.R.(PPS) 19
Built-in functions with List
● reduce() :apply a particular function passed in its argument to all of the list
elements stores the intermediate result and only returns the final summation value
● sum() :Sums up the numbers in the list
● ord() :Returns an integer representing the Unicode code point of the given
Unicode character
● Cmp():This function returns 1, if first list is “greater” than second list
● Max(): return maximum element of given list
● min() :return minimum element of given list
● All(): Returns true if all element are true or if list is empty
● Any(): return true if any element of the list is true. if list is empty, return false
● len():Returns length of the list or size of the list
● Enumerate() : Returns enumerate object of list
● Accumulate():apply a particular function passed in its argument to all of the list
elements returns a list containing the intermediate results
● filter() :tests if each element of a list true or not
● map() :returns a list of the results after applying the given function to each item of a
given iterable
● Lambda(): This function can have any number of arguments but only one
expression, which is evaluated and returned.
20. Ms.MhaskeN.R.(PPS) 20
Example
To accept list of N integers and partition list into two sub lists even and odd numbers
numlist = []
print("Enter total number of elements: t")
n = int(input())
for i in range(1, n+1):
print("Enter element:")
element = int(input())
numlist.append(element)
evenlist = []
Oddlist = []
for j in numlist:
if j % 2 == 0:
evenlist.append(j)
else:
oddlist.append(j)
print("Even numbers list t", evenlist)
print("Odd numbers list t", oddlist)
Output :Enter total number of elements:
5
Enter element:
11
Enter element:
22
Enter element:
33
Enter element:
44
Enter element:
55
Even numbers list [22, 44]
Odd numbers list [11, 33, 55]
22. Ms.MhaskeN.R.(PPS) 22
Tuple
● In Python, a tuple is similar to List except that the objects in tuple are
immutable which means we cannot change the elements of a tuple once
assigned. On the other hand, we can change the elements of a list.
● Tuple vs List
1. The elements of a list are mutable whereas the elements of a tuple are
immutable.
2. When we do not want to change the data over time, the tuple is a preferred
data type whereas when we need to change the data in future, list would be a
wise option.
3. Iterating over the elements of a tuple is faster compared to iterating over a list.
4. Elements of a tuple are enclosed in parenthesis whereas the elements of list
are enclosed in square bracket.
23. Ms.MhaskeN.R.(PPS) 23
to create a tuple in Python
● o create a tuple in Python, place all the elements in a () parenthesis, separated by commas. A tuple can have
heterogeneous data items, a tuple can have string and list as data items as well.
● In this example, we are creating few tuples. We can have tuple of same type of data items as well as mixed type of data
items. This example also shows nested tuple (tuples as data items in another tuple).
● Empty tuple:
# empty tuple
my_data = ()
# tuple of strings
my_data = ("hi", "hello", "bye")
print(my_data)
# tuple of int, float, string
my_data2 = (1, 2.8, "Hello World")
print(my_data2)
# tuple of string and list
my_data3 = ("Book", [1, 2, 3])
print(my_data3)
# tuples inside another tuple
# nested tuple
my_data4 = ((2, 3, 4), (1, 2, "hi"))
print(my_data4)
Output:
('hi', 'hello', 'bye')
(1, 2.8, 'Hello World')
('Book', [1, 2, 3])
((2, 3, 4), (1, 2, 'hi'))
24. Ms.MhaskeN.R.(PPS) 24
● Tuple with only single element:
● Note: When a tuple has only one element, we must put a
comma after the element, otherwise Python will not treat it
as a tuple.
# a tuple with single data item
my_data = (99,)
● If we do not put comma after 99 in the above example then
python will treat my_data as an int variable rather than a
tuple.
25. Ms.MhaskeN.R.(PPS) 25
to access tuple elements
● We use indexes to access the elements of a tuple
● Accessing tuple elements using positive indexes:
Indexes starts with 0 that is why we use 0 to access the first element of tuple, 1 to
access second element and so on.
# tuple of strings
my_data = ("hi", "hello", "bye")
# displaying all elements
print(my_data)
# accessing first element
# prints "hi"
print(my_data[0])
# accessing third element
# prints "bye"
print(my_data[2])
Output:
('hi', 'hello', 'bye')
hi
bye
26. Ms.MhaskeN.R.(PPS) 26
● Negative indexes in tuples:
Similar to list and strings we can use negative indexes to access the tuple elements from the end.
● -1 to access last element, -2 to access second last and so on.
my_data = (1, 2, "Kevin", 8.9)
# accessing last element
# prints 8.9
print(my_data[-1])
# prints 2
print(my_data[-3])
Output:
8.9
2
27. Ms.MhaskeN.R.(PPS) 27
Accessing elements from nested tuples
● Lets understand how the double indexes are used to access the elements of nested tuple. The first
index represents the element of main tuple and the second index represent the element of the
nested tuple.
● In the following example, when I used my_data[2][1], it accessed the second element of the nested
tuple. Because 2 represented the third element of main tuple which is a tuple and the 1 represented
the second element of that tuple.
my_data = (1, "Steve", (11, 22, 33))
# prints 'v'
print(my_data[1][3])
# prints 22
print(my_data[2][1])
Output:
v
22
29. Ms.MhaskeN.R.(PPS) 29
1.Changing the elements of a tuple
● We cannot change the elements of a tuple because elements of tuple are immutable. However we can change the
elements of nested items that are mutable. For example, in the following code, we are changing the element of the list
which is present inside the tuple. List items are mutable that’s why it is allowed.
my_data = (1, [9, 8, 7], "World")
print(my_data)
# changing the element of the list
# this is valid because list is mutable
my_data[1][2] = 99
print(my_data)
# changing the element of tuple
# This is not valid since tuple elements are immutable
# TypeError: 'tuple' object does not support item assignment
# my_data[0] = 101
# print(my_data)
Output:
(1, [9, 8, 7], 'World')
(1, [9, 8, 99], 'World')
30. Ms.MhaskeN.R.(PPS) 30
2 Delete operation on tuple
● We already discussed above that tuple elements are immutable which also means that we cannot delete the
elements of a tuple. However deleting entire tuple is possible.
my_data = (1, 2, 3, 4, 5, 6)
print(my_data)
# not possible
# error
# del my_data[2]
# deleting entire tuple is possible
del my_data
# not possible
# error
# because my_data is deleted
# print(my_data)
Output:
(1, 2, 3, 4, 5, 6)
31. Ms.MhaskeN.R.(PPS) 31
3.Slicing operation in tuples
my_data = (11, 22, 33, 44, 55, 66, 77, 88, 99)
print(my_data)
# elements from 3rd to 5th
# prints (33, 44, 55)
print(my_data[2:5])
# elements from start to 4th
# prints (11, 22, 33, 44)
print(my_data[:4])
# elements from 5th to end
# prints (55, 66, 77, 88, 99)
print(my_data[4:])
# elements from 5th to second last
# prints (55, 66, 77, 88)
print(my_data[4:-1])
# displaying entire tuple
print(my_data[:])
Output:
(11, 22, 33, 44, 55, 66, 77, 88, 99)
(33, 44, 55)
(11, 22, 33, 44)
(55, 66, 77, 88, 99)
(55, 66, 77, 88)
(11, 22, 33, 44, 55, 66, 77, 88, 99)
32. Ms.MhaskeN.R.(PPS) 32
4 Membership Test in Tuples
● in: Checks whether an element exists in the specified tuple.
● not in: Checks whether an element does not exist in the specified tuple.
my_data = (11, 22, 33, 44, 55, 66, 77, 88, 99)
print(my_data)
# true
print(22 in my_data)
# false
print(2 in my_data)
# false
print(88 not in my_data)
# true
print(101 not in my_data)
Output:
(11, 22, 33, 44, 55, 66, 77, 88, 99)
True
False
False
True
33. Ms.MhaskeN.R.(PPS) 33
5 Iterating a tuple
● # tuple of fruits
my_tuple = ("Apple", "Orange", "Grapes", "Banana")
# iterating over tuple elements
for fruit in my_tuple:
print(fruit)
Output:
Apple
Orange
Grapes
Banana
34. Ms.MhaskeN.R.(PPS) 34
Tuple Methods
● Methods that add items or remove items are not available with
tuple. Only the following two methods are available.
● count(x) : Returns the number of items x
● index(x) : Returns the index of the first item that is equal to x
my_tuple = ('a','p','p','l','e',)
print(my_tuple.count('p')) # Output: 2
print(my_tuple.index('l')) # Output: 3
35. Ms.MhaskeN.R.(PPS) 35
Dictionary
● A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries
are written with curly brackets, and they have keys and values.
Example
Create and print a dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Output:{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
36. Ms.MhaskeN.R.(PPS) 36
Accessing Items
You can access the items of a dictionary by referring to its key name, inside square brackets:
Example
Get the value of the "model" key:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
Output :
Mustang
37. Ms.MhaskeN.R.(PPS) 37
● There is also a method called get() that will give you the same result:
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict.get("model")
print(x)
Output:Mustang
38. Ms.MhaskeN.R.(PPS) 38
Change Values
● You can change the value of a specific item by referring to its key name:
Example
Change the "year" to 2018:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["year"] = 2018
Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 2018}
39. Ms.MhaskeN.R.(PPS) 39
Loop Through a Dictionary
● You can loop through a dictionary by using a for loop.
● When looping through a dictionary, the return value are the keys of the dictionary, but there are
methods to return the values as well.
Example
Print all key names in the dictionary, one by one:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in thisdict:
print(x)
Output: :
brand
model
year
40. Ms.MhaskeN.R.(PPS) 40
● Print all values in the dictionary, one by one:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in thisdict:
print(thisdict[x])
Output:
Ford
Mustang
1964
41. Ms.MhaskeN.R.(PPS) 41
You can also use the values() function to return values of a
dictionary:
isdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in thisdict.values():
print(x)
OUTPUT:
Ford
Mustang
1964
42. Ms.MhaskeN.R.(PPS) 42
Loop through both keys and values, by using the items()
function:
thisdict ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x, y in thisdict.items():
print(x, y)
OUTPUT:
brand Ford
model Mustang
year 1964
43. Ms.MhaskeN.R.(PPS) 43
Check if Key Exists
● To determine if a specified key is present in a dictionary use the in keyword:
Example
Check if "model" is present in the dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
OUTPUT :
Yes, 'model' is one of the keys in the thisdict dictionary
44. Ms.MhaskeN.R.(PPS) 44
Dictionary Length
● To determine how many items (key-value pairs) a dictionary has, use
the len() method.
Example
Print the number of items in the dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(len(thisdict))
OUTPUT:3
45. Ms.MhaskeN.R.(PPS) 45
Adding Items
● Adding an item to the dictionary is done by using a
new index key and assigning a value to it:
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
OUTPUT :
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}
46. Ms.MhaskeN.R.(PPS) 46
Removing Items
● There are several methods to remove items from a dictionary:
● Example
The pop() method removes the item with the specified key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
OUTPUT :
{'brand': 'Ford', 'year': 1964}
47. Ms.MhaskeN.R.(PPS) 47
● The popitem() method removes the last inserted item (in versions before 3.7, a
random item is removed instead):
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict)
OUTPUT:
{'brand': 'Ford', 'model': 'Mustang'}
48. Ms.MhaskeN.R.(PPS) 48
● The del keyword removes the item with the specified key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
OUTPUT :{'brand': 'Ford', 'year': 1964}
49. Ms.MhaskeN.R.(PPS) 49
● The del keyword can also delete the dictionary completely:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict" no longer exists.
OUTPUT :
Traceback (most recent call last):
File "demo_dictionary_del3.py", line 7, in <module>
print(thisdict) #this will cause an error because "thisdict" no longer exists.
NameError: name 'thisdict' is not defined
51. Ms.MhaskeN.R.(PPS) 51
Copy a Dictionary
●
You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only be a
reference to dict1, and changes made in dict1 will automatically also be made in dict2.
●
There are ways to make a copy, one way is to use the built-in Dictionary method copy().
Example
Make a copy of a dictionary with the copy() method:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)
OUTPUT:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
52. Ms.MhaskeN.R.(PPS) 52
Dictionary Methods
● clear() :Removes all the elements from the dictionary
● copy() :Returns a copy of the dictionary
● Fromkeys(): Returns a dictionary with the specified keys and values
● get():Returns the value of the specified key
● items():Returns a list containing the a tuple for each key value pair
● Keys(): Returns a list containing the dictionary's keys
● pop() :Removes the element with the specified key
● Popitem(): Removes the last inserted key-value pair
● setdefault():Returns the value of the specified key. If the key does not exist: insert
the key, with the specified value
● update() :Updates the dictionary with the specified key-value pairs
● values() :Returns a list of all the values in the dictionary
53. Ms.MhaskeN.R.(PPS) 53
Built-in Functions with Dictionary
● all() :Return True if all keys of the dictionary are true (or if
the dictionary is empty).
● any() :Return True if any key of the dictionary is true. If the
dictionary is empty, return False.
● len() :Return the length (the number of items) in the
dictionary.
● cmp() :Compares items of two dictionaries.
● sorted() :Return a new sorted list of keys in the dictionary.