Input function and basic operators

Input Function and Basic Operators in Python

Introduction

“Consistency is more important than perfection”

In this article, we will be discussing how to interact with our users and how to do basic operations with a few basic operators.

Input function

The input function allows the user to input any value and we can save it with the help of a variable for future use.

Syntax

variable=input(prompt)

Here, the prompt is totally optional, if we write something inside the prompt it will be displayed before getting the input, if we leave it empty no message will be displayed but just waiting for the input.

Always try to write the correct prompt for making a user-friendly program.

Example

name=input("Enter your name: ")
print("Hello", name)

Output

Enter your name: xyz
Hello xyz

Explanation

In line 1 we are getting the input from the user and saving it inside a variable called name.

In line 2, we are greeting the user with the help of the variable where we have saved the name.

Please note

By default, all the input given by the user is going to be a string data type. If you want to convert the string to any other data type then we need to write the input function between the data type name. 

For example,

num1=int(input("Enter a number:"))#---> int is used to convert string to integer.
num2=float(input("Enter a float number"))#---> float is used to convert string to float.

Operators

In python, we are having 7 different categories of operators. In this article, we will be seeing only the basic 4 categories of operators.

  • Arithmetic operator
  • Comparison operator
  • Logical operator
  • Assignment operator
See also  Comments and Print function

The other 3 which we are going to cover in the later part of the tutorial are, 

  • Identity operator
  • Membership operator
  • Bitwise operator

Now, let us see all the operators in detail.

Arithmetic operator

Arithmetic operators are generally used to perform mathematical operations between numbers (int and float)

Operatorexplanationexampleoutput
+Adds the numbersprint(5+6)11
Subtracts the numbersprint(5-6)-1
*Multiplies the numbersprint(5*6)30
Divides the numbersprint(7/2)3.5
//Divides the number and returns the quotient aloneprint(7//2)3
%Divides the number and returns the remainder aloneprint(7%2)1
**It provides the answer for the first number to the power of the second numberprint(2**3)8

Comparison operator

Comparison operators compare two values and return the answer in boolean form (True or False).

Operatorexplanationexampleoutput
==Returns True if both the values are equal else returns Faleprint(5==5)
print(7==5)
True
False
!=Returns True if both the values are not equal else returns Faleprint(5==5)
print(7==5)
False
True
>Returns True if LHS is greater than RHSprint(5>5)
print(7>5)
False
True
>=Returns True if LHS is greater than or equal to RHSprint(5>=5)
print(7>=5)
True
True
<Returns True if LHS is lesser than RHSprint(5<5)
print(3<5)
False
True
<=Returns True if LHS is less than or equal to RHSprint(5<=5)
print(3<=5)
True
True

Logical operator

Logical operators are used to combining conditional statements and returns True or False.

OperatorExplanation Exampleoutput
andReturns True if both the statement is True else return Falseprint(7<2 and 7==7)False
orReturns True if either of the statement is trueprint(7<2 or 7==7)True
notReverse the result. Returns True if the answer is Fales and returns False if the answer is Trueprint(not 7==7)False

Assignment operator

Assignment operators are used to assigning values to variables.

See also  Guess the Number Game
OperatorExampleSame as
=x=5x=5
+=x+=5x=x+5
-=x-=5x=x-5
*=x*=5x=x*5
/=x/=5x=x/5
//=x//=5x=x//5
%=x%=5x=x%5
**=x**=5x=x**5

Practice yourself

  • Get two numbers from the user and print their addition, subtraction, multiplication, and division.
  • Get English, maths, science, social, and computer marks from the user and find his average marks.
  • Explore all the operators with different kinds of input and explore the answers.
  • Perform arithmetic operations on complex numbers.
  • Combine two different data types with different operators and explore the output.

Conclusion

Thus, now you must be having a clear idea regarding input function and different basic operators. As the quote goes, “We all learn from our mistakes”. Try to make mistakes and explore the different results and share them in the comments, which can help others to learn from your mistakes.

1 thought on “Input Function and Basic Operators in Python”

  1. Pingback: Rock Paper Scissor Game in python -

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.