Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 6: Boolean and Comparisons

0 Replies, 695 Views

[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"
Love me like you do Heart
(This post was last modified: 04-18-2022, 06:55 PM by Covid-19.)

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,246 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,636 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,669 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,843 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,607 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,841 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,792 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,829 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,824 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,827 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)