Show Menu
Cheatography

Python Cheat Sheet (DRAFT) by

test test test test test

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)
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.d­egr­ees­(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
Round is not part of the math module.
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 = b2 - 4 ac
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

Triangle Angles

# Calculates the angles of a triangle based on its
sides.
import math
side1, side2, side3 = 3, 4, 5
angle1 = math.atan(side2/side1)
angle2 = math.acos(side2/side3)
print(f"angle 1 = {math.degrees(angle1)}")
print(f"angle 2 = {math.degrees(angle2)}")
angle 1 = 53.130­102­354­15598
angle 2 = 36.869­897­645­84401
 

Data Types

Name
Type
Descri­ption
Integers
int
Whole numbers, such as: 3 300 200
Floating point
float
Numbers with a decimal point: 2.3 4.6 100.0
Strings
str
Ordered sequence of charac­ters: "­hel­lo" 'Sammy' "­200­0" "­楽しい­"
Lists
list
Ordered sequence of objects: [10,"he­llo­"­,200.3]
Dictio­naries
dict
Unordered Key:Value pairs: {"my­key­" : "­val­ue" , "­nam­e" : "­Fra­nki­e"}
Tuples
tup
Ordered immutable sequence of objects: (10,"he­llo­"­,200.3)
Sets
set
Unordered collection of unique objects: {"a",­"­b"}
Booleans
bool
Logical value indicating True or False