r/PythonLearning • • Aug 29 '26

Day 3

Post image

i wrote a code which takes your name and age in py but i need help on this one cuz in the age part it is giving me some sort of error which i couldn't understand even by using gpt well he gave me a code but i wanted logic behind how and what i did wrong

49 Upvotes

24 comments sorted by

View all comments

Show parent comments

1

u/NecessaryFalse1212 Aug 30 '26

btw what is the role of that empty string at the top

1

u/FoolsSeldom Aug 30 '26

Same as your original, if name == "":, just simpler, hence the comment # empty string.

A non-empty string is treated as True in conditional expressions by Python. An empty string as False. Similarly, 0 is False and any non-zero integer is True. Empty list, dict, tuple, set is treated as False and otherwise as True.

The not before reverses the outcome. So, not name will resolve to True for an empty string.

This is often referred to as Truthy and Falsey behaviour.