What are the len() and count() in python?
In Python, both len()
and count()
are commonly used functions but serve different purposes.
1) len()
Function:
len() is used to return the number of items (length) in an object, such as strings, lists, tuples, dictionaries, or sets. If we use len() function on a string then it returns a total number of characters in that string.
For Example:
name = "this is theagleye hub."
print(len(name))
Output:
22
Note: We should never check the length of some data types with the help of len(); these are int, float, bool, and complex.
For Example:
number = 986286921
print(len(number))
Output:
ERROR!
Traceback (most recent call last):
File "<main.py>", line 2, in <module>
TypeError: object of type 'int' has no len()
=== Code Exited With Errors ===
Also, we can check the length of our spacial data types like list, tuple, set, or dictionary. By applying the len() function on these they return the total number of elements in that particular variable.
For Example:
nature = ["sun", "moon", "birds", "sky"]
print(len(nature))
Output:
4
2) count()
Function:
count()
is used to count the number of occurrences of a specific element in a list, string, or tuple. The count () function checks whether how many times a particular element appears in the data.
Syntex:
veriable_name.count(find_count_of)
Example:
eagle = "the eagle eye website provide good and understandable content."
print(eagle.count("nd"))
Output:
3
In this example, nd appears 3 times in this complete string.
Note:
You cannot use count() with data types like:
- Integers (int)
- Floats (float)
- Booleans (bool)
- Complex numbers (complex)
- Set (set)
- Dictionary (dict)
You only apply count() on the string, list, or tuple.
For Example:
eagle = [12, 34, 23, 34, 546, 12, 23, 45]
print(eagle.count(12))
Output:
2
Interview Questions and Answers for len() and count() Functions in Python
Q1: What does the len() function do in Python?
A1: The len() function returns the number of items (length) in an object, such as a string, list, tuple, dictionary, or set. It helps to determine the size of a collection.
For Example:
my_list = [1, 2, 3]
print(len(my_list))
Output:
3
Q2: Can you use the len() function on integers or floats?
A2: No, the len() function cannot be used on data types like integers, floats, booleans, or complex numbers. If you try, you will get a TypeError.
For Example:
num = 12345
print(len(num))
Output:
TypeError: object of type 'int' has no len()
Q3: What data types can you apply the len() function to?
A3: You can apply the len() function to strings, lists, tuples, dictionaries, and sets.
For Example:
my_string = "hello"
my_list = [1, 2, 3]
print(len(my_string))
print(len(my_list))
Output:
5
3
Q4: What does the count() function do in Python?
A4: The count() function returns the number of occurrences of a specific element in a string, list, or tuple. It helps to check how many times a particular item appears in the data.
For Example:
sentence = "hello world"
print(sentence.count("l"))
Output:
3
Q5: Can you use the count() function on data types like integers, floats, or dictionaries?
A5: No, the count() function cannot be used on data types like integers, floats, booleans, sets, or dictionaries. It is only applicable to strings, lists, or tuples.
For Example:
my_dict = {1: 'a', 2: 'b'}
print(my_dict.count(1))
Output:
AttributeError: 'dict' object has no attribute 'count'
Q6: How would you count the number of times a character appears in a string using the count() function?
A6: You can count the occurrences of a character in a string using the count() function by specifying the character as the argument.
For Example:
word = "banana"
print(word.count("a"))
Output:
3
Q7: How do you count occurrences of a value in a list using the count() function?
A7: You can count the number of times a specific value appears in a list using the count() function.
For Example:
numbers = [1, 2, 2, 3, 4, 2]
print(numbers.count(2))
Output:
3
Q8: Can the count() function be used to find a substring in a string?
A8: Yes, the count() function can be used to find how many times a substring occurs within a string.
For Example:
sentence = "the eagle eye website"
print(sentence.count("e"))
Output:
5
Q9: What is the difference between len() and count() functions?
A9: The len() function returns the total number of items in an object, whereas the count() function returns the number of occurrences of a specific element in a list, string, or tuple.
For Example:
Copy code
my_list = [1, 2, 3, 2, 2]
print(len(my_list))
print(my_list.count(2))
Output:
5
3
Q10: What will be the output of the following code?
word = "hello world"
print(len(word))
print(word.count("o"))
A10:
- len(word) will return the total number of characters in the string: 11.
- word.count("o") will return the number of times "o" appears in the string: 2.
Output:
11
2
Q11: How do you check if a list contains a specific value and how many times using the count() function?
A11: You can use the count() function to check how many times a specific value is present in a list. To show the output in an explanable way you can use f-string that helps to show output.
For Example:
my_list = [1, 2, 3, 2, 4, 2]
print(f"the count is : {my_list.count(2)}")
Output:
the count is : 3
=== Code Execution Successful ===
Q12: What will happen if you use the count() function on a data type that doesn't support it?
A12: If you try to use the count() function on a data type that doesn't support it (like integers, floats, or dictionaries), Python will raise an AttributeError.
For Example:
num = 12345
print(num.count(1))
Output:
AttributeError: 'int' object has no attribute 'count'
Tags:
Leave a comment
You must be logged in to post a comment.