Red Security
Tutorial Python 6: Boolean and Comparisons - Printable Version

+- Red Security (https://redsecurity.info/cc)
+-- Forum: Programming (https://redsecurity.info/cc/forumdisplay.php?fid=5)
+--- Forum: Python (https://redsecurity.info/cc/forumdisplay.php?fid=9)
+--- Thread: Tutorial Python 6: Boolean and Comparisons (/showthread.php?tid=7886)



Python 6: Boolean and Comparisons - Covid-19 - 04-18-2022

[Image: BFMMlbcQvml9HSqXcvNp]
Control Flow:
In the previous thread we saw that our program was running in sequential steps from top to bottom but now I am introducing to you a new concept: how can we skip some code or how can we shift flow from one line to another. As you know about Integers, Strings and floats let me introduce to you with a new data type called Boolean. Boolean has two values 1 and 0 which represent true and false respectively. We can create them by comparing two values. For comparison we use two equal “==” signs.

Exclamation  Note: We use one equal sign ‘=’ for assigning value to the variable and double equal sign to compare two values.

Let’s get them into code for understand it in details

program:

Code:
Boolean = false

print(myBoolean)

print(2 == 2)

print(“Covid” == “hello”)

Try to run this program and see the results of this code.

Here are some other comparison operators which we can perform.

== (Equal to)
<  (less than)
>  (greater than)
<= (less than or equal to)
>= (greater than or equal to)
!=  (not equal to)

Let’s see these all in program

Code:
number = 10

print(number ==  10)
print(number <=  10)
print(number >=  10)
print(number >  10)
print(number <  10)
print(number !=  10)

IF statement:
If statement is used in decision making it runs based on a given condition it means we say if the condition is true run the code inside if statement if false then skip these statements. For Example, we say that in real life if you work hard you will succeed so here ‘work hard’ is the condition. So if work hard is true then you will succeed. Before we get into coding let’s understand it’s syntax

if condition:
Statements

As you can see that there is a space of tabs before statements because that represents these statements inside the ‘if’ statement. If you don’t give this space it will generate an error so be careful with these spaces. Now, let’s jump into code…

Program:
Code:
name = input(“What is your name? ”)

if name == ‘covid’:
print(“Welcome covid :)”)


In this program this print statement runs only when user input ‘covid’..

But what when the user input something else so the program will not do anything and terminate. Because we did not give anything to it after the ‘if’ statement. So if we want to run another block of code when the ‘if’ statement condition is false. Here we use the “else“ statement. For Example when user input anything except ‘covid’ we want to print “(name) is not welcomed here, Sorry”

So, dive into code

Program

Code:
name = input(“What is your name? ”)

if name == ‘covid’:
print(“Welcome covid :)”)
else:
print(name + “ is not welcomed here, Sorry”)

("Allah Hafiz" and "Good Bye" Till the next post )
"Keep Learning and Keep exploring"