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;
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.
For ex,
a = 2 b = 4 result = (a > b) print(result) False <pre class="brush:php;toolbar:false">a = 2 b = 4 result = (a <= b) print(result) True
3.Logical operators
Used to combine conditionals (if, else)
#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 ?
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
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...
The above is the detailed content of Python - Operators and Conditionals. For more information, please follow other related articles on the PHP Chinese website!