👍5
You can check if a key exists in a dictionary by using the
my_dict = {"dani": 3, "yohanes": 5}
print("eyob" in my_dict) # Output: False
in keyword. It returns True if the key is present, and False otherwise. For example:my_dict = {"dani": 3, "yohanes": 5}
print("eyob" in my_dict) # Output: False
my_dict = {"dani": 3, "eyob": 5, "bisrat": 2}
1.print(my_dict["bisrat"])
2.print("yohannes" in my_dict)
3.my_dict["dani"] = 4
4.print(len(my_dict))
5.del my_dict["eyob"]
Now, based on the code, can you answer the following questions about the dictionary?⚡1👍1
👍2
3. what is the use of the no.3 code
Anonymous Quiz
14%
none
79%
to change the key of the value 'dani' in the dictionary
7%
to change 'dani' to 3
☃3
5. what will be the output of the following code after the no.5 code run? print(my_dict)
Anonymous Quiz
9%
{"dani": 3, "eyob": 5, "bisrat": 2}
86%
{"dani": 3, "bisrat": 2}
5%
my_dict
Leetcode with dani
Can a dictionary have multiple values for the same key?
dictionary in Python cannot have multiple values for the same key. Each key in a dictionary must be unique, and it can only map to a single value.
If you try to assign a new value to an existing key in a dictionary, it will overwrite the previous value associated with that key. Here's an example:
If you try to assign a new value to an existing key in a dictionary, it will overwrite the previous value associated with that key. Here's an example:
my_dict = {"dani": 3, "abebe": 5, "yonas": 2}
my_dict["dani"] = 7 my_dict["kebe"] = 9
print(my_dict)
Output:{'dani': 7, 'abebe': 5, 'yonas': 2, 'kebe': 9}
As you can see, the value associated with the key "dani" has been updated from 3 to 7. The dictionary does not store multiple values for the same key.👍7❤1
Set in python
A set is an unordered collection of unique elements in Python. It is denoted by curly braces ({}) or by using the
Creating a set:
Adding elements to a set:
Removing elements from a set:
Set operations:
Sets are mutable, meaning you can modify them by adding or removing elements.
A set is an unordered collection of unique elements in Python. It is denoted by curly braces ({}) or by using the
set() function. Sets are useful when you want to store a collection of items without any duplicates and perform operations like union, intersection, and difference.Creating a set:
# Creating an empty set
my_set = set()
print(my_set) # Output: set()
# Creating a set with initial values
my_set = {1, 2, 3, 4, 5}
print(my_set) # Output: {1, 2, 3, 4, 5}
Adding elements to a set:
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}
# Adding multiple elements using update()
my_set.update([5, 6, 7])
print(my_set) # Output: {1, 2, 3, 4, 5, 6, 7}
Removing elements from a set:
my_set = {1, 2, 3, 4, 5}
my_set.remove(3)
print(my_set) # Output: {1, 2, 4, 5}
# Removing an element that doesn't exist will raise an error
my_set.remove(6) # Raises KeyError
# Alternatively, you can use discard() to avoid errors
my_set.discard(6) # No error raised
Set operations:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
# Union of two sets
union_set = set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4, 5}
# Intersection of two sets
intersection_set = set1.intersection(set2)
print(intersection_set) # Output: {3}
# Difference between two sets
difference_set = set1.difference(set2)
print(difference_set) # Output: {1, 2}
Sets are mutable, meaning you can modify them by adding or removing elements.
👍5
The similarities and differences between list, tuple, and set
1. Lists:
- Lists are ordered collections of items that can be modified (mutable).
- Elements in a list are enclosed in square brackets ().
- Lists allow duplicate elements.
- Elements in a list can be accessed using indexing and slicing.
- Lists are commonly used when you need to store and manipulate a collection of items that may change over time.
Example:
2. Tuples:
- Tuples are ordered collections of items that cannot be modified (immutable).
- Elements in a tuple are enclosed in parentheses ().
- Tuples allow duplicate elements.
- Elements in a tuple can be accessed using indexing and slicing, similar to lists.
- Tuples are commonly used when you want to store a collection of items that should not be changed, such as coordinates or database records.
Example:
3. Sets:
- Sets are unordered collections of unique elements.
- Elements in a set are enclosed in curly braces ({}) or created using the
- Sets do not allow duplicate elements. If you try to add a duplicate element, it will be ignored.
- Elements in a set cannot be accessed using indexing or slicing since they are unordered.
- Sets are commonly used when you need to store a collection of unique items and perform operations like union, intersection, and difference.
Example:
In summary, lists are mutable and allow duplicates, tuples are immutable and allow duplicates, while sets are mutable, unordered, and do not allow duplicates. The choice between them depends on the specific requirements of your program.
1. Lists:
- Lists are ordered collections of items that can be modified (mutable).
- Elements in a list are enclosed in square brackets ().
- Lists allow duplicate elements.
- Elements in a list can be accessed using indexing and slicing.
- Lists are commonly used when you need to store and manipulate a collection of items that may change over time.
Example:
my_list = [1, 2, 3, 4, 5]
2. Tuples:
- Tuples are ordered collections of items that cannot be modified (immutable).
- Elements in a tuple are enclosed in parentheses ().
- Tuples allow duplicate elements.
- Elements in a tuple can be accessed using indexing and slicing, similar to lists.
- Tuples are commonly used when you want to store a collection of items that should not be changed, such as coordinates or database records.
Example:
my_tuple = (1, 2, 3, 4, 5)
3. Sets:
- Sets are unordered collections of unique elements.
- Elements in a set are enclosed in curly braces ({}) or created using the
set() function.- Sets do not allow duplicate elements. If you try to add a duplicate element, it will be ignored.
- Elements in a set cannot be accessed using indexing or slicing since they are unordered.
- Sets are commonly used when you need to store a collection of unique items and perform operations like union, intersection, and difference.
Example:
my_set = {1, 2, 3, 4, 5}
In summary, lists are mutable and allow duplicates, tuples are immutable and allow duplicates, while sets are mutable, unordered, and do not allow duplicates. The choice between them depends on the specific requirements of your program.
👍6✍1🆒1
1. Which of the following collection types in Python is mutable?
Anonymous Quiz
23%
List
10%
Tuple
8%
Set
59%
A and C
2. What is the output of the following code snippet?
my_set = {1, 2, 3} my_set.add([4, 5]) print(my_set)
my_set = {1, 2, 3} my_set.add([4, 5]) print(my_set)
Anonymous Quiz
46%
{1, 2, 3, [4, 5]}
9%
{1, 2, 3, (4, 5)}
24%
TypeError: unhashable type: 'list'
21%
TypeError: unhashable type: 'set'
👍5
3. What is the output of the following code snippet?
set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1 ^ set2 print(result)
set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1 ^ set2 print(result)
Anonymous Quiz
8%
{1, 2}
4%
{2, 4, 5}
73%
{1, 2, 3, 4, 5}
15%
{1, 2, 4, 5}
👍1👏1
4. How do you check if an element exists in a list called my_list in Python?
Anonymous Quiz
36%
5 in my_list
17%
my_list.contains(5)
18%
my_list.exists(5)
29%
my_list.check(5)
🔥2👍1
5. What happens when you try to add a duplicate element to a set in Python?
Anonymous Quiz
17%
The duplicate element is added to the set.
62%
The duplicate element is ignored and not added to the set.
19%
An error is raised.
2%
The set is cleared and the duplicate element is added.
👍1
6. Which of the following methods is used to clear all elements from a set in Python?
Anonymous Quiz
44%
clear()
23%
remove_all()
22%
delete_all()
11%
discard_all()
🌚2
What is a while loop in Python? While loop is a control flow statement that allows you to repeatedly execute a block of code as long as a certain condition is true. It is useful when you want to repeat a specific task until a certain condition is met.
The basic syntax of a while loop in Python is as follows:
Here's a breakdown of how a while loop works:
1. The condition is evaluated. If the condition is true, the code inside the loop is executed. If the condition is false, the loop is skipped, and the program continues with the next statement after the loop.
2. After executing the code inside the loop, the condition is evaluated again. If it is still true, the loop continues to execute. This process repeats until the condition becomes false.
It's important to ensure that the condition within the while loop eventually becomes false; otherwise, the loop will continue indefinitely, resulting in an infinite loop.
Here's an example to illustrate the usage of a while loop:
In this example, the loop will execute as long as the value of
Remember to be cautious when using while loops to avoid infinite loops. It's important to ensure that the condition will eventually become false to prevent your program from running indefinitely.
The basic syntax of a while loop in Python is as follows:
while condition:
# code to be executed
Here's a breakdown of how a while loop works:
1. The condition is evaluated. If the condition is true, the code inside the loop is executed. If the condition is false, the loop is skipped, and the program continues with the next statement after the loop.
2. After executing the code inside the loop, the condition is evaluated again. If it is still true, the loop continues to execute. This process repeats until the condition becomes false.
It's important to ensure that the condition within the while loop eventually becomes false; otherwise, the loop will continue indefinitely, resulting in an infinite loop.
Here's an example to illustrate the usage of a while loop:
count = 0
while count < 5:
print("Count:", count)
count = count + 1
print("Loop finished")
In this example, the loop will execute as long as the value of
count is less than 5. The code inside the loop will print the current value of count and then increment it by 1. Once count reaches 5, the condition becomes false, and the loop terminates. The program then continues with the statement after the loop, which prints "Loop finished".Remember to be cautious when using while loops to avoid infinite loops. It's important to ensure that the condition will eventually become false to prevent your program from running indefinitely.
examples of while loops:
1. Counting from 1 to 5:
This loop will print the numbers from 1 to 5. The variable
2. Printing even numbers:
This loop will print even numbers from 2 to 10. The variable
3. Repeating a message a certain number of times:
This loop will print the message "Hello!" three times. The variable
1. Counting from 1 to 5:
count = 1
while count <= 5:
print(count)
count = count + 1
This loop will print the numbers from 1 to 5. The variable
count is initially set to 1, and the loop continues as long as count is less than or equal to 5. Inside the loop, we print the value of count and then increment it by 1 using count = count + 1 or we can use count += 1.2. Printing even numbers:
num = 2
while num <= 10:
print(num)
num += 2
This loop will print even numbers from 2 to 10. The variable
num is initially set to 2, and the loop continues as long as num is less than or equal to 10. Inside the loop, we print the value of num and then increment it by 2 using num += 2.3. Repeating a message a certain number of times:
count = 0
while count < 3:
print("Hello!")
count += 1
This loop will print the message "Hello!" three times. The variable
count is initially set to 0, and the loop continues as long as count is less than 3. Inside the loop, we print the message and then increment count by 1.👍4
If you don't include the
For example, let's consider the following code without the increment statement:
In this case, the loop will print the value of
An infinite loop can cause your program to hang or become unresponsive, as it will keep executing the loop indefinitely without progressing further in the code.
To avoid infinite loops, it's important to include an increment statement (or any other appropriate modification) inside the loop, so that the loop control variable or condition can eventually change and lead to the termination of the loop.
So, in the case of the previous example, including
Always remember to include an appropriate modification statement inside the loop to prevent infinite loops and ensure that your program executes as intended.
count += 1 (or any other similar increment statement) in the loop, the loop will continue indefinitely, resulting in an infinite loop.For example, let's consider the following code without the increment statement:
count = 1
while count <= 5:
print(count)
In this case, the loop will print the value of
count (which is initially 1) and then check the condition count <= 5. Since the condition is true, it will print the value of count again. However, without the increment statement, the value of count will never change, and the loop will keep printing the same value infinitely.An infinite loop can cause your program to hang or become unresponsive, as it will keep executing the loop indefinitely without progressing further in the code.
To avoid infinite loops, it's important to include an increment statement (or any other appropriate modification) inside the loop, so that the loop control variable or condition can eventually change and lead to the termination of the loop.
So, in the case of the previous example, including
count += 1 ensures that the value of count is incremented by 1 in each iteration, allowing the loop to eventually reach a point where the condition count <= 5 becomes false and the loop terminates.Always remember to include an appropriate modification statement inside the loop to prevent infinite loops and ensure that your program executes as intended.
what is the output ? count = 0
while count < 5:
print(count) count += 1
while count < 5:
print(count) count += 1
Anonymous Quiz
10%
1 2 3 4 5
14%
0 1 2 3 4 5
75%
0 1 2 3 4
👏4