100 Days of DevOps — Day 86-Python Flow Control(if-else statement)

Prashant Lakhera
3 min readMay 7, 2019

--

To view the updated DevOps course(101DaysofDevOps)

Course Registration link: https://www.101daysofdevops.com/register/

Course Link: https://www.101daysofdevops.com/courses/101-days-of-devops/

YouTube link: https://www.youtube.com/user/laprashant/videos

Welcome to Day 86 of 100 Days of DevOps, Focus for today is Python Flow Control(if-else statement)

if Statements

To understand if statement, let see one example

name = "Prashant"
if
name == "Prashant":
print ("Hello " + name)
Hello Prashant

Here as you can see, if statement starts with a condition, if the condition evaluates to true the indented code block is executed else it just skip the statement.

Indentation is just a Python way to tell which code block is inside if statement and which is not.

So if statement can be used to conditionally execute code depending on whether or not the if statement’s condition is True or False

Now if we change the code, in this case as the condition evaluates to False, it just skips the print block.

name = "Prashant1"
if
name == "Prashant":
print ("Hello " + name)

Let’s take a look at if-else statement

name = "Prashant1"
if
name == "Prashant":
print("Hello " + name)
else:
print("Hello "+ name)
Hello Prashant1

As you can see, if the statement is not True so else block is executed. Remember only one block is going to be executed.

Let’s add elif to this list which provides many blocks to be executed. In the case of elif order of elif statement matter, as the execution enters the block which is True, it just going to skip rest of the conditions.

An else statement comes at the end. Its block is executed if all of the previous conditions have been false

name = "Prashant1"
if
name == "Prashant":
print("Hello " + name)
elif
name == "Lak":
print("Hello "+ name)
else:
print("Hello "+ name)
Hello Prashant1

Let’s take a look at one more code

print("Please enter your name")
name = input()
if
name:
print("Welcome Back")
else:
print("Who are you")

This is something weird and the reason it will work because of the Truthy/Falsey value. Blank string means falsey and others are truthy.

So if we don’t enter anything

Please enter your nameWho are you

In case if I enter a value(Prashant)

Please enter your name
Prashant
Welcome Back

In case of integers

  • 0 is falsey and all others are truthy
  • 0.0 is falsey, all others are truthy

To check these let's pass value to bool() function.

>>> bool(0)False>>> bool(42)True

Looking forward from you guys to join this journey and spend a minimum an hour every day for the next 100 days on DevOps work and post your progress using any of the below medium.

Reference

--

--

Prashant Lakhera
Prashant Lakhera

Written by Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

No responses yet