Leetcode with dani – Telegram
Leetcode with dani
1.31K subscribers
197 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
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
Here are some questions related to data types, input, lists, and tuples to assess your understanding:

1. Data Types:
   a. What are the different data types available in Python
   b. Give examples of integer, string, float and boolean types in Python.
   C. How do you convert one data type to another? Provide examples.

2. Input:
   a. How do you take user input in Python?
  

3. Lists:
   a. What is a list in Python? How is it different from other data types?
   b. How do you create an empty list and initialize a list with values?
   c. Explain the concept of indexing and slicing in lists.
   d. How do you add, remove, or modify elements in a list?
   e. What are some built-in methods available for lists? Provide examples.

4. Tuples:
   a. What is a tuple in Python? How is it different from a list?
   b. How do you create a tuple? Can you modify a tuple once it is created?
   c. Explain the concept of unpacking a tuple. Provide an example.
   d. How do you convert a list to a tuple and vice versa?

These questions cover the basics of data types, input, lists, and tuples in Python. I want this to test your understanding and identify areas where you may need further clarification.
👍5
Touch the profile and touch 3 points on the right and join the discussion group
When you convert a tuple to a list or vice versa, a new object of the desired data type is created. The original tuple or list remains unchanged.

For example, let's consider converting a tuple to a list:

my_tuple = (1, 2, 3)
my_list = list(my_tuple)


In this case, my_tuple remains unchanged as a tuple (1, 2, 3), and a new list my_list is created with the values [1, 2, 3].

Similarly, when converting a list to a tuple:

my_list = [1, 2, 3]
my_tuple = tuple(my_list)


Here, my_list remains unchanged as a list [1, 2, 3], and a new tuple my_tuple is created with the values (1, 2, 3).

It's important to note that the conversion functions list() and tuple() create new objects of the desired data type, and the original object is not modified.
👍4
After lists and tuples, the next concept in Python is usually dictionaries.

Dictionaries are unordered collections of key-value pairs. They are mutable, meaning you can add, remove, and modify elements within them. Each element in a dictionary is accessed by its key rather than its index, which allows for efficient retrieval of values.

Here's an example of a dictionary in Python:

my_dict = {"name": "John", "age": 25, "city": "New York"}


In this example, "name", "age", and "city" are the keys, and "John", 25, and "New York" are the corresponding values.

Dictionaries are commonly used for tasks such as storing and retrieving data, mapping values, and representing real-world objects or entities. They provide a flexible and powerful way to organize and manipulate data in Python.
👍3
To get the value of a specific key in a dictionary, you can use the key as an index. Here's an example:

my_dict = {"name": "John", "age": 25, "city": "New York"}

name_value = my_dict["name"]
print(name_value)  # Output: John

age_value = my_dict["age"]
print(age_value)  # Output: 25

city_value = my_dict["city"]
print(city_value)  # Output: New York


In this example, we access the values of the keys "name", "age", and "city" by using them as indices in square brackets. The corresponding values are then assigned to the variables name_value, age_value, and city_value, respectively.

If the key does not exist in the dictionary, a KeyError will be raised. To avoid this, you can use the get() method, which allows you to provide a default value if the key is not found:

my_dict = {"name": "John", "age": 25, "city": "New York"}

name_value = my_dict.get("name", "Unknown")
print(name_value)  # Output: John

country_value = my_dict.get("country", "Unknown")
print(country_value)  # Output: Unknown


In this case, if the key "name" is found, its corresponding value is returned. If the key "country" is not found, the default value "Unknown" is returned instead of raising an error.
👍21
esti channelun ke10 rate yestu
Anonymous Poll
55%
10+
19%
10
13%
9
12%
8
3%
7
3%
6
7%
5 and below
❤‍🔥8👍1
In Python, dictionaries are mutable data structures that allow you to store key-value pairs. One of the advantages of dictionaries is the ability to add and remove items dynamically.

To add an item to a dictionary, you can simply assign a value to a new or existing key. For example:

my_dict = {"apple": 3, "banana": 5}
my_dict["orange"] = 2


In this example, we added a new key-value pair "orange": 2 to the dictionary my_dict. If the key already exists, the value will be updated; otherwise, a new key-value pair will be created.

To remove an item from a dictionary, you can use the del keyword followed by the key you want to remove. For example:

my_dict = {"apple": 3, "banana": 5, "orange": 2}
del my_dict["banana"]


In this example, we removed the key-value pair "banana": 5 from the dictionary my_dict using the del keyword.

It's important to note that if you try to remove a key that doesn't exist in the dictionary, a KeyError will be raised. To avoid this, you can use the dict.pop() method, which removes the item with the specified key and returns its value. For example:

my_dict = {"apple": 3, "banana": 5, "orange": 2}
removed_value = my_dict.pop("banana")


In this example, the key-value pair "banana": 5 is removed from the dictionary my_dict, and the value 5 is assigned to the variable removed_value.

Remember, dictionaries in Python are unordered, so the order of the items may not be the same as the order in which they were added.
👍32
Leetcode with dani pinned «esti channelun ke10 rate yestu»
I used this book to teach my students and i recommended that for you