Python Fundamentals
January 23, 2026
A value is a literal such as a number or text.
Examples of values:
352.3 → float22 → int"Hello World!" → str
10) is an object in Python.= )# Here we assign the integer value 5 to the variable x.
x = 5
# Now we can use the variable x in the next line.
y = x + 12
y= means assignment:
Note
✅ In math, = often means “equal.”
✅ In Python, = means “store this value in the variable.”
#
# is ignored by the interpreter
The Type column shows Python’s official type name.
Mutable?
int (e.g., 10)float (e.g., 1.23)str (e.g., "hello")bool (e.g., True)NoneType (e.g., None)[] in Python[] to create a list[] to access an element by index{} in Python{} is used to denote a set or a dictionary{} for sets and dictionaries() in Python() for tuples() to pass arguments into functionscity_to_temp = {
"Paris": 28,
"London": 22,
"New York": 18,
"Seoul": 29,
"Rochester": 10
}
city_to_temp["Paris"] # 🔍 look up a value by key
city_to_temp["London"] = 32 # ✏️ update a value
city_to_temp.keys() # 🔑 all keys
city_to_temp.values() # 🌡️ all values
city_to_temp.items() # 🧾 (key, value) pairsBeing able to create empty containers is sometimes useful, especially when using loops (e.g., for, while).
Q. What is the type of an empty list?
int(), float(), str()if StatementsHere, both x and y are boolean.
Here, both x and y are variables.
==The == is an operator that compares the objects on either side and returns True if they have the same values
Q. What does not (not True) evaluate to?
True or False.if Statementsname = "Geneseo"
score = 99
if name == "Geneseo" and score > 90:
print("Geneseo, you achieved a high score.")
if name == "Geneseo" or score > 90:
print("You could be called Geneseo or have a high score")
if name != "Geneseo" and score > 90:
print("You are not called Geneseo and you have a high score")if statements.in Keyword: Membership Testin checks whether something exists inside a list, string, etc.
Q. Check if “a” is in the string “Wilson Ice Arena” using in. Is “a” in “Anyone”?
if-else Chainif Statements with infruits = ["apple", "banana", "cherry"]
favorite = "banana"
if favorite in fruits:
print(f"{favorite} is available!")
else:
print(f"{favorite} is not in the list.")in lets you check whether a value is present in a list, string, or other iterable.if-else structure.An f-string is a convenient way to create strings that include variable values directly inside the text.
✅ Key idea: Put an f before the quotation marks, then use {} to insert variables.
In Python, indentation is required for code blocks, such as code inside:
def ...),if / elif / else),for / while).Indentation is how Python knows which lines belong to a block.
It tells the interpreter what should run inside the block (e.g., inside an if) and what should run after the block ends.
Standard Python style is 4 spaces per indentation level.
Slicing methods can apply for strings, lists, and DataFrames.
With slicing methods, we can get subset of the data object.
Python is
letters = "abcdefghij"
letters[:] # whole string
letters[3:] # from index 3 to end
letters[:5] # from start to index 4
letters[:-4] # take the last 4 characters
letters[2:7] # index 2 to 6
letters[::2] # step size 2
letters[::-1] # reverse[start : end : step]
start is includedend is excludedstep controls how many characters to skiplen()len() tells you how many items/characters are stored
[index]Common built-in functions you will use often:
type() → data typelen() → lengthmax() → largest valuesum() → totaldef keyword.print("Cherry", "Strawberry", "Key Lime")
print("Cherry", "Strawberry", "Key Lime", sep = "!")
print("Cherry", "Strawberry", "Key Lime", sep=" ")To call a function, write its name followed by parentheses:
function_name(...)Inside the parentheses, you provide arguments (inputs), separated by commas.
A parameter is the name used in the function definition for an expected input
sep is a parameter of print().A default argument is the value used automatically if you do not specify it.
print(), the default separator is a space: sep = " ".while and for+=+= is a shortcut assignment operatorcount += 1 means the same thing as count = count + 1.whilecount = 1.count <= 5
True, run the loop body.count += 1.count <= 5 becomes False.stuff = input()
# Type something and press Return/Enter on Python Console
# before running print(stuff)
print(stuff)input() pauses the program and waits for the user to type something.breakwhile True:
user_input = input("Enter 'yes' to continue or 'no' to stop: ")
if user_input.lower() == 'no':
print("Exiting the loop. Goodbye!")
break
elif user_input.lower() == 'yes':
print("You chose to continue.")
else:
print("Invalid input, please enter 'yes' or 'no'.")While loop is used to execute a block of code repeatedly until given boolean condition evaluated to False.
while True creates an infinite loopbreakbreak exits the loop immediatelycontinuewhile True:
value = input("Integer, please [q to quit]: ")
if value == 'q': # quit
break
number = int(value)
if number % 2 == 0: # an even number
continue
print(number, "squared is", number*number)continue skips the rest of the loop body for the current iterationfor and infor loop when you want to go through each item in:
range())for Loop with breakbreak exits the loop immediatelyfor Loop with continuecontinue skips the current iteration and moves to the next onecontinue, pass, breakfor num in range(1, 6):
if num == 2:
continue # skip printing 2
if num == 3:
pass # do nothing, move on
if num == 4:
break # exit the loop
print(num)continue → skips to the next iterationpass → does nothing (useful as a placeholder)break → exits the loop completelyrange()range() creates a sequence of integers without storing a full listfor loopsrange( start, stop, step )
start defaults to 0step defaults to 1enumerate()enumerate() gives you two things while looping:
i)fruit)enumerate(iterable, start=0)
iterable can be a list, tuple, string, etc.start controls the first index (default is 0){key_expression: value_expression for item in iterable if condition}my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
filtered_dict = {k: v for k, v in my_dict.items() if v != 2}remove(): Deletes the first occurrence of value in the list.del statement: Deletes an item by index or a slice of items.try and exceptshort_list = [1, 2, 3]
positions = [0, 1, 5, 2] # 5 is out of range
for i in positions:
print(short_list[i])try and exceptshort_list = [1, 2, 3]
positions = [0, 1, 5, 2] # 5 is out of range
for i in positions:
try:
print(short_list[i])
except:
print("Index error:", i, "is not between 0 and", len(short_list) - 1)try to run code that might fail, and except to handle the error gracefully.
except block.except block.Python is a general-purpose programming language and is not specialized for numerical or statistical computation.
The core libraries that enable Python to store and analyze data efficiently are:
pandasnumpypandas
pandas provides Series and DataFrames which are used to store data in an easy-to-use format.numpy
numpy, numerical Python, provides the array block (np.array()) for doing fast and efficient computations;import statementA module is basically a bunch of related codes saved in a file with the extension .py.
A package is basically a directory of a collection of modules.
A library is a collection of packages
We refer to code of other module/package/library by using the Python import statement.
import statement with as or fromasas keyword when importing the module/package/library using its canonical names.pip tool