Your First Steps in Python

A Beginner’s Guide to Writing python Code in Jupyter Notebook

Author

Prosper Ayinebyona

Published

April 13, 2026

1 Getting Started with Jupyter Notebook

Before writing any code, let’s get comfortable with the tool we will be using (Jupyter Notebook). Think of it like a digital notebook where you can write, run, and see the results of your code all in one place.

If you do not have anaconda installed, look at this Installing Python via Anaconda guide

1.1 Launching Jupyter Notebook

  1. Open Anaconda Navigator from your Start Menu (Windows) or Applications folder (macOS/Linux)
  2. Click Launch under Jupyter Notebook
  3. A new tab will open in your web browser, this is your Jupyter environment
Nothing to install!

Jupyter Notebook comes pre-installed with Anaconda. You do not need to install anything extra.

1.2 Creating Your First Notebook

  1. In the Jupyter home page, click the New button (top right)
  2. Select Python 3 (ipykernel)
  3. A new notebook will open — you are ready to code!

1.3 Understanding the Interface

The most important things to know:

  • Cell: A box where you type your code
  • Run a cell: Press Shift + Enter to run the code inside a cell
  • Add a new cell: Click the + button in the toolbar
  • Output (Cell Output): The result of your code appears directly below the cell
Your most used shortcut

Shift + Enter — Runs the current cell and moves to the next one. You will use this constantly!


2 Your Very First Line of Code

Every programmer in the world starts with the same thing, making the computer display a message. Let’s do that.

2.1 The print() Function

The print() function tells Python to display something on the screen. Type the following into a cell and press Shift + Enter:

print("Hello, World!")

Output:

Hello, World!

Congratulations you just wrote your first line of Python!

Let’s try a few more:

print("My name is Rukundo")
print("I am learning Python")
print("This is fun")
print("Byabintu ni sawa")

Output:

My name is Rukundo
I am learning Python
This is fun!
Byabintu ni sawa
What are the quotation marks for?

The quotation marks " " tell Python that what is inside is a piece of text (called a string). You can use either double quotes "hello" or single quotes 'hello' — both work the same way.

2.2 Adding Comments

A comment is a line that Python ignores when running your code. Comments are for you (or others reading your code) to understand what is going on. They start with a # symbol:

# This is a comment — Python will NOT run this line
print("But Python WILL run this line!")

# You can use comments to label your work
print("Section 1 complete.")
Tip

Get into the habit of writing comments. Even experienced programmers use them to keep their code readable.


3 Variables — Storing Information

Imagine a variable as a labeled box where you can store a piece of information and refer to it later by its name.

3.1 Creating a Variable

name = "Alice"
age  = 25
city = "Nairobi"

Here:

  • name, age, and city are the variable names (the labels on the boxes)
  • "Alice", 25, and "Nairobi" are the values stored inside them
  • The = sign means “store this value into this variable”

3.2 Using Variables

Once stored, you can use the variable name anywhere:

name = "Alice"
age  = 25
city = "Nairobi"

print(name)
print(age)
print(city)

Output:

Alice
25
Nairobi

3.3 Combining Variables and Text

You can mix variables and text in a print() statement using f-strings — just put an f before the quote and wrap variable names in {}:

name = "Alice"
age  = 25
city = "Nairobi"

print(f"My name is {name}.")
print(f"I am {age} years old.")
print(f"I live in {city}.")

Output:

My name is Alice.
I am 25 years old.
I live in Nairobi.

3.4 Updating a Variable

You can change the value stored in a variable at any time:

score = 0
print(f"Starting score: {score}")

score = 10
print(f"Updated score: {score}")

Output:

Starting score: 0
Updated score: 10
Naming rules

Variable names:

  • Must start with a letter or underscore (_)
  • Cannot contain spaces — use an underscore instead: first_name, not first name
  • Are case-sensitive: Name and name are two different variables

4 Numbers and Basic Math

Python works as a powerful calculator. You can perform all kinds of math operations directly in a cell.

4.1 Basic Arithmetic

# Addition
print(10 + 5)

# Subtraction
print(10 - 5)

# Multiplication
print(10 * 5)

# Division
print(10 / 5)

Output:

15
5
50
2.0

4.2 More Operations

# Exponent (power): 2 to the power of 8
print(2 ** 8)

# Floor division (quotient only, no remainder)
print(17 // 5)

# Modulo (remainder only)
print(17 % 5)

Output:

256
3
2

4.3 Math with Variables

price     = 150
quantity  = 4
discount  = 20

total     = price * quantity
final     = total - discount

print(f"Subtotal : {total}")
print(f"Discount : {discount}")
print(f"Total    : {final}")

Output:

Subtotal : 600
Discount : 20
Total    : 580

4.4 Asking the User for Input

The input() function pauses the program and waits for the user to type something:

name = input("What is your name? ")
print(f"Hello, {name}! Welcome to Python.")
Note

When you run this cell, a text box will appear below it. Type your name and press Enter.

# input() always returns text, so convert to a number first using int()
age = int(input("How old are you? "))
print(f"In 10 years, you will be {age + 10} years old.")

5 Working with Text (Strings)

Text in Python is called a string. You already used strings with print() — now let’s explore what you can do with them.

5.1 String Basics

greeting = "Hello, World!"

# Count the number of characters
print(len(greeting))

# Convert to ALL CAPS
print(greeting.upper())

# Convert to all lowercase
print(greeting.lower())

# Replace a word
print(greeting.replace("World", "Python"))

Output:

13
HELLO, WORLD!
hello, world!
Hello, Python!

5.2 Joining Strings Together

Combining strings is called concatenation:

first_name = "Mugabo"
last_name  = "Isaac"

full_name  = first_name + " " + last_name
print(full_name)

Output:

Mugabo Isaac

5.3 Useful String Methods

sentence = "  python is amazing  "

# Remove extra spaces from both ends
print(sentence.strip())

# Check if text starts with a word
print(sentence.strip().startswith("python"))

# Check if text ends with a word
print(sentence.strip().endswith("amazing"))

# Count how many times a letter appears
print("banana".count("a"))

Output:

python is amazing
True
True
3

5.4 A Mini Exercise

# Fill in your own information and run the cell
first_name  = "Your first name here"
last_name   = "Your last name here"
university  = "Your University here"
hobby       = "Your hobby here"

print(f"My full name is {first_name} {last_name}.")
print(f"I am student at {university}.")
print(f"My favourite hobby is {hobby}.")
print(f"My first name has {len(first_name)} letters in it.")

6 Lists

A list lets you store multiple items in a single variable — like a shopping list or a class register.

6.1 Creating a List

# A list of fruits
fruits = ["apple", "banana", "mango", "orange"]
print(fruits)

Output:

['apple', 'banana', 'mango', 'orange']

6.2 Accessing Items

Each item in a list has a position number called an index, starting from 0:

fruits = ["apple", "banana", "mango", "orange"]
#           0         1        2         3

print(fruits[0])   # First item
print(fruits[1])   # Second item
print(fruits[-1])  # Last item (negative index counts from the end)

Output:

apple
banana
orange

6.3 Modifying a List

fruits = ["apple", "banana", "mango"]

# Add an item to the end
fruits.append("orange")
print(fruits)

# Remove a specific item
fruits.remove("banana")
print(fruits)

# How many items are in the list?
print(len(fruits))

Output:

['apple', 'banana', 'mango', 'orange']
['apple', 'mango', 'orange']
3

6.4 Example

students = ["Alice", "Bob", "Carol", "David", "Eve"]

print(f"Number of students : {len(students)}")
print(f"First student      : {students[0]}")
print(f"Last student       : {students[-1]}")

# Add a new student
students.append("Frank")
print(f"After adding Frank : {students}")

Output:

Number of students : 5
First student      : Alice
Last student       : Eve
After adding Frank : ['Alice', 'Bob', 'Carol', 'David', 'Eve', 'Frank']

7 Making Decisions with if Statements

In real life, we make decisions based on conditions: “If it is raining, I will take an umbrella.” Python does the same using if statements.

7.1 Basic if Statement

age = 20

if age >= 18:
    print("You are an adult.")
Indentation matters!

The code inside an if block must be indented (pushed inward with a Tab or 4 spaces). Python uses indentation to know what belongs inside the if block.

7.2 if / else

age = 15

if age >= 18:
    print("You are allowed to vote.")
else:
    print("You are not old enough to vote yet.")

Output:

You are not old enough to vote yet.

7.3 if / elif / else (Multiple Conditions)

elif means “else if” — it checks another condition if the first one is False:

score = 72

if score >= 90:
    print("Grade: A — Excellent!")
elif score >= 75:
    print("Grade: B — Good job!")
elif score >= 60:
    print("Grade: C — Keep it up!")
else:
    print("Grade: F — Let's study harder!")

Output:

Grade: C — Keep it up!

7.4 Comparison Operators

Operator Meaning Example
== Equal to 5 == 5 → True
!= Not equal to 5 != 3 → True
> Greater than 7 > 3 → True
< Less than 2 < 9 → True
>= Greater than or equal 5 >= 5 → True
<= Less than or equal 3 <= 4 → True

7.5 A Mini Exercise — Try It Yourself! 🧪

# Change the temperature value and see what happens
temperature = 30

if temperature > 35:
    print("It is very hot outside. Stay hydrated!")
elif temperature > 25:
    print("It is warm. A great day to go out!")
elif temperature > 15:
    print("It is mild. Bring a light jacket.")
else:
    print("It is cold. Dress warmly!")

8 Repeating Actions with Loops

A loop lets you repeat a block of code multiple times without writing it over and over. This is one of the most powerful ideas in programming.

8.1 The for Loop

A for loop goes through a list (or a range of numbers) one item at a time:

fruits = ["apple", "banana", "mango", "orange"]

for fruit in fruits:
    print(fruit)

Output:

apple
banana
mango
orange

Think of it as: “For each fruit in my list of fruits, print that fruit.”

8.2 Looping Through Numbers with range()

range(n) generates numbers from 0 up to (but not including) n:

for number in range(5):
    print(number)

Output:

0
1
2
3
4

You can also specify a start and an end:

for number in range(1, 6):
    print(f"Step {number}")

Output:

Step 1
Step 2
Step 3
Step 4
Step 5

8.3 Doing Something Useful in a Loop

students = ["Alice", "Bob", "Carol", "David"]

for student in students:
    print(f"Hello, {student}! Welcome to class.")

Output:

Hello, Alice! Welcome to class.
Hello, Bob! Welcome to class.
Hello, Carol! Welcome to class.
Hello, David! Welcome to class.

8.4 Combining a Loop with if (Putting It All Together!)

scores = [88, 45, 95, 61, 73, 39, 82]

for score in scores:
    if score >= 75:
        print(f"Score {score} → PASS ✅")
    else:
        print(f"Score {score} → FAIL ❌")

Output:

Score 88 → PASS ✅
Score 45 → FAIL ❌
Score 95 → PASS ✅
Score 61 → FAIL ❌
Score 73 → FAIL ❌
Score 39 → FAIL ❌
Score 82 → PASS ✅

8.5 A Final Challenge

# This program prints a personalised times table
# Change the number below and run the cell!

number = 7

print(f"--- Times Table for {number} ---")
for i in range(1, 11):
    result = number * i
    print(f"{number} x {i:2} = {result}")

Output (for number = 7):

--- Times Table for 7 ---
7 x  1 = 7
7 x  2 = 14
7 x  3 = 21
7 x  4 = 28
7 x  5 = 35
7 x  6 = 42
7 x  7 = 49
7 x  8 = 56
7 x  9 = 63
7 x 10 = 70

9 What’s Next?

You have made it through your first Python journey, well done!

Here is a summary of everything you have learned:

Chapter Concept What It Does
2 print() & Comments Displays output; adds notes to code
3 Variables Stores and labels information
4 Numbers & Math Performs calculations
5 Strings Works with text
6 Lists Groups multiple items together
7 if Statements Makes decisions based on conditions
8 for Loops Repeats actions automatically

9.1 Keep Practising

The best way to learn programming is by doing. Here are some ideas to try on your own:

  • Write a program that asks for someone’s name and age, then prints how old they will be in 20 years
  • Create a list of 5 of your favourite movies and print them one by one using a loop
  • Write an if/elif/else program that converts a numeric grade into a letter grade