xxxxxxxxxx
a_number = 3
the_complete_number = a_number + a_number #result should be 6
the_the_complete_number = a_number * a_numner #result should be 9
the_the_the_complete_number = a_number - a_number #result should be 0
xxxxxxxxxx
# Below follow the math operators that can be used in python
# ** Exponent
2 ** 3 # Output: 8
# % Modulus/Remaider
22 % 8 # Output: 6
# // Integer division
22 // 8 # Output: 2
# / Division
22 / 8 # Output: 2.75
# * Multiplication
3 * 3 # Output: 9
# - Subtraction
5 - 2 # Output: 3
# + Addition
2 + 2 # Output: 4
xxxxxxxxxx
#math in python: Multiple action
x = int(input("Type value for x: "))
y = int(input("Type value for y: "))
z = int(input("Type value for z: "))
print(x / y + z)
#you need multiple solution at once.
#Go ahead and copy the code to your
# .py script to see the results.
Python has a built-in math module that provides a set of functions for mathematical operations. Here are some commonly used math methods in Python:
abs(): Returns the absolute value of a number.
ceil(): Returns the smallest integer greater than or equal to a number.
floor(): Returns the largest integer less than or equal to a number.
round(): Returns the nearest integer to a number.
sqrt(): Returns the square root of a number.
pow(): Returns the value of a number raised to a specified power.
log(): Returns the natural logarithm of a number.
log10(): Returns the base-10 logarithm of a number.
exp(): Returns the value of e raised to the power of a specified number.
cos(): Returns the cosine of a number in radians.
sin(): Returns the sine of a number in radians.
tan(): Returns the tangent of a number in radians.
acos(): Returns the inverse cosine of a number in radians.
asin(): Returns the inverse sine of a number in radians.
atan(): Returns the inverse tangent of a number in radians.
Here's an example of using some of these methods:
xxxxxxxxxx
import math
# Absolute value
x = -5
print(abs(x))
# Ceil and floor
y = 3.7
print(math.ceil(y))
print(math.floor(y))
# Round
z = 2.6
print(round(z))
# Square root and power
a = 16
print(math.sqrt(a))
print(pow(a, 2))
# Logarithm
b = 10
print(math.log(b))
print(math.log10(b))
# Exponential
c = 2
print(math.exp(c))
# Trigonometric functions
angle = math.pi / 4
print(math.cos(angle))
print(math.sin(angle))
print(math.tan(angle))
print(math.acos(math.sqrt(2)/2))
print(math.asin(math.sqrt(2)/2))
print(math.atan(1))
This will output:
5
4
3
3
4.0
256
2.302585092994046
1.0
7.3890560989306495
0.7071067811865476
0.7071067811865475
0.9999999999999999
0.7853981633974484
0.7853981633974483
0.7853981633974483
xxxxxxxxxx
import math
# Basic operations
addition = 2 + 3
subtraction = 5 - 1
multiplication = 4 * 2
division = 10 / 3 # Returns a float
# Advanced operations
power = pow(2, 3) # Calculates 2^3
square_root = math.sqrt(16)
absolute_value = abs(-7)
rounding = round(4.7)
# Trigonometry
sine = math.sin(math.radians(45))
cosine = math.cos(math.radians(60))
tangent = math.tan(math.radians(30))
# Constants
pi = math.pi
euler_number = math.e
# More complex functions available in the math module
print(f"Addition: {addition}")
print(f"Subtraction: {subtraction}")
print(f"Multiplication: {multiplication}")
print(f"Division: {division}")
print(f"Power: {power}")
print(f"Square Root: {square_root}")
print(f"Absolute Value: {absolute_value}")
print(f"Rounding: {rounding}")
print(f"Sine: {sine}")
print(f"Cosine: {cosine}")
print(f"Tangent: {tangent}")
print(f"Pi: {pi}")
print(f"Euler's number: {euler_number}")
xxxxxxxxxx
x = int(10)
y = int(5)
print(x + y)
#you need to define integer extra otherwise
# python will count this as a str and won't work.
#copy the code to your py script to see the accurate result.