1/30
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
print() function
The primary function used to output text, numbers, or variable contents to the console screen.
Ex. print ("Hello, World!")
Escape characters
Special non-printable characters prefixed with a backslash () used inside strings to format text (e.g., \n for a new line, \t for a tab).
Ex. print("Line 1\nLine 2") # Prints on two separate lines
print("Name:\tAlice") # Inserts a tab space
end parameter
end defines what is printed at the very end of the line (default is newline \n).
Ex. print("Loading", end="…") # Doesn't jump to a new line
Variable Assignment
Storing a data value in memory using the assignment operator (=).
Ex. score = 100
playerName = "Alex"
Primitive Data Types
The fundamental built-in data types:
int: Whole numbers (e.g., 42)
float: Decimal numbers (e.g., 3.14)
str: Text wrapped in quotes (e.g., "Python")
bool: Truth values (True or False)
Integer (int)
whole number
Ex. x = 10 # int
float
Decimal numbers
Ex. pi = 3.14159 # float
String (str)
A sequence of characters within ""
Ex. greeting = "Hi" # str
Boolean (bool)
Represents true or false values.
Ex. is_active = True # bool
input () Function
Reads a string entered by the USER from the console.
Ex. user_name = input("Enter your name: ")
type casting (conversion)
Converting a value from one data type to another using built-in functions like int(), float(), str(), or bool().
Ex. age_str = input("Enter age: ") # Returns "25" (str)
age_num = int(age_str) # Converts to 25 (int)
next_year = age_num + 1 # 26
Arithmetic Operators
Addition (+)
Subtraction (-)
Multiplication (*)
Floating-Point Division (/) — always returns a float
Floating Point Division
Always returns a float
Ex. result = 10 / 2 # Returns 5.0 (float)
Floor Division (//)
Divides two numbers and rounds the result down to the nearest integer, discarding the decimal portion. (INTEGER)
Ex. print(7 // 2) # Outputs: 3
print(-7 // 2) # Outputs: -4
Modulus Operator (%)
Returns the remainder after integer division. Frequently used to check if a number is even/odd or divisible by another number.
Ex. print(10 % 3) # Outputs: 1
print(8 % 2) # Outputs: 0
Exponential Operator (**)
Raises the left number to the power of the right number.
Ex. power = 2**3 # 2 raised to power 3 = 8
Math Module
Functions such as math.sqrt(), math.ceil(), and math.floor().
Ex. import math print(math.sqrt(16)) # 4.0
print(math.ceil(4.1)) # 5 (rounds up)
print(math.floor(4.9)) # 4 (rounds down)
String Indexing
Accessing individual characters in a string by position, starting from index 0. Negative indexing accesses characters from the end, starting at -1.
Ex. text = "Python"
print(text[0]) # 'P'
print(text[-1]) # 'n'
String Slicing ( random.randrange) ([start:stop:step])
Extracting a portion of a string. Starts at start, stops before stop, moving by step increments.
Ex. phrase = "Hello World"
print(phrase[0:5]) # 'Hello'
print(phrase[:: -1]) # 'dlroW olleH' (reverses string)
String Slicing Format
The second number is always excluded
phrase = "Hello World"
print(phrase[0:5]) # 'Hello'
String Formatting (f-strings)
Formatted string literals prefixed with f allow expressions inside {} to be evaluated within the string.
Ex. name = "Sam" score = 95
print(f"Player {name} scored {score} points.")
Built-in String Methods
.lower() / .upper(): Changes all letters to uppercase.
.count(): counts the number of occurrences of the given text
.replace(old, new): Replaces occurrences of a substring.
.find(): searches the string for the given text and returns the first place it finds it or -1 if it doesn't.
Random Numbers (random module)
import random
random.randint(a, b)
Generates a random integer (both endpoints a and b are inclusive).
Ex. import random
dice_roll = random.randint(1, 6) # Generates 1, 2, 3, 4, 5, or 6
random.random()
Returns the next random float in the range [0.0, 1.0). # 0 INCLUDED
random.choice(sequence)
Selects and returns a single random element from a sequence (like a list or tuple).
Ex. import random
colors = ["red", "green", "blue"]
pick = random.choice(colors)
random.uniform(a, b)
picks a random float between a and b
datetime module and functions
from datetime import date, datetime, timedelta
datetime.now()
A method from the datetime module that returns a object containing the current local date and time.
date.today()
Returns a date object representing the current date.
strftime() method
Formats a datetime object into a custom text string using directive codes.
Ex. from datetime import datetime now = datetime.now()
formatted = now.strftime("%B %d, %Y - %H:%M")
print(formatted) # e.g., July 25, 2026 - 12:48