Show Menu
Cheatography

Python Basics - Math Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Math Operations

a = 3
b = 4
a + b
Sum 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)

Logic Tests

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)

Math Module

import math
Imports module math
math.c­eil(x)
Rounds x up
math.f­loor(x)
Rounds x down
round(x)1
Rounds x with 0 decimal places
round(x, 2)
Rounds x with 2 decimal places
math.s­qrt(x)
Square root of x
math.s­in(­angle)
Sine of angle
math.c­os(­angle)
Cosine of angle
math.t­an(­angle)
Tangent of angle
math.s­inh(x)
Hiperbolic sine of x
math.c­osh(x)
Hiperbolic cosine of x
math.t­anh(x)
Hiperbolic tangent of x
math.a­sin­(angle)
Arc sine of angle
math.a­cos­(angle)
Arc cosine of angle
math.a­tan­(angle)
Arc tangent of angle
math.a­sinh(x)
Inverse hiperbolic sine of x
math.a­cosh(x)
Inverse hiperbolic cosine of x
math.a­tanh(x)
Inverse hiperbolic tangent of x
math.degrees(angle)
Covert rad_angle from radians to degrees
math.r­adi­ans­(angle)
Covert rad_angle from degrees to radians
math.f­act­ori­al(x)
Factorial of x
math.g­amma(x)
Gamma function of x
math.e­xp(x)
e to the power of x
math.l­og(x)
Natural logarithm of x
math.l­og(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

Second Degree Equation Roots

# This script solves ax^2 + bx + c = 0
import math

a = 1
b = -1
c = -6

delta = b*2 - 4a*c
r1 = (-b + math.sqrt(delta))/(2*a)
r2 = (-b - math.sqrt(delta))/(2*a)

print(f"r1 = {r1}")
print(f"r2 = {r2}")
r1 = 3.0
r2 = -2.0