1/121
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Compiler
A compiler translates the whole source program into another form, often machine code or bytecode, before the program runs. After compilation, the resulting program can usually run faster because the translation is already done.
Interpreter
An interpreter executes code step by step. Python is commonly described as interpreted because code can be run interactively and executed line by line by the python runtime.
Translation
Compiler: Usually translates the entire program before execution
Interpreter: Executes statements as the program runs
Error Discovery
Compiler: May report many syntax errors before running
Interpreter: May stop when it reaches the error
Speed
Compiler: Often faster after compilation
Interpreter: often slower because execution includes interpretation
Python Identifier Rules
Can contain letters, digits, and underscores
cannot start with a digit (number)
cannot contain spaces or symbols like @, %, -, or $
cannot be a python keyword such as if, else, for, import, class, or while
identifiers are case-sensitive: name, Name, and NAME are different
Comments
a comment is ignored by Python and is used to explain code
Single, double, and triple quotes
Single quotes and double quotes both create strings. Triple quotes are useful for multi-line strings
Escape Sequences
An escape sequence begins with a backlash. The exam review specifically mentions the difference between n and \n
n
Meaning: just the letter n
Example output: HellonWorld
\n
Meaning: Newline character
Example output: Hello then World on the next line
\t
Meaning: Tab character
Ex: Adds spacing like a tab
\\
Meaning: Literal backslash
Ex: Prints one backslash
+
addition (10+3=13)
-
Subtraction
*
Multiplication
/
Regular division (10/3=3.333)
//
Floor division 10//3=3
%
Modulus/Remainder (10%3=1)
**
Exponent (2**3=8)
==
Equal to
!=
Not equal to
>
greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
and
Meaning: Both conditions must be true
Ex: age > 18 and age < 30
or
Meaning: At least one condition must be true
Ex: x == 1 or x == 2
not
Meaning: reverses the Boolean value
Ex: not True is False
string
Key idea: text, indexed and sliceable
Ex: “hello”
integer
Key idea: Whole number
Ex: 20
float
Key idea: Decimal
Ex: 19.99
Boolean
Key idea: represent truth values
Ex: True/False
list
Key idea: Ordered and Mutable
Ex: [1,2,3]
tuple
Key idea: Ordered and immutable
Example: (1,2,3)
dictionary
Key idea: key value pairs
Ex: {“name”: “Chris”}
set
Key idea: Unordered, no duplicates; can contain different data types
Ex: {1,2,3}
List vs. Tuple
A list can be changed after it is created. A tuple cannot be changed after its created
Indexing
Python Indexing starts at 0. Negative indexing starts from the end
Slicing
Slice format is [start:stop:step]. The start index is included, but the stop index is not included.
Dictionary
A dictionary stores data as key value pairs. You use the key to access the value
keys()
returns all keys
ex: student.keys()
values()
returns all values
Ex: student.values()
items()
returns key value pairs
Ex: student.items()
update()
Updates or adds data
Ex: student.update({"age": 21})
pop()
Removes a key and returns its value
student.pop(“age”)
setdefault()
Gets value if key exists; otherwise inserts default
Ex: student.setdefault(“school”. “UMB”)
if, elif, and else
Conditional statements allow a program to make decisions. Python checks the conditions from top to bottom and stops after the first true condition.
Order Matters
if a condition is true, then python skips the remaining elif and else blocks
os
Work with directories, paths, and the operating systems
sys
interact with the Python runtime; common example is sys.exit()
Import and Modules
the import keyword loads code from another module so you can use its functions, constants, or classes
pprint
pretty-print complex structures like dictionaries
shutil
Higher-level file operations such as copying files
random
Generate random numbers
for Loops
A for loop repeats code for each item in a sequence, such as a list, string, or range
range()
generates an immutable sequence of numbers
range(5)
0 through stop
values produed: 0,1,2,3,4
range(1, 5)
start through stop
1,2,3,4
range(1, 10, 2)
start, stop, step
1,3,5,7,9
break
stops the loop completely
continue
skips the rest of the current iteration and moves to the next one
Strings
immutable sequences of characters
len(s)
returns length
len(“abc”) is 3
s.upper()
Uppercase copy
Original string is not changed unless reassigned
s.lower()
Lowercase copy
useful for case-insensitive checks
s.find()
first index of x
returns -1 if not found
s.rfind()
last index of x
searches from the right
s.index(x)
first index of x
raises ValueError if not found
s.startswith(x)
checks beginning
returns true or false
s.endswith(x)
checks ending
returns True or False
s.split(sep)
Splits string into list
Default split separates on whitespace
sep.join(list)
Joins list into string
the separator goes between items
Random Module
The random module generates pseudo-random values
random.random()
Returns: Float from 0.0 up to but not including 1.0.
Ex: 0.72834
random.uniform(a, b)
Returns: Float between a and b
random.uniform(1,10)
random.randint(a,b)
returns: integer from a through b, including both endpoints
Ex: random.randint(1, 10) can return 1 or 10.
OS Module
is central for cybersecurity scripting because scripts often need to inspect folders, check file types, build paths, and collect evidence files
os.chdir(path)
Changes the current working directory.
os.getcwd()
Returns the current working directory
os.listdir(path)
Lists files and folders inside a directory
os.path.getsize(path)
Returns size in bytes.
os.path.isfile(path)
True if path is a file
os.path.isdir(path)
True if path is a directory.
os.path.exists(path)
True if path exists.
os.path.basename(path)
Returns final file/folder name from a path
os.path.dirname(path)
Returns the directory portion of a path.
os.path.relpath(path)
Returns a relative path.
os.path.abspath(path)
Returns an absolute/full path.
os.path.join(a, b, ...)
Safely combines path parts.
os.mkdir(path)
Creates one directory level.
os.makedirs(path)
Creates nested directories.
Windows File Path Separator
Backslash \
C:\Users\Chris\Desktop\file.txt
Linux/macOS File Path Separator
Forward slash /
/home/chris/Desktop/file.txt
File pointer
is the current position inside a file. Reading and writing start from the pointer position
r (read)
pointer position: Beginning
reads an existing file
w (write)
pointer position: beginning after file is erased
overwrites existing file or creates a new file
a (append)
pointer position: end
appends new data to the end
Binary File Modes
are used for non text files such as images, PDFs, executables, disk images, and other forensic evidence files
rb (read binary)
open image/evidence file for reading