Python Unit 1 - Foundations

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/30

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 6:17 PM on 7/25/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

31 Terms

1
New cards

print() function

The primary function used to output text, numbers, or variable contents to the console screen.

Ex. print ("Hello, World!")

2
New cards

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

3
New cards

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

4
New cards

Variable Assignment

Storing a data value in memory using the assignment operator (=).

Ex. score = 100

playerName = "Alex"

5
New cards

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)

6
New cards

Integer (int)

whole number

Ex. x = 10 # int

7
New cards

float

Decimal numbers

Ex. pi = 3.14159 # float

8
New cards

String (str)

A sequence of characters within ""

Ex. greeting = "Hi" # str

9
New cards

Boolean (bool)

Represents true or false values.

Ex. is_active = True # bool

10
New cards

input () Function

Reads a string entered by the USER from the console.

Ex. user_name = input("Enter your name: ")

11
New cards

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

12
New cards

Arithmetic Operators

Addition (+)

Subtraction (-)

Multiplication (*)

Floating-Point Division (/) — always returns a float

13
New cards

Floating Point Division

Always returns a float

Ex. result = 10 / 2 # Returns 5.0 (float)

14
New cards

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

15
New cards

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

16
New cards

Exponential Operator (**)

Raises the left number to the power of the right number.

Ex. power = 2**3 # 2 raised to power 3 = 8

17
New cards

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)

18
New cards

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'

19
New cards

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)

20
New cards

String Slicing Format

The second number is always excluded

phrase = "Hello World"

print(phrase[0:5]) # 'Hello'

21
New cards

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.")

22
New cards

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.

23
New cards

Random Numbers (random module)

import random

24
New cards

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

25
New cards

random.random()

Returns the next random float in the range [0.0, 1.0). # 0 INCLUDED

26
New cards

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)

27
New cards

random.uniform(a, b)

picks a random float between a and b

28
New cards

datetime module and functions

from datetime import date, datetime, timedelta

29
New cards

datetime.now()

A method from the datetime module that returns a object containing the current local date and time.

30
New cards

date.today()

Returns a date object representing the current date.

31
New cards

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