Python Reference

Created by Brandon Concepcion

Table of Contents

  1. Python Reference
    1. Data Types
    2. Arithmetic Operators
    3. Variables and Expressions

Data Types

Data TypeDescriptionExamples
intRepresents integers: whole numbers that are positive or negative.42, -100, 0
floatRepresents decimal numbers (floating-point).3.14, -1.23, 0.0
strRepresents text (strings of characters)."Hello", 'Data'
boolRepresents boolean values (True or False).True, False

Arithmetic Operators

Numeric ExpressionDescriptionExamples
Addition (+)Adds two numbers together.2 + 3 would return 5

x + 10 adds 10 to the value of x
Subtraction (-)Subtracts the second number from the first.5 - 2 would return 3

x - 4 subtracts 4 from x
Multiplication (*)Multiplies two numbers.3 * 4 would return 12

x * 2 multiplies x by 2
Division (/)Divides the first number by the second, returns a floating-point result.10 / 2 would return 5.0

x / 4 divides x by 4
Floor Division (//)Divides the first number by the second, rounds down to the nearest integer.10 // 3 would return 3

x // 2 divides x and rounds down
Modulus (%)Returns the remainder of dividing the first number by the second.10 % 3 would return 1

x % 2 checks if x is even or odd (remainder)
Exponentiation (**)Raises the first number to the power of the second number.2 ** 3 would return 8

x ** 2 squares x
Unary Negation (-)Returns the negative of the number.-x returns the negative of x

-10 would return -10

Variables and Expressions

ConceptDescriptionExamples
ExpressionAny combination of values, variables, operators, and function calls that evaluates to a value.2 + 3, x * 10, len('hello')
VariableA name that refers to a value or an expression result. Can be reassigned to different values.x = 10

name = "Sid"
AssignmentThe process of assigning a value to a variable using the = operator.age = 25 assigns the value of 25 to the variable age

total = price * quantity sets the total variable equal to the product of the values in the price and quantity variable
Arithmetic ExpressionAn expression involving arithmetic operators (+, -, *, /, %, **, //).a + b, 3 * 4, 10 / 2, 2 ** 3
Comparison ExpressionAn expression that compares two values using operators (==, !=, >, <, >=, <=), and returns either True or Falsea == b check the equivalency of a and b

5 > 3 will return True

x != y checks if x is not equal to y, returning True if it is not

String ExpressionAn expression that manipulates strings with operators like + for concatenation or * for repetition."Hello" + " World", "a" * 3
Variable ReferenceReferring to a variable in an expression to use its value.x * 5 would return the value of x times 5

count - 1 would return the value of count minus 1
Compound ExpressionA combination of multiple expressions into one, possibly using parentheses for grouping.(a + b) * 3