bool(1j)
“Is the complex number 1j considered True or False?”
- 1j is a complex number with real part 0 and imaginary part 1.
- In Python, any nonzero number (whether int, float, or complex) evaluates to True when converted to bool.
- Only 0, 0.0, or 0j (complex zero) evaluate to False.
A complex number is a number that has two parts:
z=a+bj
- a → the real part (ordinary number line)
- b → the imaginary part (multiplied by j in Python, or i in mathematics)
Key ideas
- The imaginary unit j (or i in math) is defined as \sqrt{-1}.
- Complex numbers extend the real numbers so we can solve equations like x^2+1=0, which has no real solution but has complex solutions x=\pm i.
Visual intuition
You can think of complex numbers as points on a 2D plane:
- The horizontal axis = real part
- The vertical axis = imaginary part
So 3+4j is the point (3, 4).
'') into a boolean value.Rule
- Empty sequences/collections (like
'',[],{},()) evaluate to False. - Non-empty sequences/collections (like
'hello',[1],(0,)) evaluate to True.
[] → List
- A list is an ordered, mutable sequence of elements.
- You can store mixed types (numbers, strings, other lists).
- Lists allow indexing, slicing, and modification
"hello" is indeed the fourth element (index 3, since Python starts counting at 0).
{} → Dictionary (or empty set, depending on usage)- By default,
{}creates an empty dictionary. - A dictionary stores key-value pairs.
- Keys must be unique and immutable (like strings, numbers, tuples).
() → Tuple- A tuple is an ordered, immutable sequence.
- Similar to a list, but you cannot change its contents after creation.
- Often used for fixed collections of items.
my_tuple = (10, 20, 30) print(my_tuple[1]) # → 20
Data structures include list, tuple, dictionary and set.
explicit: clear and easy to understand, so that you have no doubt what is meant
a = 5
b = 5
if a - b:
print('a and b are not equal!')
No output will be printed.
if condition:-(haveUmbrella or weatherIsNice) → evaluates to (False or True) → True.-not (True) → False.False, the else block runs.- The condition says: “If you do not have an umbrella and the weather is not nice, then stay inside.”
- In this case, the weather is nice, so you can go for a walk even without an umbrella.
This condition means:
- “Stay inside only if you don’t have an umbrella and the weather is not nice.”
- In this case, the weather is nice, so you go for a walk.
This condition means:
- “If you either have an umbrella or the weather is nice, then go for a walk.”
- Only if both are false (no umbrella and bad weather) will it print “Stay inside.”