All the cool Python tricks you need, GeoCities style!
Get ready for some calculations!
Operation | Description | Result |
---|---|---|
a = 3 b = 4 |
||
a + b |
Sum of a and b | (7) |
a - b |
Subtract b from a | (-1) |
a * b |
a times b | (12) |
a / b |
a divided by b | (0.75) |
a // b |
Integer part of a divided by b | (0) |
a % b |
Rest of a divided by b | (3) |
a ** b |
a to the power of b | (81) |
Pay attention to these!
Test | Description | Result |
---|---|---|
5 > 3 |
Tests if 5 is greater than 3 | True |
5 >= 3 |
Tests if 5 is greater than or equal to 3 | True |
5 == 3 |
Tests if 5 is equal to 3 | False |
5 != 3 |
Tests if 5 is different than 3 | True |
5 <= 3 |
Tests if 5 is lower than or equal to 3 | False |
5 < 3 |
Tests if 5 is lower than 3 | False |
not True |
Opposite of True | False |
Import module:
import math
Function | Description |
---|---|
math.ceil(x) |
Rounds x up |
math.floor(x) |
Rounds x down |
round(x) |
1 Rounds x with 0 decimal places |
round(x, 2) |
Rounds x with 2 decimal places |
math.sqrt(x) |
Square root of x |
math.sin(angle) |
Sine of angle |
math.cos(angle) |
Cosine of angle |
math.tan(angle) |
Tangent of angle |
math.sinh(x) |
Hyperbolic sine of x |
math.cosh(x) |
Hyperbolic cosine of x |
math.tanh(x) |
Hyperbolic tangent of x |
math.asin(angle) |
Arc sine of angle |
math.acos(angle) |
Arc cosine of angle |
math.atan(angle) |
Arc tangent of angle |
math.asinh(x) |
Inverse hyperbolic sine of x |
math.acosh(x) |
Inverse hyperbolic cosine of x |
math.atanh(x) |
Inverse hyperbolic tangent of x |
math.degrees(angle) |
Convert angle from radians to degrees |
math.radians(angle) |
Convert angle from degrees to radians |
math.factorial(x) |
Factorial of x |
math.gamma(x) |
Gamma function of x |
math.exp(x) |
e to the power of x |
math.log(x) |
Natural logarithm of x |
math.log(x, 2) |
Base 2 logarithm of x |
math.e |
Constant e |
math.pi |
Constant pi |
1 `round()` is not part of the `math` module.
2 The Python standard is to work with angles in radians.
# This script solves ax^2 + bx + c = 0
import math a = 1 b = -1 c = -6 delta = b**2 - 4*a*c r1 = (-b + math.sqrt(delta))/(2*a) r2 = (-b - math.sqrt(delta))/(2*a) print(f"r1 = {r1}") print(f"r2 = {r2}")
Results:
r1 = 3.0 r2 = -2.0
Check out this cool new section!
This section is still under construction, check back soon for more rad Python tips!