Session 4
Session 4
1 print(len(grades))
3
In [ ]:
1 # modifying grades
2 grades["Suresh"] = 98
3 print(grades)
{'Suresh': 98, 'Seema': 95, 'Mohan': 76}
In [ ]:
1 # removing item
2 del grades["Seema"]
3 print(grades)
In [ ]:
1 favorite_languages = {
2 'Raju': 'python',
3 'Rani': 'c',
4 'Suresh': 'ruby',
5 'Seema': 'python',
6 }
7
8 print("Raju's favorite language is " +
9 favorite_languages['Raju'].title() +
10 ".")
In [ ]:
1 "c" in favorite_languages
Out[93]:
False
Sets
Another data structure is set , which represents a collection of distinct elements:
SET
CREATING A SET
In [ ]:
1 s = {1,3,5}
ACCESSING SET
In [ ]:
1 print(s)
{1, 3, 5}
In [ ]:
1 s1 = set()
In [ ]:
1 print(s1)
set()
In [ ]:
1 s2 = set([1,2,3])
In [ ]:
1 print(s2)
{1, 2, 3}
In [ ]:
1 s.add(6)
In [ ]:
1 print(s)
{1, 3, 5, 6}
TYPE
In [ ]:
1 s={1,3,5}
In [ ]:
1 print(type(s))
<class 'set'>
Duplicate Value
In [ ]:
1 s={1,2,3,4,4}
In [ ]:
1 print(s)
{1, 2, 3, 4}
UPDATEING
In [ ]:
1 s.update([8,9,10])
In [ ]:
1 print(s)
{1, 3, 5, 8, 9, 10}
DELETING ELEMENT
In [ ]:
1 s.discard(3)
In [ ]:
1 print(s)
{1, 5, 8, 9, 10}
In [ ]:
1 s.remove(5)
In [ ]:
1 print(s)
{1, 8, 9, 10}
In [ ]:
1 s.clear()
In [ ]:
1 print(s)
set()
In [ ]:
1 s={1,3,5}
In [ ]:
1 print(s)
{1, 3, 5}
MEMBERSHIP TEST
In [ ]:
1 print(3 in s)
True
In [ ]:
1 print(6 not in s)
False
COPYING SET
In [ ]:
1 s_copy=s.copy()
In [ ]:
1 print(s_copy)
{1, 3, 5}
FUNCTIONS
In [ ]:
1 s={1,3,5}
In [ ]:
1 s2={1,3,6}
In [ ]:
1 print(s&s2)
{1, 3}
In [ ]:
1 print (s.intersection(s2))
{1, 3}
In [ ]:
1 print(s|s2)
{1, 3, 5, 6}
In [ ]:
1 print (s.union(s2))
{1, 3, 5, 6}
In [ ]:
1 print(len(s))
In [ ]:
1 print(max(s))
In [ ]:
1 print(min(s))
1
In [ ]:
1 print(sum(s))
9
In [ ]:
1 s={1,2,0}
In [ ]:
1 print(s)
In [ ]:
1 print(all(s))
False
In [ ]:
1 s11={0,0,0,0,0,0}
In [ ]:
1 print(any(s11))
False
In [ ]:
1 s={2,3,'ABHISHEK'}
In [ ]:
1 print(s)
{'ABHISHEK', 2, 3}
In [ ]:
1 se={'abhishek', 'ABHISHEK'}
In [ ]:
1 print(max(se))
abhishek
Reason to use sets: The first is that in is a very fast operation on sets.
If we have a large collection of items that we want
to use for a membership test, a set is more appropriate
than a list:
stopwords_set = set(stopwords_list)
"zip" in stopwords_set # very fast to check
1 item_list = [1, 2, 3, 1, 2, 3]
2 num_items = len(item_list)
3 item_set = set(item_list)
4 num_distinct_items = len(item_set)
5 distinct_item_list = list(item_set)
6
7 print(item_list)
8 print(num_items)
9 print(item_set)
10 print(num_distinct_items)
11 print(distinct_item_list)
Control Flow
It is the order in which individual statements, instructions or function calls
The if statement
if condition:
Statement 1
Statement 2
In [ ]:
1 # if statement
2 age = int(input("Enter your age: "))
3 if age >= 18:
4 print("You are old enough to vote!")
Enter your age: 12
In [ ]:
if-else
if (condition):
Statement 1
Statement 2
else:
Statement 3
Statement 4
In [ ]:
1 # if-else
2 age = int(input("Enter your age: "))
3 if age >= 18:
4 print("You are old enough to vote!")
5 print("Have you registered to vote yet?")
6 else:
7 print("Sorry, you are too young to vote.")
8 print("Please register to vote as soon as you turn 18!")
Enter your age: 24
You are old enough to vote!
Have you registered to vote yet?
In [ ]:
While Loop
With the while loop we can execute a set of statements as long as a condition is true.
while condition:
statements
In [ ]:
1 # while loop
2 current_number = 1
3 while current_number <= 5:
4 print(current_number)
5 current_number += 1 # a += 1 ---> a = a+1
6
7 print()
8 print(current_number)
In [ ]:
1 # while loop
2 number = int(input("Enter any number: "))
3 loop_number = 1
4 while loop_number <= 10:
5 mul = number * loop_number
6 print(str(number) + " X " + str(loop_number) + " = " + str(mul))
7 loop_number += 1
In [ ]: