Quick Reference: Variables & Data Types

Module 2 • Introduction to Python

Variables & Assignment

name = "Alice"        # string
age = 25              # integer
gpa = 3.85            # float
is_student = True     # boolean

# Multiple assignment
a, b, c = 1, 2, 3

# Reassignment
x = 10
x = x + 5            # x is now 15
x += 5                # shorthand, x is now 20

Naming Rules

RuleExample
Start with letter or _name, _count
Letters, numbers, _ onlyitem2, my_var
No spacesfirst_name (not first name)
Case-sensitiveNamename
No reserved wordsCannot use if, class, etc.
Convention: use snake_case for variables.

Arithmetic Operators

OperatorNameExampleResult
+Addition5 + 38
-Subtraction10 - 46
*Multiply6 * 742
/Division15 / 43.75
//Int Division15 // 43
%Modulo15 % 43
**Exponent2 ** 38
/ always returns a float! 10 / 2 = 5.0

Data Types

TypeExampleDescription
int42, -7Whole numbers
float3.14, 2.0Decimal numbers
str"hello"Text
boolTrue, FalseBoolean
NoneTypeNoneNo value
type(42)       # <class 'int'>
type(3.14)     # <class 'float'>
type("hello")  # <class 'str'>
type(True)     # <class 'bool'>

String Basics

# Single or double quotes
msg = "Hello"
msg = 'Hello'

# Escape characters
\n  newline    \t  tab
\\  backslash  \'  single quote
\"  double quote

# Operations
"Hi " + "there"      # "Hi there"
"Go! " * 3           # "Go! Go! Go! "
len("Python")        # 6

# Indexing (zero-based)
word = "Python"
word[0]   # 'P'
word[-1]  # 'n'

Type Conversion

int("42")         # 42
float("3.14")     # 3.14
str(42)           # "42"
bool(1)           # True
int(float("3.7")) # 3
int() truncates, does NOT round. int(9.9) = 9

Truthy / Falsy

FalsyTruthy (everything else)
0, 0.0Any non-zero number
"" (empty string)Any non-empty string
None"0", " ", etc.
FalseTrue

Useful Functions

abs(-15)            # 15
round(3.14159, 2)   # 3.14
max(10, 20, 5)      # 20
min(10, 20, 5)      # 5