Dictionary and Sets Session 6
Dictionary
Dictionary is a collection of key-value pairs.
Syntax:
Properties of Python Dictionaries
- It is unordered
- It is mutable
- It is indexed
- It cannot contain duplicate keys
Dictionary Methods
Consider the following dictionary,
- items() : returns a list of (key,value) tuple.
- keys() : returns a list containing dictionary’s keys.
- update({“friend”: “Iqra”}) : updates the dictionary with supplied key-value pairs.
- get(“name”) : returns the value of the specified keys (and value is returned e.g., “Iqra” is returned here)
More methods are available on docs.python.org
Sets in Python
Set is a collection of non-repetitive elements.
If you are a programming beginner without much knowledge of mathematical operations on sets, you can simply look at sets in python as data types containing unique values.
Properties of Sets
- Sets are unordered # Elements order doesn’t matter
- Sets are unindexed # Cannot access elements by index
- There is no way to change items in sets
- Sets cannot contain duplicate values
Operations on Sets
Consider the following set:
- Len(s) : Returns 4, the length of the set
- remove(8) : Updates the set S and removes 8 from S
- pop() : Removes an arbitrary element from the set and returns the element removed.
- clear() : Empties the set S
- union({8, 11}) : Returns a new set with all items from both sets. #{1,8,2,3,11}
- intersection({8, 11}) : Returns a set which contains only items in both sets. #{8}
Conditional Expressions
Sometimes we want to play pubg on our phone if the day is Sunday.
Sometimes we order Ice-cream online if the day is sunny.
Sometimes we go hiking if our parents allow.
All these are decisions that depend on the condition being met.
In python programming too, we must be able to execute instructions on a condition(s) being met. This is what conditions are for!
If else and elif in Python
If else and elif statements are a multiway decision taken by our program due to certain conditions in our code.
Syntax:
Code example:
Quick Quiz: Write a program to print yes when the age entered by the user is greater than or equal to 18.
Relational Operators
Relational operators are used to evaluate conditions inside if statements. Some examples of relational operators are:
Logical Operators
In python, logical operators operate on conditional statements. Example:
elif clause
elif in python means [else if]. If statement can be chained together with a lot of these elif statements followed by an else statement.
- The above ladder will stop once a condition in an if or elif is met.
Important Notes:
- There can be any number of elif statements.
- Last else is executed only if all the conditions inside elifs fail.
Practice Set
- Write a program to create a dictionary of urdu words with values as their English translation. Provide the user with an option to look it up!
- Write a program to input eight numbers from the user and display all the unique numbers (once).
- Can we have a set with 18(int) and “18”(str) as a value in it?
- What will be the length of the following set S:
- S = {}, what is the type of S?
- Create an empty dictionary. Allow 4 friends to enter their favorite language as values and use keys as their names. Assume that the names are unique.
- If the names of 2 friends are the same; what will happen to the program in Program 6?
- If the languages of two friends are the same; what will happen to the program in Program 6?
- Can you change the values inside a list which is contained in set S
What will be the length of S after the above operations?
10. A spam comment is defined as a text containing the following keywords:
“make a lot of money”, “buy now”, “subscribe this”, “click this”. Write a program to detect these spams.
11.Write a program to find whether a given username contains less than 10 characters or not.
12. Write a program that finds out whether a given name is present in a list or not. 13. Write a program to calculate the grade of a student from his marks from the following scheme:
90-100 | Ex |
80-90 | A |
70-80 | B |
60-70 | C |
50-60 | D |
<50 | F |
Comments
Post a Comment