Lab 1: Introduction to Python Programming
DS 1000 — Data Science Concepts
Welcome to DS 1000!
In this lab, you will learn the foundational building blocks of Python programming. By the end of this lab, you will be able to:
- Write and run Python code directly in your browser
- Create variables and understand basic data types
- Work with data structures: lists and dictionaries
- Use built-in Python functions
- Use comparison operators and Booleans
- Write conditional statements with
if,elif, andelse
No prior programming experience is required — we’ll walk through everything step by step.
Getting Started
This page runs Python entirely in your browser using WebAssembly. You don’t need to install anything on your computer.
- Code cells: You can edit and run any code block you see below.
- Running code: Click the Run button (or press
Shift + Enter) to execute a code cell. - Persistence: Variables you define in one cell are available in subsequent cells, just like a real notebook.
1. Python Basics
Let’s start with the most fundamental operations in Python.
Printing Output
The print() function displays a message on the screen. This is how your program communicates results to you.
In interactive mode, the last line of a cell is automatically displayed even without print(). However, using print() is clearer and works for displaying multiple values:
✏️ Practice
Write code that prints your name and your birth month. Add a comment explaining what your code does.
Python as a Calculator
Python can perform arithmetic operations using these symbols:
| Symbol | Operation | Example |
|---|---|---|
+ |
Addition | 5 + 3 → 8 |
- |
Subtraction | 5 - 3 → 2 |
* |
Multiplication | 5 * 3 → 15 |
/ |
Division | 5 / 2 → 2.5 |
** |
Exponentiation (power) | 5 ** 2 → 25 |
For more operators, see: Python Operators
✏️ Practice
Write code to calculate: take the product of 3 and 4, add 7, then divide the whole thing by 2.
Hint: Remember BEDMAS/PEMDAS — use parentheses to control the order of operations.
2. Variables and Data Types
What is a Variable?
A variable is a name that stores a value. Think of it as a labeled container that holds information you want to use later.
To create a variable, use the = sign (called the assignment operator):
Variable naming rules:
- Names can contain letters, numbers, and underscores (
_) - Names must start with a letter or underscore (not a number)
- Names are case-sensitive (
ageandAgeare different variables) - Choose descriptive names:
student_ageis better thanx
Variables make your code reusable. If you need to change a value, you only change it in one place:
✏️ Practice
Create two variables: birth_year (your birth year as a number) and birth_month (your birth month as text, e.g., “January”). Print both values.
Data Types
Every value in Python has a type that determines what you can do with it. The four basic types are:
| Type | Description | Example |
|---|---|---|
int |
Whole numbers | 42, -7, 0 |
float |
Decimal numbers | 3.14, -0.5, 2.0 |
str |
Text (strings) | "Hello", 'Data Science' |
bool |
True or False | True, False |
Use the type() function to check a value’s type:
A note about decimals: Computers store decimal numbers in a way that can sometimes produce tiny rounding differences. This is normal and rarely causes problems in practice:
✏️ Practice
Create a variable height with the value 75.2 (no quotation marks) and check its type. Then create height2 with the value "75.2" (with quotation marks) and check its type. What’s the difference?
Working with Strings
Strings are text enclosed in quotation marks. You can use either single quotes ('...') or double quotes ("...").
More about strings: Python Strings
The + operator behaves differently depending on the data type:
- For numbers: adds them together
- For strings: joins them together (called concatenation)
Type Conversion
Sometimes you need to convert between types. Use these functions:
str()— converts to stringint()— converts to integerfloat()— converts to decimal
This is useful when combining text with numbers:
Tip: An easier way to combine text and values is using f-strings (formatted strings). Put f before the quotation mark and place variables inside curly braces {}:
3. Data Structures: Lists and Dictionaries
So far, each variable has stored a single value. But often we need to work with collections of values. Python provides data structures for this purpose.
Lists
A list is an ordered collection of values. Create a list using square brackets [] with values separated by commas.
More about lists: Python Lists
✏️ Practice
Create a list called my_list containing: "DataScience", "Python", 100, True, 3.14. Print the list.
Accessing List Elements (Indexing)
Each element in a list has a position called an index.
⚠️ Important: Python starts counting at 0, not 1!
| Element | "apple" |
"banana" |
"cherry" |
"date" |
|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 |
Access an element using list_name[index]:
Slicing Lists
To get multiple elements, use slicing: list_name[start:end]
- Includes the element at
start - Excludes the element at
end
Modifying Lists
Lists are mutable — you can change their contents after creation.
✏️ Practice
Using the heights list from above, change the last element to 100, then print the list.
Dictionaries
A dictionary stores data as key-value pairs. Instead of using numeric indices, you use meaningful keys to look up values.
Create a dictionary using curly braces {} with key: value pairs:
More about dictionaries: Python Dictionaries
Dictionary values can be lists, which is useful for storing related data:
✏️ Practice
Create a dictionary called my_courses with two keys: "Course" and "Professor". Each key should have a list of 2-3 courses/professors you have this semester. Print the dictionary.
Adding to Dictionaries
Use square brackets [] to add new key-value pairs:
4. Built-in Functions
Python comes with many useful built-in functions that perform common tasks. You’ve already seen some: print(), type(), str().
Here are a few more:
Use the help() function to learn about any function:
Methods
Methods are functions that belong to a specific type of object. You call them using dot notation: object.method()
For example, lists have an append() method to add elements:
✏️ Practice
Create a list called scores with values 100, 50, 56.5, 70.2, 85, 99. Use the append() method to add 78 to the list. Then print the list and its length.
5. Comparison Operators and Booleans
Comparison operators compare two values and return a Boolean (True or False).
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 4 == 5 |
False |
!= |
Not equal to | 4 != 5 |
True |
< |
Less than | 4 < 5 |
True |
> |
Greater than | 4 > 5 |
False |
<= |
Less than or equal | 4 <= 5 |
True |
>= |
Greater than or equal | 4 >= 5 |
False |
More about comparisons: Python Conditions
Logical Operators
Combine multiple conditions using logical operators:
| Operator | Meaning | Example |
|---|---|---|
and |
True if both are True | (5 > 3) and (2 < 4) → True |
or |
True if at least one is True | (5 > 3) or (2 > 4) → True |
not |
Reverses the result | not (5 > 3) → False |
More about logical operators: Python Logical Operators
6. Conditional Statements (if/elif/else)
Conditional statements let your program make decisions based on conditions.
Structure:
if condition:
# code to run if condition is True
elif another_condition:
# code to run if this condition is True
else:
# code to run if no conditions were True⚠️ Important: The indentation (4 spaces) is required in Python!
✏️ Practice
Write a program that:
- Creates a variable
temperaturewith a value of your choice - Prints “Hot” if temperature > 30, “Warm” if between 20-30 (inclusive), “Cool” if between 10-20 (inclusive), and “Cold” otherwise
Summary
We’ve covered the following fundamentals of Python programming:
| Concept | What You Learned |
|---|---|
| Output | Use print() to display results |
| Comments | Use # to add explanatory notes |
| Variables | Store values with meaningful names |
| Data Types | int, float, str, bool |
| Lists | Ordered collections using [] |
| Dictionaries | Key-value pairs using {} |
| Functions | Built-in tools like len(), max(), round() |
| Methods | Type-specific functions like .append() |
| Comparisons | ==, !=, <, >, <=, >= |
| Conditionals | if, elif, else for decisions |
Comments
Comments help explain what your code does. They start with
#and are ignored by Python. Good comments make your code easier to understand — both for others and for yourself when you return to it later.