Dr. Wei Xing - University of Sheffield
If you're reading this, you probably felt a bit lost in Week 1's lab. That's completely normal, and you're in the right place. This guide will help you understand the Python basics you need for machine learning - nothing more, nothing less.
Important: You don't need to memorize any of this. The goal is to understand concepts so that when you see them in labs, they make sense. Think of this as a reference guide, not a textbook to study.
How to use this guide:
Let's start from the very beginning.
A code cell is like a calculator - you type something in, press the play button (or Shift+Enter), and it shows you the result. Let's try it.
Your first code: Type this in the code cell and press Shift+Enter:
print("Hello, I'm learning Python!")
What happened? The computer printed your message below the cell. Congratulations - you just wrote your first Python code!
Here's a secret: professional programmers use AI to help them code every single day. You should too! In Google Colab, you can use ChatGPT (in another browser tab) to help you understand anything.
Try this right now: Open ChatGPT and paste this prompt:
I'm learning Python for a machine learning course. I just wrote my first
line: print("Hello, I'm learning Python!")
Can you explain what this line does in very simple terms, like I've never
programmed before? Then show me 2 more examples of print statements I could try.
What to do with AI's response:
Remember: AI is your patient tutor. Ask it to explain things multiple times in different ways until you understand.
Think of a variable like a labeled box where you store information. You give it a name and put something inside.
Try this in Colab:
age = 21
name = "Alex"
print(age)
print(name)
What happened?:
age and put the number 21 in itname and put the text "Alex" in itPaste this in ChatGPT:
I'm learning about Python variables. Can you:
1. Explain why we use variables (give me a real-world analogy)
2. Give me 3 examples of creating variables with different types of information
3. Show me what happens if I try to use a variable before creating it
Keep explanations very simple please!
Try the examples ChatGPT gives you in Colab. Run each one and see what happens.
Variable names can't have spaces. This doesn't work:
my age = 21 # This will give an error!
This works:
my_age = 21 # Use underscore instead of space
myAge = 21 # Or capitalize the second word
AI Prompt if you get confused:
I got an error with my variable name. Here's my code:
[paste your code]
What's wrong and how do I fix it?
Python can do math just like a calculator.
Try these in Colab:
# Addition
result = 10 + 5
print(result)
# Subtraction
result = 10 - 5
print(result)
# Multiplication
result = 10 * 5
print(result)
# Division
result = 10 / 5
print(result)
Note: Lines starting with # are comments - Python ignores them. They're notes for humans reading the code.
You can do math with variables too:
price = 100
discount = 20
final_price = price - discount
print(final_price)
Practice with AI:
Give me 5 simple math problems I can solve in Python using variables.
Make them practical examples (like calculating discounts, areas, etc).
Show me the code for the first one, then just describe the others so
I can try coding them myself.
Sometimes you need to store multiple related items. That's what lists are for.
Try this:
# A list of numbers
temperatures = [20, 22, 19, 21, 23]
print(temperatures)
# A list of text
cities = ["Sheffield", "London", "Manchester"]
print(cities)
Lists are numbered starting from 0 (yes, zero!):
cities = ["Sheffield", "London", "Manchester"]
print(cities[0]) # Prints "Sheffield" (first item)
print(cities[1]) # Prints "London" (second item)
print(cities[2]) # Prints "Manchester" (third item)
Why start from 0? It's a computer science thing. Just remember: first item is [0], second is [1], third is [2].
When you're confused about lists:
I'm working with Python lists and I'm confused about:
[describe what confuses you]
Can you explain using a simple real-world example, then show me
code that demonstrates the concept?
Common list operations to ask AI about:
This is where Week 1 got confusing for many students. Let's break it down.
A method is an action you can perform on something. You write it with a dot: thing.action()
Example with a list:
numbers = [3, 1, 4, 1, 5]
numbers.sort() # This sorts the list
print(numbers) # Now prints [1, 1, 3, 4, 5]
What happened?:
numbers is the list.sort() is the action (method)# With text (called "strings")
message = "hello"
message.upper() # Makes it uppercase: "HELLO"
# With lists
my_list = [1, 2, 3]
my_list.append(4) # Adds 4 to the end: [1, 2, 3, 4]
This is crucial: When you see a method you don't understand in the lab, immediately ask AI:
I see this code in my lab:
[paste the confusing line]
Can you:
1. Explain what the .something() part does in simple terms
2. Show me a simpler example of the same method
3. Tell me what the result will be
Practice right now:
Copy this code into Colab:
cities = ["sheffield", "london", "manchester"]
cities.sort()
print(cities)
Then ask ChatGPT to explain what .sort() does and why the output looks the way it does.
Errors are normal. Professional programmers see errors all day long. The key is learning to read them.
NameError - You used something that doesn't exist:
print(x) # Error! We never created 'x'
Fix: Create the variable first:
x = 10
print(x) # Works!
SyntaxError - You made a typo or forgot something:
print("hello" # Error! Missing closing )
This is the most powerful AI use: When you get an error, don't panic. Use this exact process:
I'm learning Python and got this error:
[paste the entire error message]
Here's my code:
[paste your code]
Can you:
1. Explain what the error means in simple terms
2. Show me exactly what I need to fix
3. Explain why the error happened so I don't make the same mistake again
Try this right now: Deliberately create an error in Colab (like forgetting a closing parenthesis), then practice using AI to fix it.
Sometimes code stops working. Here's what to do:
Colab auto-saves to your Google Drive, but it's good practice to:
Here are the specific things you'll need to understand for our machine learning labs. Don't try to memorize - just understand the concepts.
This loads tools we need:
import pandas as pd
What it means: "Load the pandas tool and let me call it 'pd' for short"
AI prompt when you see imports:
I see this import statement:
[paste the import]
What is this library used for? Keep it simple and relevant to
machine learning and data analysis.
data = pd.read_csv("file.csv")
What it means: "Read data from a file and store it in a variable called 'data'"
data.head() # Shows first few rows
data.shape # Shows how big the data is
data['column_name'].value_counts() # Counts different values
Strategy: When you see code like this in labs, break it down with AI:
Break down this code line-by-line:
[paste the code]
Explain each part separately:
- What is data?
- What does ['column_name'] mean?
- What does .value_counts() do?
- What will the result look like?
Here's how to use AI effectively for this course:
1. Be specific about your level:
I'm a complete beginner learning Python for machine learning.
[your question]
Explain like I've never programmed before.
2. Ask for examples:
Show me 3 different examples of [concept], from simplest to
slightly more complex.
3. Ask "why" not just "how":
Not just how to do [X], but why would I want to do [X] in
machine learning?
4. Request step-by-step breakdowns:
Break this code down line by line and explain what each part does:
[paste code]
5. Learn from errors:
I got this error: [paste error]
My code: [paste code]
Explain what went wrong and why, then show me the fix.
When code is confusing:
I'm looking at this code from my lab and it's confusing:
[paste code]
Can you:
1. Explain what this code is trying to accomplish overall
2. Break down each line
3. Show me a simpler version that does the same thing
When you need to modify code:
I have this working code:
[paste code]
I need to change it to [describe what you want instead].
Show me what to change and explain why.
When starting an exercise:
My lab asks me to: [paste the exercise instructions]
I'm not sure where to start. Can you:
1. Outline the steps I need to take (but don't give me the full code)
2. Give me a hint for step 1
3. Let me try, then I'll come back for the next step
If you want more practice (totally optional):
You can do this. The Python is just a tool. The machine learning concepts are what matter, and we'll teach those clearly. Focus on understanding, not memorizing, and use AI to help you learn.
See you in the labs!
Dr. Wei Xing
w.xing@sheffield.ac.uk
Office Hours: Tuesday 12-1pm, Hicks Building I22