Exercise 28
Write a Python program that tests whether a list is empty or not. Same question for a string of characters.
Solution
First method
Try if a list is empty
# define a list L
L = list()
# try if the list L is empty
if L == []:
print("The list L is empty")
else:
print("The list L is not empty")
Try if a string is empty
# define a string s
s=""
# try if the string s is empty
if s == "":
print("The string s is empty")
else:
print("The list s is not empty")
Second Method
Try if a list is empty
# define a list L
L = list()
# try if the list L is empty by using the len() function
if len(L) == 0:
print("The list L is empty")
else:
print("The list L is not empty")
Try if a string is empty
# define a string s
s=""
# try if the string s is empty by using the len() function
if len(s) == 0:
print("The string s is empty")
else:
print("The list s is not empty")
Younes Derfoufi