Python's Working Logic - Code Indentation In Python, each line of code is executed respectively until our program ends.. Let's see this with a simple example below: Before we start to th
Trang 1If Else Statements
In this lesson, we will learn to make decisions in a Python program using different forms of if else statement
Python's Working Logic - Code Indentation
In Python, each line of code is executed respectively until our program ends
Let's see this with a simple example below:
Before we start to the conditional statements, let's look at how to a Python program runs if you want When a Python program runs, it starts from the top to the bottom Each line and each operation is executed rescpectively and step by step And if there is no code to be run, our Python program ends
In [1]: a = 1
b = 2
c = 3
print(a+b+c)
In the program above each line of code is executed respectively until our program ends And when it finished, it is printed
on the screen the sum of 3 values as you can see So, this is our the essential logic of working Python programs But, the structure of every Python programs that we will write in the future may not be that simple
Python programs are actually consist of block of codes generally So, what is a block of code? You can think a block of code as a unit that consist of more than one code lines working for a specific task in a particular domain From now on, usually we are going to write our Python codes within block of codes anymore
To indicate a block of code in Python, you must indent each line of the block by the same amount Python differentiates the block of codes thanks to the indents This block of code in our example if-statement is indented four spaces, which is a typical amount of indentation for Python
In [2]: # Simple Password Checking Program
password = "omer123" # Block 1
if password == "omer123":
print('Logged in ') # Block 2 print('All done!') # Block 1
6
Logged in
All done!
Trang 2In above, they were printed "Logged in" and "All done".
So, print function is executed two times although their blocks are different All the codes of Block 1 and Block 2 are being executed
In [3]: # Simple Password Checking Program
password = "omer123" # Block 1
if password == "alice":
print('Logging on ') # Block 2 print('All done!') # Block 1
Since in our if condtion password's value is "omer123" and it is not equal to "alice"
So, our if condition is not satisfied Therefore, it's not printed "Logging on message"
Python Conditional Statements
Now, we're ready to learn conditional statements in Python
Conditional statements is required when we want to execute a code only if a certain condition is satisfied
Let’s start with if statements
if Statement
if statements in Python are very simple
If statement allows us to check a a condition that we need in anywhere in our programs
It works only condition is True Here is our structure of if statements
if (condition):
do something #if statement body
The body of the if statement is indicated by the indentation Body starts with an indentation and the first unindented line marks the end
Let's make an example:
In [4]: # Checks whether a number is positive or negative
num = int(input("Please enter a number: "))
if num > 0:
print(num, "is a positive number.") else:
print(num, "is a negative number.") All done!
Please enter a number: 2
2 is a positive number
Trang 3else Statement
else block works in case of the if statement is not satisfied
The structure of else block is quite similar with if statement
else:
do something #else block body
There is nothing to write after the else statement, because it works in case of the if statement is not satisfied
Let's make an another example
In [5]: # age control
age = int(input("Please enter your age: "))
if (age < 18):
print("You're not allowed to enter this place!") else:
print("Welcome to the club!")
In [6]: # Simple Password Checking Program
password = "omer" # Block 1
if password == "alice":
print('Logging on ') # Block 2 else:
print('Incorrect password.') # Block 2 print('All done!') # Block 1
We can write an if block without an else block, but, we can not write else block without an if block
If we want to write an else block, we have to write an if block else block is always written after if statement
In [7]: if 1 < 2:
print("Hello world!")
In [8]: else:
print("Hello world!")
Please enter your age: 18
Welcome to the club!
Incorrect password
All done!
Hello world!
File "<ipython-input-8-bba8cc20ee03>", line 1
else:
^
SyntaxError: invalid syntax
Trang 4This is the core logic of the conditional statements.
That's all for this lesson
We're going to continue to see other conditional statements in next lesson
In [ ]: