Python 1 – Printing & Variables

Output text to the screen, use escape sequences, and store data in variables you can reuse.

Printing basics

Use print() to display output:

print("Hello, world!")
print("Welcome to CodeCraft")

Text (strings) goes in quotes. You can use single or double quotes.

Escape sequences & newlines

Use \n for a newline and \t for a tab. To include quotes inside a string, either switch quote types or escape with \.

print("Line 1\nLine 2")
print("Name:\tAisha")
print('He said "hi" to me')
print("He said \"hi\" to me")

Concatenation & commas

There are two common ways to combine values in a print:

  1. Concatenate strings with + (no spaces added automatically).
  2. Use commas to print multiple items (adds spaces automatically).
first = "Computer"
second = "Science"
print(first + " " + second)       # Concatenation
print("Sum is", 3 + 4)            # Commas, space inserted
print("Name:", "Aisha", "Age:", 14)

Variables

Variables store data you can reuse. Use = to assign:

name = "Zayn"
year = 9
is_student = True

print("Name:", name)
print("Year:", year)
print("Student:", is_student)

Variable names should be descriptive and use snake_case like total_score.

Numbers vs strings

Strings (text) and numbers are different types. You can add numbers, but adding strings joins text.

print(10 + 5)          # 15 (addition)
print("10" + "5")      # "105" (concatenation)
print(type(10), type("10"))

Type conversion

Convert between types with int(), float(), and str():

age_text = "15"
age_num = int(age_text)      # "15" -> 15
print("Next year:", age_num + 1)

pi = 3.14
print("PI is " + str(pi))    # Convert number -> string for concatenation

Quick tasks

Task 1 – Name badge

Print your full name on one line and your class on the next line using \n.

print("Alex Johnson\nClass 9B")

Task 2 – Favourite things

Create variables for your name, favourite_colour and favourite_movie, then print them on separate lines using a single print().

name = "Mina"
favourite_colour = "yellow"
favourite_movie = "Interstellar"
print(name + "\n" + favourite_colour + "\n" + favourite_movie)

Task 3 – Simple maths output

Print a sentence that includes the result of a calculation (e.g., The total is 42), using either concatenation or commas.

a = 18
b = 24
print("The total is", a + b)