Home > Backend Development > Python Tutorial > Python - Operators and Conditionals

Python - Operators and Conditionals

王林
Release: 2024-07-19 04:34:40
Original
764 people have browsed it

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







<pre class="brush:php;toolbar:false">a = 2
b = 4

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

True

Copy after login

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

Copy after login
#not
condition_1 = True
print(not condition_1 )

False

Copy after login

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

Copy after login

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

Copy after login

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

Copy after login

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

Copy after login

or,

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

Copy after login

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

Copy after login

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

Copy after login

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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template