Python - Operators and Conditionals
Tags: python, tutorial, learning
Published: 2024-07-17 10:31:10

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

```

```
a = 2
b = 4

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

True

```

**_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

```

```
#not
condition_1 = True
print(not condition_1 )

False

```
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")

```
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

```

---


**_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.")

```
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.")

```
or,

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

```
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}.")

```

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")

```

Try it out yourself...