2 + 5 * 842
Before we jump into analyzing data, let’s talk about one of the most powerful tools in programming: variables. Think of a variable like a storage box where you can put information that you might want to use later. This is how we make computers remember things for us!
Let’s start with something simple. Imagine using Python like a calculator. You can do quick math, like this:
This is very handy, but let’s be honest, it’s not very exciting. What makes programming powerful is the ability to store this result or any other data, and use it again whenever we need it. That’s where variables come in.
A variable is a name that refers to a value. In the previous example, we saw that we can use Python as an advanced calculator. However, the results of these computations are lost after we perform our operations. If we want to save and reuse any value in a code, we need to save these computations in a place in computer memory to be able to access it in the future.
Variables are names that we associate with a memory location in computers. As such variables are not values themselves but they point to a memory location where the actual values are stored.
Now, Python will remember that weight_kg equals 60. Instead of doing the math all over again or writing the number repeatedly, we just use weight_kg whenever we need it.
A Few Rules for Naming Variables: - You can use letters, digits, and **underscores (_), but the name can’t** start with a digit. - Variable names are case-sensitive, so weight and Weight are two different things.
For example: - weight0 is valid, but 0weight isn’t. - weight_kg is different from Weight_kg.
Python can work with different types of data. Three of the most common types are:
For example, let’s say we want to store a patient’s weight. If the weight is exactly 60 kilograms, we can use an integer:
But if we want to be more precise, like 60.3 kilograms, we can use a floating point number:
Here, weight_kg is a variable that stores a decimal number, or a float. It could represent any weight in kg measurement, which we might later use for calculations or comparisons.
We can also store text:
Strings can store text data, which can be useful for labeling or adding metadata.
And lastly, we have booleans, which are used to represent true or false values. They are helpful in logical operations or conditions:
With this variable, we could decide whether to apply certain calculations or adjustments based on the height. It can help us write cleaner, more conditional code.
Addition is one of the simplest operations. If you want to increase a value, you just add to it. For example, if you have a population measurement of 20000 and want to add 5000 more, Python gives you a new population number of 25000 like this:
population = 20000 # current population of a town
new_population = population + 5000 # population increase after migration
new_population25000
Other operations are just as simple. If you want to double a temperature value, you multiply it by 2 like this:
Division also works the same way: dividing 20000 by 100 gives 200.0 like this:
Python can also handle exponents, like squaring numbers. If you want to find what 20000 times 20000 is, you can get it this way:
This is useful for calculating things like squared distances or other exponential relationships.
### Exercise 1: You are studying urban growth in a geography class. City A had a population of 1,500,000 people in the year 2000, and it grew to 2,100,000 people by the year 2020.
Your task is to write a Python program that: - Stores the population values in variables. - Calculates how much the population has grown. - Displays the result using print().
With strings, one common thing we do is combine them, known as concatenation. Imagine you have two words, first name and last name, like “John” and “Doe”, and you want to make them into one full name: “John Doe.” You do this by placing them side by side with a plus sign between them, and Python combines them into one.
Another thing you can do with strings is repeat them. For instance, if you have the word “hello,” you could repeat it multiple times, like “hello hello hello” In Python, you simply tell it to multiply the word by the number of times you want it to appear like this:
Booleans in Python are very straightforward: they represent only two options, True or False. They’re useful when you’re asking questions or checking if something meets certain conditions.
For example, let’s say you want to check if a town’s population is above a certain number. Imagine the town has a population of 20,000 people, and you want to know if it’s larger than 10,000:
population = 20000 # current population of a town
is_large_town = population > 10000 # check if it's considered a large town
is_large_townTrue
We can also ask Python to check multiple conditions at once. Maybe you want to know if a town has both a large population (over 10,000 people) and a low unemployment rate (under 5%). You can combine these two questions into one. If both are true, Python returns True; if either one isn’t, it returns False.
Sometimes, Python lets us mix different types in ways that make sense. For example, in Python, True is treated as 1 and False as 0. So if you’re working with a number and a condition, Python might add 1 if the condition is True, or add nothing if it’s False. This can be handy for quick adjustments, like slightly increasing a value if a condition is met.
adjusted_population = population + is_low_unemployment adjusted_population
These simple operations let you do quite a bit in Python. With strings, you can create descriptive phrases; with numbers, you can add, multiply, and do more complex math; with booleans, you can check conditions and make decisions. This flexibility is what makes Python both powerful and beginner-friendly—it’s easy to write code that mimics real-world problem-solving, step by step.
Data structures are essential for organizing and managing data in programming. In Python, we have several types of data structures, but three of the most commonly used ones are lists, tuples, and dictionaries. Each of these has unique characteristics and uses that make them suitable for different tasks.
A list in Python is a collection of items that can be modified. This means you can add, remove, or change items in a list as needed. Lists are defined using square brackets []:
In this example, we have a list called grades that stores test scores from different students.
1. Accessing Lists: Each item in a list has a position called an index, starting from 0. You can access any item using its index:
2. Modifying Items: Lists are mutable, meaning you can change their contents:
3. Adding Items: You can add new items to the end of a list using the append() method:
4. Removing Items: You can remove items from a list using the remove() method or the pop() method:
Consider a scenario where you are recording student test scores for a class. You can store these scores in a list. As new test results come in, you can easily update the list by adding new scores or modifying existing ones.
philosophers = ["Plato", "Aristotle", "Confucius", "Socrates"]
# Add "Descartes" to the list
philosophers.append("Descartes")
# Remove "Confucius" from the list
# Option 1:
# philosophers.remove("Confucius")
# Option 2 (remove by index):
# philosophers.pop(2)
print(philosophers)['Plato', 'Aristotle', 'Confucius', 'Socrates', 'Descartes']
A tuple is a collection of items, similar to a list, but it is immutable. This means that once a tuple is created, it cannot be changed. Tuples are defined using parentheses ().
This tuple represents the geographic coordinates of a specific location.
1. Accessing Items: Just like lists, you can access items in a tuple using their index:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[68], line 1 ----> 1 coordinates[0] = 48.5 TypeError: 'tuple' object does not support item assignment
2. Benefits of Immutability: Since tuples cannot be changed, they are useful for storing fixed data that should not be altered. This can help prevent accidental changes to important values.
In oceanography, you might want to store the coordinates of a specific research station. Since these coordinates are fixed and won’t change, a tuple is the ideal choice. If you need to reference these coordinates later in your code, you can do so confidently, knowing they will not change.
A dictionary in Python is a collection of key-value pairs, similar to how a real dictionary has words (keys) and their meanings (values). Each key in a dictionary must be unique, and you can use it to access its corresponding value. Dictionaries are defined using curly braces {}.
student_data = {
"name": "Alex Johnson",
"age": 20,
"grade": 88.5,
"major": "Computer Science"
}
student_data {'name': 'Alex Johnson', 'age': 20, 'grade': 88.5, 'major': 'Computer Science'}
In this example, we have a dictionary called student_data that stores various pieces of information about a student’s academic details.
1. Accessing Values: You can retrieve a value using its corresponding key.
2. Adding or Modifying Values: You can easily add new key-value pairs or modify existing ones:
{'name': 'Alex Johnson', 'age': 20, 'grade': 92.0, 'major': 'Computer Science'}
{'name': 'Alex Johnson',
'age': 20,
'grade': 92.0,
'major': 'Computer Science',
'attendance': 95}
3. Removing Key-Value Pairs: You can remove a key-value pair using the del statement:
countries = {
"United States": "English",
"Mexico": "Spanish",
"France": "French",
"Canada": "English and French"
}
print(countries)
# Add Brazil
countries["Brazil"] = "Portuguese"
print(countries)
# Remove France using del
del countries["France"]
print(countries){'United States': 'English', 'Mexico': 'Spanish', 'France': 'French', 'Canada': 'English and French'}
{'United States': 'English', 'Mexico': 'Spanish', 'France': 'French', 'Canada': 'English and French', 'Brazil': 'Portuguese'}
{'United States': 'English', 'Mexico': 'Spanish', 'Canada': 'English and French', 'Brazil': 'Portuguese'}
Selecting the appropriate data structure is crucial. Here’s a quick guide:
Use Lists when you need to store a collection of items that you might want to change or add to, like student grades over multiple subjects or assignments.
Use Tuples for fixed data points that won’t change, such as geographic coordinates of research locations.
Use Dictionaries when you need to associate related information together, such as all academic details for a specific student.
Choosing the right data structure will make your data management more efficient and your code easier to understand.
Control structures are essential programming constructs that allow us to dictate the flow of our program. In Python, the most commonly used control structures include loops (for and while) and conditionals (if and else). These structures enable us to repeat actions, make decisions based on conditions, and manage how we process data.
Conditionals allow us to execute certain blocks of code based on specific conditions. This means that our program can make decisions and act differently depending on the situation. The most common conditional statements in Python are if, elif, and else.
Here’s a simple structure for using conditionals:
if condition: # Code to execute if the condition is true elif another_condition: # Code to execute if the first condition is false and this one is true else: # Code to execute if both conditions are false
Comparison operators allow us to compare two values. Here are the most commonly used comparison operators in Python:
==: Equal to (checks if two values are the same) !=: Not equal to (checks if two values are different) >: Greater than (checks if the left value is larger than the right) <: Less than (checks if the left value is smaller than the right) >=: Greater than or equal to (checks if the left value is larger than or equal to the right) <=: Less than or equal to (checks if the left value is smaller than or equal to the right)
Let’s say we want to categorize towns based on their population size:
population = 20000
if population < 5000:
print("Small town")
elif 5000 <= population <= 20000:
print("Medium-sized town")
else:
print("Large town")Medium-sized town
In this example:
Logical operators allow us to combine multiple conditions. The most commonly used logical operators are:
Let’s say we want to check if someone’s annual income falls within the middle-income bracket:
annual_income = 45000
if annual_income >= 30000 and annual_income <= 70000:
print("Middle-income bracket")
else:
print("Outside middle-income bracket")Middle-income bracket
In this example:
Loops allow us to repeat a block of code multiple times. This is especially useful when we have to process a collection of items, like a list of temperature readings or salinity measurements. The two primary types of loops in Python are for loops and while loops.
A for loop is used to iterate over a sequence (like a list or a tuple). Here’s the basic structure:
for item in sequence: # Code to execute for each item
Let’s say we want to print out each survey response score from a list:
survey_scores = [3, 4, 5, 2, 4]
for score in survey_scores:
print(f"Survey score: {score} out of 5")
scoreSurvey score: 3 out of 5
Survey score: 4 out of 5
Survey score: 5 out of 5
Survey score: 2 out of 5
Survey score: 4 out of 5
4
In this example, the loop goes through each item in the survey_scores list and prints it.
For loops are useful for analyzing data, such as calculating the average monthly income from several months:
monthly_incomes = [3000, 3200, 3100, 2900, 4000, 3500, 2500, 2900, 3050, 3200, 3500, 2950]
total_income = 0
for income in monthly_incomes:
total_income += income
average_income = total_income / len(monthly_incomes)
print(f"Average Monthly Income: ${average_income:.2f}")Average Monthly Income: $3150.00
In this case, the loop adds up all the monthly incomes and then finds the average by dividing by the number of months.
populations = [2731, 631, 1135, 934, 1705]
total_population = 0
for pop in populations:
total_population += pop
average_population = total_population / len(populations)
print("Average population (in thousands):", round(average_population))Average population (in thousands): 1427
A while loop continues to execute as long as a specified condition is true. The basic structure looks like this:
while condition: # Code to execute while the condition is true
Let’s say we want to keep recording monthly expenses until we have data for five months:
monthly_expenses = []
max_months = 5
while len(monthly_expenses) < max_months:
new_expense = float(input("Enter monthly expense amount: "))
monthly_expenses.append(new_expense)
print("Monthly expenses collected:", monthly_expenses)--------------------------------------------------------------------------- StdinNotImplementedError Traceback (most recent call last) Cell In[80], line 5 2 max_months = 5 4 while len(monthly_expenses) < max_months: ----> 5 new_expense = float(input("Enter monthly expense amount: ")) 6 monthly_expenses.append(new_expense) 8 print("Monthly expenses collected:", monthly_expenses) File /usr/lib/python3.13/site-packages/ipykernel/kernelbase.py:1281, in Kernel.raw_input(self, prompt) 1279 if not self._allow_stdin: 1280 msg = "raw_input was called, but this frontend does not support input requests." -> 1281 raise StdinNotImplementedError(msg) 1282 return self._input_request( 1283 str(prompt), 1284 self._parent_ident["shell"], 1285 self.get_parent("shell"), 1286 password=False, 1287 ) StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
In this example, the loop keeps asking for monthly expenses until it has collected five amounts.
While loops are useful for continuous data collection, like tracking monthly donations until reaching a target amount:
donations = 0
while donations < 5000:
donations += 500
print(f"Total Donations Collected: ${donations}")Total Donations Collected: $500
Total Donations Collected: $1000
Total Donations Collected: $1500
Total Donations Collected: $2000
Total Donations Collected: $2500
Total Donations Collected: $3000
Total Donations Collected: $3500
Total Donations Collected: $4000
Total Donations Collected: $4500
Total Donations Collected: $5000
Often, we will need to use both loops and conditionals together to process data effectively. For example, you might want to analyze temperature readings and categorize them within a loop.
Let’s categorize a list of temperature readings as “Cold,” “Warm,” or “Hot”:
temperature_readings = [10.0, 20.5, 25.0, 30.5]
for temperature in temperature_readings:
if temperature < 15:
print(f"Temperature {temperature} C: Cold")
elif 15 <= temperature <=25:
print(f"Temperature {temperature} C: Warm")
else:
print(f"Temperature {temperature} C: Hot")Temperature 10.0 C: Cold
Temperature 20.5 C: Warm
Temperature 25.0 C: Warm
Temperature 30.5 C: Hot
In this example, the loop goes through each temperature reading, and the conditional statements categorize each temperature accordingly.
Now, let’s say we want to use a loop to analyze data stored in a dictionary. We have a dictionary of historical events and the years they happened. Using a for loop, we can go through each event and year, and use a control structure like an if statement to check whether the event happened before or after 1900.
events = {
"Signing of the Magna Carta": 1215,
"French Revolution": 1789,
"Moon Landing": 1969,
"Canadian Confederation": 1867,
"Fall of the Berlin Wall": 1989
}
for event, year in events.items():
print(event)
if year < 1900:
print("This event happened before 1900.\n")
else:
print("This event happened in or after 1900.\n")Signing of the Magna Carta
This event happened before 1900.
French Revolution
This event happened before 1900.
Moon Landing
This event happened in or after 1900.
Canadian Confederation
This event happened before 1900.
Fall of the Berlin Wall
This event happened in or after 1900.
You are given a dictionary of universities worldwide with their average graduation rates (in percentages):
universities = { “Harvard University”: 98,
"University of Oxford": 92,
"Stanford University": 94,
"Massachusetts Institute of Technology (MIT)": 92,
"University of Cambridge": 93,
"University of Tokyo": 70,
"University of Melbourne": 79,
"University of Cape Town": 68,
"University of São Paulo": 65,
"National University of Singapore": 80
}
Task:
universities = {
"Harvard University": 98,
"University of Oxford": 92,
"Stanford University": 94,
"Massachusetts Institute of Technology (MIT)": 92,
"University of Cambridge": 93,
"University of Tokyo": 70,
"University of Melbourne": 79,
"University of Cape Town": 68,
"University of São Paulo": 65,
"National University of Singapore": 80
}
for university, rate in universities.items():
print(university + " has a graduation rate of " + str(rate) + "%.")
if rate >= 90:
print("Excellent graduation rate\n")
elif 75 <= rate < 90:
print("Good graduation rate\n")
else:
print("Needs improvement\n")Harvard University has a graduation rate of 98%.
Excellent graduation rate
University of Oxford has a graduation rate of 92%.
Excellent graduation rate
Stanford University has a graduation rate of 94%.
Excellent graduation rate
Massachusetts Institute of Technology (MIT) has a graduation rate of 92%.
Excellent graduation rate
University of Cambridge has a graduation rate of 93%.
Excellent graduation rate
University of Tokyo has a graduation rate of 70%.
Needs improvement
University of Melbourne has a graduation rate of 79%.
Good graduation rate
University of Cape Town has a graduation rate of 68%.
Needs improvement
University of São Paulo has a graduation rate of 65%.
Needs improvement
National University of Singapore has a graduation rate of 80%.
Good graduation rate