DEV Community

ABYS
ABYS

Posted on

Python - Operators and Conditionals

In this blog, we'll get to know about operators, conditionals and input() functions.
Let's jump into Operators.

What are Operators ?

Symbols that perform specific mathematical / logical operations in computer.
This is of 3 types namely;

  • Arithmetic operators
  • Comparison operators
  • Logical operators

What are these and what functions they perform ?

Lemme tell something, you guys will be surprised to learn how simple it is...

1.Arithmetic operators

It includes basic mathematics like addition, subtraction, multiplication, division and few more..
We've seen all these in previous blog where we created a calculator.

ok you would be asking what about the remaining two..
yeah, I'll discuss that now.

2.Comparison operators

It compare two values and return either True or False.

  • Equal to ( == )
  • Not equal to ( != )
  • Greater than ( > )
  • Less than ( < )
  • Greater than or equal to ( >= )
  • Less than or equal to ( <= )

For ex,

a = 2
b = 4

result = (a > b)
print(result)

False

Enter fullscreen mode Exit fullscreen mode
a = 2
b = 4

result = (a <= b)
print(result)

True

Enter fullscreen mode Exit fullscreen mode

3.Logical operators

Used to combine conditionals (if, else)

  • and - if both the statements are true, Returns True.
  • or - if one of the statements is true, Returns True.
  • not - returns False if the result is true i.e, Reverses the result.
#and
condition_1 = True
condition_2 = True
print(condition_1 and condition_2)

True

condition_1 = True
condition_2 = False
print(condition_1 and condition_2)

False

#or
condition_1 = True
condition_2 = False
print(condition_1 or condition_2)

True

Enter fullscreen mode Exit fullscreen mode
#not
condition_1 = True
print(not condition_1 )

False

Enter fullscreen mode Exit fullscreen mode

With this, Operators done.


Now, What are Conditionals ?

  • It used decide which path to take based on given conditions.
  • The commonly used conditional statements in Py. are if, elif, and else.

Lemme explain it using a realtime scenario,
I'm planning to go out and I wanna select my clothes. So, I've three options tracks, dress or I'm not going.

if tracks_available:
wear tracts
elif dress_aviable:
wear dress
else:
sit at home

The same we're gonna do it by coding.
Let's compare two numbers;

a = 25
b = 25
if a > b:
    print("a is greater than b")
elif a == b:
    print("a is equal to b")
else:
    print("a is less than b")

Enter fullscreen mode Exit fullscreen mode

So, each condition is checked by steps, as according to line 5 and 6
the result will be as following..

a is equal to b

Enter fullscreen mode Exit fullscreen mode

Get User Input using input()

It is to get input from the user.
We always get input in string type i.e, text format, so if we need a number we've to convert it.

Here's a basic usage of this function:

name = input("What is your name? ")
print("Hello, " + name + "!")
print("Have a nice day.")

Enter fullscreen mode Exit fullscreen mode

It asks the user for their name and then prints as given.
But, that's not the case for numbers as we've discussed earlier while creating calculator.

For numbers we ought to convert the input from string to an integer or float..

age = input("Enter your age: ")
age = int(age)
print("You are " + str(age) + " years old.")

Enter fullscreen mode Exit fullscreen mode

or,

age = int(input("Enter your age: "))
print("You are " + str(age) + " years old.")

Enter fullscreen mode Exit fullscreen mode

Let us now look into a question which comprises it all.

Create a program that asks the user to enter a number and then prints whether the number is positive, negative, or zero.

num = float(input("Enter a number: "))
if num > 0 :
   result = "positive"
elif num < 0 :
   result = "negative"
else :
   result = 0
print(f"The number is {result}.")

Enter fullscreen mode Exit fullscreen mode

This program

  • Asks the user to enter a number.
  • Converts the input to a float (as it could be applicable for decimals too)
  • Check if the number is positive, negative, or zero, and prints the result.

Okay, with this in our mind try to make a grading system.

Grading system
A - 100 to 90
B - 90 to 80
C - 80 to 70
D - 70 to 60
E - 60 to 45
FAIL - 45 to 0

Lets create a program that takes a numerical grade as input and prints the corresponding letter grade (A, B, C, D, or F). Total Marks is 100.

mark = float(input("Enter your mark : "))

if mark >= 91 and mark <= 100:
    print("Grade A")
elif mark >= 81 and mark < 91:
    print("Grade B")
elif mark >= 71 and mark < 81:
    print("Grade C")
elif mark >= 61 and mark < 71:
    print("Grade D")
elif mark >= 45 and mark < 61:
    print("Grade E")
elif mark < 45:
    print("Fail")
else:
    print("Mark not valid")

Enter fullscreen mode Exit fullscreen mode

Try it out yourself...

Top comments (0)