Skip to main content

Python Conditional Statements

 Python Conditional Statements

Hi. Welcome back to another amazing tutorial on this platform. If you are new here, please check out our previous articles. Today, we will be discussing conditional statements in python.

Python has 4 main conditional statements and these conditional statements are mostly used in decision making. Below are the different types of conditional statements, with syntaxes and examples.

if statements

This statement always starts with a Boolean expression followed by a statement or a group of statements that tells what action should be done if the test expression is true. 

SYNTAX

if expression:
   statement(s)

Take note that the compiler will only execute the program only if the ‘if expression’ is true and you must take into consideration the indentation of the statements on the body of the ‘if expression’.

 Let us look now at an example

val = 25 
if ( val == 25 ) : print ("  I am 25 years old")
print ("Good bye!")

The compiler prints the statements that are in the brackets (()) if the inputs meets the condition, which in our case here is the ‘val’ 

OUPUT

If….else statements

This statement is the same if statement. The only difference that comes in here is when the condition is false, then the compiler will execute the statements in the else block. 

SYNTAX

if test expression:
    statement(s)
else:
    statement(s)

To better illustrate, let’s look at an example with the ‘if…else structure

a = 25
b = 30
if a > b:
  print("kelvin is younger than rayan")
else:
  print("rayan is older than kelvin")

The compiler in this case did not found the given if statement to be true, so it takes the statement in the else block 

OUTPUT

If…elif…else statements

An ‘elif’ statement is mostly used to check multiple expressions. It first checks if the given ‘if statement’ is true and if found true, the compiler will execute the statement in the ‘if’ block. If false, it tests the condition in the ‘elif’ block. If the ‘elif’ statement is true, then python will execute the elif block. Otherwise, then the else statement will be executed. 

SYNTAX

if expression:
     if block
elif expression:
     elif block
else:
else block

EXAMPLE

num = 1122
if 9 < num < 99:
    print("Two digit number")
elif 99 < num < 999:
    print("Three digit number")
elif 999 < num < 9999:
    print("Four digit number")
else:
    print("number is <= 9 or >= 9999")

OUTPUT


Nested if ….elif….else statements

This particular statement is used is to check for other statements when the first if statement is true.

SYNTAX.

If test expression1:
If test_expression-a:
    statement _block1-a
elif test_expression-b
    statement_block1-b
else
    statement_block1-c
elif test_expression2:
else :
    statement_block3
var = 400
if var < 140:
   print ("Expression value is less than 140")
   if var == 150:
      print ("Which is 150")
   elif var == 75:
      print ("Which is 75")
   elif var == 50:
      print ("Which is 50")
   elif var < 50:
      print ("Expression value is less than 50")
else:
   print ("Could not find true expression")

print ("Good bye!")

OUTPUT

Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...