Leetcode with dani – Telegram
Leetcode with dani
1.31K subscribers
196 photos
14 videos
56 files
240 links
Join us and let's tackle leet code questions together: improve your problem-solving skills
Preparing for coding interviews
learning new algorithms and data structures
connect with other coding enthusiasts
Download Telegram
2. What is the output of the following code?

numbers = [1, 2, 3, 4, 5] numbers[1] = 10 print(numbers)
Anonymous Quiz
7%
[1, 2, 3, 4, 5]
24%
[10, 2, 3, 4, 5]
18%
error
50%
[1, 10, 3, 4, 5]
👍5🔥1
Leetcode with dani
1. What is the output of the following code?

numbers = [1, 2, 3, 4, 5] print(numbers[2])
in Python, indexing starts from zero.the first element in a list has an index of 0, the second element has an index of 1, and so on
❤‍🔥2
try to understand the above idea on list before answer this question ,it's alittel bit confusing 3. What is the output of the following code?

numbers = [1, 2, 3, 4, 5] sliced_numbers = numbers[1:4] print(sliced_numbers)
Anonymous Quiz
40%
[ 2, 3, 4]
25%
[ 2, 3, 4, 5]
30%
[ 2, 3, 4 ]
5%
[1, 2, 3, 4, 5]
👍61
4.What is the output of the following code?

fruits = ["apple", "banana", "orange"] fruits.append("mango") print(len(fruits))
Anonymous Quiz
4%
["apple", "banana", "orange"]
24%
["apple", "banana", "orange","mango"]
63%
4
6%
3
3%
["mango"]
👍4
colors = ['red', 'green', 'blue'], what is the output of colors[-1]?
Anonymous Quiz
4%
'red'
8%
'green'
64%
'blue'
25%
error
🤔62👍2🔥1
The output of sum([10, 20, 30, 40, 50]) is
Anonymous Quiz
21%
none
57%
150
22%
([10, 20, 30, 40, 50])
👍1
#code_challenge write a code that Check whether a list is empty or not
👍1
What will be the output of the following code?
my_list = [1, 0, -1, 4, '1']
if my_list[0] == my_list[-1]: print("The first and last elements are the same.") else: print("The first and last elements are different.") Output:
Anonymous Quiz
22%
The first and last elements are the same
10%
error
69%
The first and last elements are different
8👍2👎2
here is the code that check whether the list is empty or not. my_list = []
if len(my_list) == 0:
print("The list is empty.") else: print("The list is not empty.")
Anonymous Quiz
79%
The list is empty.
13%
The list is not empty.
8%
{}
🆒7
TUPLES IN PYTHON Tuples are an immutable data structure in Python, similar to lists. They are used to store a collection of related values that should not be changed. Tuples are often used to represent a group of items that belong together, such as coordinates, RGB color values, or database records.

Creating a Tuple:
To create a tuple, you can enclose a comma-separated sequence of items within parentheses (). For example:

my_tuple = (1, 2, 3, "apple", "banana")


Alternatively, you can create a tuple without using parentheses by separating the values with commas:

my_tuple = 1, 2, 3, "apple", "banana"


Accessing Elements:
You can access individual elements in a tuple using their index, just like with lists. The index starts from 0 for the first element, 1 for the second element, and so on.

print(my_tuple[0])  # Output: 1
print(my_tuple[3])  # Output: "apple"


Tuples are immutable, which means you cannot modify their elements or assign new values to them. If you try to modify a tuple, you will get a TypeError.

my_tuple[0] = 10  # This will raise a TypeError


Tuple Packing and Unpacking:
You can create a tuple by simply separating values with commas, without using parentheses. This is called tuple packing.

my_tuple = 1, 2, 3


You can also assign the values of a tuple to multiple variables in a single line. This is called tuple unpacking.

a, b, c = my_tuple
print(a)  # Output: 1
print(b)  # Output: 2
print(c)  # Output: 3


Tuple Methods:
Tuples have fewer built-in methods compared to lists, but there are a few useful ones:

my_tuple = (1, 2, 3, 4, 5)

# Get the index of a specific element
index = my_tuple.index(3)
print(index)  # Output: 2

# Count the number of occurrences of an element
count = my_tuple.count(4)
print(count)  # Output: 1


Tuples are commonly used when you want to store a collection of values that should not be modified.
👍3👌3🆒3
Can you modify the elements of a tuple once it is defined?
Anonymous Quiz
36%
yes
64%
no
👏4👍1
Leetcode with dani pinned «TUPLES IN PYTHON Tuples are an immutable data structure in Python, similar to lists. They are used to store a collection of related…»
1. Code:
my_tuple = (1, 2, 3, "apple", "banana")
print(my_tuple[-1]) Question: What will be the output of the above code?
Anonymous Quiz
4%
1
6%
2
4%
3
4%
apple
83%
banana
👍3
3. Code:
my_tuple = (1, 2, 3, "apple", "banana")
print(my_tuple.index("banana")) Question: What will be the output of the above code?
Anonymous Quiz
9%
3
74%
4
17%
5
👍3
ስለ ቻናላችን ሚሰጡት ሃሳብ ወይም ይሻሻል ሚሉትን ነገር ካለ በዚህ @zprogramming_bot ያሳውቁን
4.Code:
my_tuple = (1, 2, 3, "apple", "banana")
print(len(my_tuple)) Question: What will be the output of the above code?
Anonymous Quiz
92%
5
4%
6
4%
3
👏3
4. Code:
my_tuple = (1, 2, 3, 4, 5)
my_tuple.append(6,) print(my_tuple) Question: What will be the output of the above code?
Anonymous Quiz
45%
(1, 2, 3, 4, 5,6)
48%
error
8%
(1, 2, 3, 4, 5)
💯4🍾3🤝2👍1
Once a tuple is created, you cannot add, remove, or change elements within it. However, there are a few workarounds if you need to modify the contents of a tuple:

1. Convert the tuple to a list, make the necessary modifications, and then convert it back to a tuple:
my_tuple = (1, 2, 3, 4, 5)
my_list = list(my_tuple)
my_list.append(6)
my_tuple = tuple(my_list)
print(my_tuple)

Output: (1, 2, 3, 4, 5, 6)

2. Use tuple concatenation to create a new tuple with the desired modifications:
my_tuple = (1, 2, 3, 4, 5)
new_tuple = my_tuple + (6,)
print(new_tuple)

Output: (1, 2, 3, 4, 5, 6)

Remember that both of these methods create a new tuple rather than modifying the original tuple.
👍81