If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In Python, arithmetic operators are used to perform basic arithmetic operations on numbers. These operators allow you to add, subtract, multiply, divide, and perform other mathematical operations. Here are the common arithmetic operators in Python:
- Addition: + Adds two numbers or concatenates two strings.
Example:
pythonCopy code
a = 5 b = 3 result = a + b print(result) # Output: 8 str1 = "Hello, " str2 = "World!" result_str = str1 + str2 print(result_str) # Output: Hello, World!
- Subtraction: - Subtracts the second number from the first number.
Example:
pythonCopy code
a = 10 b = 3 result = a - b print(result) # Output: 7
- Multiplication: * Multiplies two numbers or repeats a string.
Example:
pythonCopy code
a = 5 b = 4 result = a * b print(result) # Output: 20 str1 = "Hello! " result_str = str1 * 3 print(result_str) # Output: Hello! Hello! Hello!
- Division: / Divides the first number by the second number, and the result is a float.
Example:
pythonCopy code
a = 10 b = 2 result = a / b print(result) # Output: 5.0 (float) c = 15 d = 4 result2 = c / d print(result2) # Output: 3.75 (float)
- Floor Division: // Divides the first number by the second number, and the result is the floor value (rounded down) of the division as an integer.
Example:
pythonCopy code
a = 10 b = 3 result = a // b print(result) # Output: 3 (integer) c = 15 d = 4 result2 = c // d print(result2) # Output: 3 (integer)
- Modulus: % Returns the remainder of the division between the first number and the second number.
Example:
pythonCopy code
a = 10 b = 3 result = a % b print(result) # Output: 1
- Exponentiation: ** Raises the first number to the power of the second number.
Example:
pythonCopy code
a = 2 b = 3 result = a ** b print(result) # Output: 8
These arithmetic operators are fundamental in performing mathematical calculations and operations in Python. They can be used with both numerical values and strings, making Python a versatile language for various applications.
Comments: 0