Introductory Programming with Python

Variables

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:

#your code goes here

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.

What Is a Variable?

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.

#your code goes here

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.

Types of Data in Python

Python can work with different types of data. Three of the most common types are:

  • Integers (whole numbers like 1, 2, 100)
  • Floating point numbers (numbers with decimals like 2.5, 60.3)
  • Strings (text like “Hello”, “001”)
  • Booleans (True or False)

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:

#your code goes here

But if we want to be more precise, like 60.3 kilograms, we can use a floating point number:

#your code goes here

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:

#your code goes here

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:

#your code goes here

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.

Basic Operations with Variables

Working with Numbers: Integers and Floats

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:

#your code goes here

Other operations are just as simple. If you want to double a temperature value, you multiply it by 2 like this:

#your code goes here

Division also works the same way: dividing 20000 by 100 gives 200.0 like this:

#your code goes here

Python can also handle exponents, like squaring numbers. If you want to find what 20000 times 20000 is, you can get it this way:

#your code goes here

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

#your code goes here

Working with Strings

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.

#your code goes here

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:

#your code goes here

Working with Booleans

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:

#your code goes here

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.

#your code goes here

Mixing Types Together

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.

#your code goes here

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

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.

Lists

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 []:

#your code goes here  

In this example, we have a list called grades that stores test scores from different students.

Using Lists:

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:

#your code goes here

2. Modifying Items: Lists are mutable, meaning you can change their contents:

#your code goes here

3. Adding Items: You can add new items to the end of a list using the append() method:

#your code goes here

4. Removing Items: You can remove items from a list using the remove() method or the pop() method:

#your code goes here
#your code goes here

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.

Exercise 2

  • Create a list named philosophers that contains these names as strings: “Plato”, “Aristotle”, “Confucius”, and “Socrates”.
  • Add the philosopher “Descartes” to the end of the list.
  • Remove “Confucius” from the list.
  • Print the updated list to see the changes.
#your code goes here

Tuples

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

#your code goes here

This tuple represents the geographic coordinates of a specific location.

Using Tuples:

1. Accessing Items: Just like lists, you can access items in a tuple using their index:

#your code goes here
#your code goes here

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.

Dictionaries

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 {}.

#your code goes here

In this example, we have a dictionary called student_data that stores various pieces of information about a student’s academic details.

Using Dictionaries:

1. Accessing Values: You can retrieve a value using its corresponding key.

#your code goes here 

2. Adding or Modifying Values: You can easily add new key-value pairs or modify existing ones:

#Modify Existing one
#your code goes here 
#Adding new key-value pair
#your code goes here 

3. Removing Key-Value Pairs: You can remove a key-value pair using the del statement:

#your code goes here

Exercise 3

  1. Create a dictionary named countries with these key-value pairs:
  • “United States” : “English”
  • “Mexico” : “Spanish”
  • “France” : “French”
  • “Canada” : “English and French”
  1. Add a new entry to the dictionary: “Brazil” : “Portuguese”
  2. Remove the key “France” and its value using the del statement.
  3. Print the updated dictionary every time.
#your code goes here

Choosing the Right Data Structure

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

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

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.

Basic Structure of Conditionals:

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

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:

#your code goes here

In this example:

  • If the population is less than 5,000, it prints “Small town.”
  • If the population is between 5,000 and 20,000 (inclusive), it prints “Medium-sized town.”
  • If the population is over 20,000, it prints “Large town.”

Logical Operators

Logical operators allow us to combine multiple conditions. The most commonly used logical operators are:

  • and: Returns True if both conditions are true.
  • or: Returns True if at least one condition is true.
  • not: Reverses the truth value (makes True become False and vice versa).

Let’s say we want to check if someone’s annual income falls within the middle-income bracket:

#your code goes here

In this example:

  • The condition annual_income >= 30000 and annual_income <= 70000 checks if the income is between 30,000 and 70,000 inclusive.
  • If both are true, it prints “Middle-income bracket.”

Loops

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.

For 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:

#your code goes here

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]
#your code goes here

In this case, the loop adds up all the monthly incomes and then finds the average by dividing by the number of months.

Exercise 4

  1. You are given a list named populations that contains population numbers (in thousands).
  2. Use a for loop to add all the population numbers together and store the total in a variable called total_population.
  3. Calculate the average population by dividing the total population.
  4. Print the average population rounded to the nearest whole number.
populations = [2731, 631, 1135, 934, 1705]

#your code goes here

While Loops

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:

#your code goes here

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:

#your code goes here

Combining Loops and Conditionals

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”:

#your code goes here

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.

#your code goes here

Exercise 5

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:

  1. Loop through each university and its graduation rate.
  2. Print the university name and its graduation rate.
  3. Use conditional statements to classify the graduation rate:
    • 90% or above: print “Excellent graduation rate”.
    • Between 75% and 89% (inclusive): print “Good graduation rate”.
    • Below 75%: print “Needs improvement”.
#your code goes here