“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.
The input function allows the user to input any value and we can save it with the help of a variable for future use.
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.
name=input("Enter your name: ") print("Hello", name)
Enter your name: xyz Hello xyz
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.
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.
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.
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
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 operators are generally used to perform mathematical operations between numbers (int and float)
|+||Adds the numbers||print(5+6)||11|
|–||Subtracts the numbers||print(5-6)||-1|
|*||Multiplies the numbers||print(5*6)||30|
|/||Divides the numbers||print(7/2)||3.5|
|//||Divides the number and returns the quotient alone||print(7//2)||3|
|%||Divides the number and returns the remainder alone||print(7%2)||1|
|**||It provides the answer for the first number to the power of the second number||print(2**3)||8|
Comparison operators compare two values and return the answer in boolean form (True or False).
|==||Returns True if both the values are equal else returns Fale||print(5==5)|
|!=||Returns True if both the values are not equal else returns Fale||print(5==5)|
|>||Returns True if LHS is greater than RHS||print(5>5)|
|>=||Returns True if LHS is greater than or equal to RHS||print(5>=5)|
|<||Returns True if LHS is lesser than RHS||print(5<5)|
|<=||Returns True if LHS is less than or equal to RHS||print(5<=5)|
Logical operators are used to combining conditional statements and returns True or False.
|and||Returns True if both the statement is True else return False||print(7<2 and 7==7)||False|
|or||Returns True if either of the statement is true||print(7<2 or 7==7)||True|
|not||Reverse the result. Returns True if the answer is Fales and returns False if the answer is True||print(not 7==7)||False|
Assignment operators are used to assigning values to variables.
- 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.
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.