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
1 2 | 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,
1 2 | 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
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)
Operator | explanation | example | output |
+ | 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 operator
Comparison operators compare two values and return the answer in boolean form (True or False).
Operator | explanation | example | output |
== | Returns True if both the values are equal else returns Fale | print(5==5) print(7==5) | True False |
!= | Returns True if both the values are not equal else returns Fale | print(5==5) print(7==5) | False True |
> | Returns True if LHS is greater than RHS | print(5>5) print(7>5) | False True |
>= | Returns True if LHS is greater than or equal to RHS | print(5>=5) print(7>=5) | True True |
< | Returns True if LHS is lesser than RHS | print(5<5) print(3<5) | False True |
<= | Returns True if LHS is less than or equal to RHS | print(5<=5) print(3<=5) | True True |
Logical operator
Logical operators are used to combining conditional statements and returns True or False.
Operator | Explanation | Example | output |
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 operator
Assignment operators are used to assigning values to variables.
Operator | Example | Same as |
= | x=5 | x=5 |
+= | x+=5 | x=x+5 |
-= | x-=5 | x=x-5 |
*= | x*=5 | x=x*5 |
/= | x/=5 | x=x/5 |
//= | x//=5 | x=x//5 |
%= | x%=5 | x=x%5 |
**= | x**=5 | x=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.
Pingback: Rock Paper Scissor Game in python -