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
2. what is the output ? num = 10
while num >= 0:
print(num) num -= 2 or we can say num = num -2
while num >= 0:
print(num) num -= 2 or we can say num = num -2
Anonymous Quiz
5%
10 9 8 7 6 5 4 3 2 1
23%
10 8 6 4 2
72%
10 8 6 4 2 0
#Day_note The modulo operator, denoted by the symbol
For example, if we have the expression
Here are a few examples to illustrate the usage of the modulo operator:
1.
2.
3.
%, is a mathematical operator that returns the remainder of a division operation. It is used to determine the remainder when one number is divided by another.For example, if we have the expression
a % b, the modulo operator will divide a by b and return the remainder.Here are a few examples to illustrate the usage of the modulo operator:
1.
10 % 3 returns 1 because when 10 is divided by 3, the remainder is 1.2.
15 % 4 returns 3 because when 15 is divided by 4, the remainder is 3.3.
7 % 2 returns 1 because when 7 is divided by 2, the remainder is 1.👍7
Write a program that takes an input from the user and determines whether the number is even or odd. The program should display a message indicating the result.
Your program should perform these tasks: 1. take input from the user and say even or odd and take input from the user again...
hint Use the modulo operator and while loop
Your program should perform these tasks: 1. take input from the user and say even or odd and take input from the user again...
hint Use the modulo operator and while loop
👍1
Leetcode with dani pinned «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…»
Leetcode with dani
Write a program that takes an input from the user and determines whether the number is even or odd. The program should display a message indicating the result. Your program should perform these tasks: 1. take input from the user and say even or…
while True:
number = int(input("Enter a number: "))
if number % 2 == 0:
print(number, "is even.")
else:
print(number, "is odd.")❤5⚡1
1. What is the output of the following code snippet?
x = 5
while x >= 0: print(x) x -= 1
x = 5
while x >= 0: print(x) x -= 1
Anonymous Quiz
20%
5 4 3 2 1
6%
1 2 3 4 5
61%
5 4 3 2 1 0
13%
0 1 2 3 4 5
❤🔥1
1. What is the output of the following code snippet?
x = 10
while x > 0: if x % 2 == 0: print(x) x -= 1
x = 10
while x > 0: if x % 2 == 0: print(x) x -= 1
Anonymous Quiz
73%
10 8 6 4 2
9%
2 4 6 8 10
10%
10 9 8 7 6 5 4 3 2 1
8%
1 3 5 7 9
3. What is the output of the following code snippet?
count = 0
while count < 5: count += 1 if count == 3: break print(count)
count = 0
while count < 5: count += 1 if count == 3: break print(count)
Anonymous Quiz
8%
a) 1 2 3 4 5
16%
b) 1 2 4 5
12%
c) 1 2 3 4
63%
d) 1 2
❤3
4. What is the output of the following code snippet?
x = 1
while x <= 5: print(x) x += 2
x = 1
while x <= 5: print(x) x += 2
Anonymous Quiz
10%
a) 1 2 3 4 5
77%
b) 1 3 5
7%
c) 2 4 6
6%
d) 1 2 4