This article is a part of a challenge series on Python. You can find the link to the previous articles in this series here,
It has been an exciting 4 days of experience learning Python. Until now I have been able to cover some of the basic syntaxes of Python along with the data types and how to perform actions on them using built-in functions and methods along with some best practices. This was probably the dull parts and scratching the surface of Python. Today my short goal was to understand logical and conditional programming along with repeating tasks using loops in Python. Very interesting indeed!
Conditional Logic
- age = input('Please enter your age')
- if(int(age) >= 18):
- print('You are allowed to enter the club')
- else:
- print('Sorry! you are not allowed!')
The above is an example of an if-else conditional statement in Python. It is used to execute logic based on some condition. If the condition is not fulfilled, then the code in the else block is executed. Coming from the JavaScript universe, the differences I noted is there are no curly braces surrounding the if-else blocks and instead of a : is used after the conditional statement. The block inside a conditional statement is also indented. I wired these two things to my Python mental model,
- exam_score = input('Enter your exam score')
- if(int(exam_score) > 90):
- print('You have got grade A+')
- elif(int(exam_score) > 80):
- print('You have got grade A')
- else:
- print('You have got grade B')
If there are multiple conditions that need to be executed, then an elif block is used. In many other programming languages including JavaScript, it is called else if block. There can be any number of else if blocks to check different conditions. Python makes it more compact.
- is_adult = True
- is_licensed = True
- if(is_adult and is_licensed): ## In JavaScript && is used instead of 'and'
- print('You are allowed to drive')
- else:
- print('You are not allowed to drive')
The above code checks for two conditions in a single expression to execute the block. The and keyword is used to check if both conditions evaluates to True then only the block is executed. This syntax is so easy to read! Python definitely is more readable. Indentation is the way to separate lines of code using spaces/tabs so that the interpreter can distinguish code blocks and execute the code accordingly. Python does not use any braces unlike JavaScript, and instead uses indentation to chunk blocks of code. The code editors make our life easy by automatically doing the indentation. The online editor Repl that I am using as a playground also does that.
- if(10 > 8):
- print('Such a silly logic. I will get printed')
- else:
- print('I will never get printed')
- print('I am not get printed coz I am indented')
- # Now without indentation
- if(10 > 8):
- print('Such a silly logic. I will get printed')
- else:
- print('I will never get printed')
- print('I will be printed anyways coz I am not indented')
- # The above line gets printed always as interpreter treats it as a new line
Truthy and Falsy
- Values that evaluate to true are called Truthy
- Values that evaluate to false are called Falsy
When checking for a condition, the expression inside a condition is evaluated to a boolean value using type conversion. Now all values are considered “truthy” except for the following, which are “falsy”:
- None
- False
- 0
- 0.0
- 0j
- Decimal(0)
- Fraction(0, 1)
- [] - an empty list
- {} - an empty dict
- () - an empty tuple
- '' - an empty str
- b'' - an empty bytes
- set() - an empty set
- an empty range, like range(0)
- objects for which
- obj.__bool__() returns False
- obj.__len__() returns 0
- username = 'santa' # bool('santa') => True
- password = 'superSecretPassword' # bool('superSecretPassword') => True
- if username and password:
- print('Details found')
- else:
- print('Not found')
Ternary Operator
While going through the syntax of the ternary operator in Python, I found it a bit confusing initially but then found it quite easy to read compared to the one I am familiar with in my JavaScript world
- is_single = True
- message = 'You can date' if is_single else 'you cannot date'
- # result = (value 1) if (condition is truthy) else (value 2)
- print(message) # You can date
Ternary operator is also sometimes called a conditional expression. It is a very handy way to check a condition in a single statement. I compared it with the ? operator syntax of JavaScript to solidify my mental model.
Short-Circuiting
While checking for multiple logical conditions in a single statement, the interpreter is smart enough to to ignore the rest of the condition and check if the first condition fails. This is known as short-circuiting. It can be better explained using an example.
- knows_javascript = True
- knows_python = True
- if(knows_javascript or knows_python): # doesn't depend on value of knows_python
- print('Javscript or python developer')
- else:
- print('Some other developer')
- Copy
- knows_javascript = False
- knows_python = True
- if(knows_javascript and knows_python): # doesn't depend on value of knows_python
- print('Javscript or python developer')
- else:
- print('Some other developer')

Join the conversation! Your thoughts help the community grow.