0% found this document useful (0 votes)
25 views9 pages

Session 4

The document discusses Python data structures like lists, dictionaries, and sets. It shows how to create, access, update, and delete elements in these structures. It also covers control flow statements like if/else, if/elif/else chains, and while loops.

Uploaded by

detomal301
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)
25 views9 pages

Session 4

The document discusses Python data structures like lists, dictionaries, and sets. It shows how to create, access, update, and delete elements in these structures. It also covers control flow statements like if/else, if/elif/else chains, and while loops.

Uploaded by

detomal301
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/ 9

In [ ]:

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)

{'Suresh': 98, 'Mohan': 76}

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 ".")

Raju's favorite language is Python.

In [ ]:

1 "c" in favorite_languages
Out[93]:

False

Sets
Another data structure is set , which represents a collection of distinct elements:

SET

unindexed, unordered, mutable

Creating, Accessing, Updating and Deleting

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}

ADD ELEMENT IN SET

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_list = ["a","an","at"] + hundreds_of_other_words + ["ye


t", "you"]
"zip" in stopwords_list # False, but have to check every element

stopwords_set = set(stopwords_list)
"zip" in stopwords_set # very fast to check

The second reason is to find the distinct items in a collection:


In [ ]:

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 [ ]:

1 age = int(input("Enter your age: "))


2 if age >= 18:
3 print("You are old enough to vote!")
Enter your age: 18
You are old enough to vote!

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?

The if-elif-else Chain


if (condition):
statement
elif (condition):
statement
.
.
else:
statement

Many real-world situations involve more than two possible conditions.


For example, consider an amusement park that charges different rates for different age groups:

Admission for anyone under age 4 is free.


Admission for anyone between the ages of 4 and 18 is $5.
Admission for anyone age 18 or older is $10.

In [ ]:

1 # The if-elif-else Chain


2 age = int(input("Enter your age: "))
3 if age < 4:
4 print("Your admission cost is $0.")
5 elif age < 18:
6 print("Your admission cost is $5.")
7 else:
8 print("Your admission cost is $10.")

Enter your age: 23


Your admission cost is $10.
In [ ]:

1 age = int(input("Enter your age: "))


2 if age < 4:
3 price = 0
4 elif age < 18:
5 price = 5
6 else:
7 price = 10
8 print("Your admission cost is $" + str(price) + ".")

Enter your age: 12


Your admission cost is $5.

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 [ ]:

1 prompt = "\nTell me something, and I will repeat it back to you:"


2 prompt += "\nEnter 'quit' to end the program. "
3 message = ""
4 while message != 'quit':
5 message = input(prompt)
6 print(message)

You might also like