I was given an assignment for the fourth time. My goal was to write a python program that checks if a list is empty or not. If the list is empty, then it’ll print empty, if not, it’ll print not empty.
I’m going to start with an empty list.
emp = []
So that I can start the next step. I’m going to use
- if statement
- len()
- else
- print()
When using if, there must be a condition met. My condition is:
emp == 0
But there’s somthing wrong. emp is a variable assigned to an empty list. An empty list has nothing in it which equals 0. So how come? I can’t compare a list with an integer. Like how? This can be a common mistake.
So instead, we can do the len() of emp instead. Like this:
len(emp) == 0
Here is the code:
emp = []
if len(emp) == 0:
print("empty")
else:
print("not empty")
The End for now…
(JK)