Introduction to Python and Basic Output

SA
StudyAI
AI-generated study notes
· Published Updated

From the ICT curriculum

TL;DR

Python is a popular, easy-to-read programming language that lets you tell computers what to do step-by-step. The print() function is your main tool for showing text or results on the screen. Understanding how print() works is crucial for seeing your program's output and debugging.

1. The Mental Model

Think of Python as a chef following your recipe. You write instructions (code), and Python executes them. The print() function is like the chef showing you a specific ingredient or the final dish on a plate.

2. The Core Material

Python is a high-level programming language, meaning it's closer to human language than to machine code. This makes it easier to learn and use. It's also interpreted, so you don't need to compile your code before running it; Python executes it line by line.

A fundamental part of programming is being able to see what your program is doing. This is where output comes in. In Python, the primary way to display output to the user (or to yourself for debugging) is using the print() function.

The print() Function

Detailed view of an industrial printing machine showcasing parts and rollers in action.
Photo by wutthichai charoenburi on Pexels

The print() function simply displays whatever you put inside its parentheses () to the console (your screen).

print("Hello, world!")

When you run this code, it will show:

Hello, world!

You can print different types of information:

  • Text (Strings): Always put text inside single quotes (') or double quotes ("). These are called strings.
    python print('This is a string.') print("Another string example.")
  • Numbers: You don't need quotes for numbers.
    python print(123) print(3.14)
  • Variables: You'll learn more about variables soon, but they are names that store data.
    python message = "Python is fun!" print(message)
  • Results of calculations:
    python print(5 + 3) print(10 / 2)

Combining Output with Commas

Black and white abstract blocks on a white background, conceptual design.
Photo by Google DeepMind on Pexels

You can print multiple items separated by commas. Python will automatically put a space between them.

print("My name is", "Alice", "and I am", 30, "years old.")

Output:

My name is Alice and I am 30 years old.

Newlines

By default, print() adds a new line character at the end of its output, meaning the next print() statement will start on a new line.

print("First line.")
print("Second line.")

Output:

First line.
Second line.

You can change this behavior using the end parameter, but we'll cover that later when you're more comfortable.

Here's a diagram showing the basic flow of running a Python script with print statements:

graph TD
    A["You write Python code (.py file)"] --> B{"Does code have print() statements?"}
    B -- Yes --> C["Python Interpreter reads code line by line"]
    C --> D["When print() is encountered"]
    D --> E["Content inside print() is displayed on screen"]
    E --> C
    B -- No --> F["Program runs without visible output (unless errors)"]

3. Worked Example

Let's say you want to display a simple greeting and a calculation.

# Step 1: Print a welcoming message
print("Welcome to your first Python program!")

# Step 2: Print a simple arithmetic calculation
print("The sum of 15 and 27 is:", 15 + 27)

# Step 3: Print a message combining different types of data
user_name = "Charlie"
favorite_number = 7
print("Hello,", user_name, "! Your favorite number is", favorite_number)

When you run this code, the output will be:

Welcome to your first Python program!
The sum of 15 and 27 is: 42
Hello, Charlie ! Your favorite number is 7

4. Key Takeaways

  • Python is an easy-to-read, interpreted programming language.
  • The print() function is essential for displaying output to the console.
  • You can print strings (text in quotes), numbers, variables, and calculation results.
  • Use commas to combine multiple items in a single print() statement; Python adds spaces automatically.
  • Each print() statement typically outputs on a new line by default.

Common Mistakes to Avoid

Flat lay of a spiral notebook and eraser on a pastel pink background with crossed out words.
Photo by KATRIN BOLOVTSOVA on Pexels

  • Forgetting to put text inside quotes (" or '). This will cause a NameError.
  • Forgetting the parentheses () after print. This will cause a SyntaxError.
  • Mixing single and double quotes (e.g., print("Hello.'). Stick to one type for a string.
  • Misspelling print (e.g., Print, prnt). Python is case-sensitive.

5. Now Try It

Write a Python script that calculates the area of a rectangle. Define two variables, length and width, set them to 10 and 5 respectively. Then, calculate the area and use print() to display a message like: "The area of a rectangle with length 10 and width 5 is 50."

What success looks like: Your script runs without errors and produces the exact output shown above.

Frequently asked about Introduction to Python and Basic Output

Python is a popular, easy-to-read programming language that lets you tell computers what to do step-by-step. The print() function is your main tool for showing text or results on the screen. Read the full notes above for the details.

Introduction to Python and Basic Output is a core topic in ICT. Most exam papers test it via a mix of definitions, worked examples, and applied problems. The notes above cover the high-yield sub-topics, common pitfalls, and the kind of questions examiners typically set.

Yes — every note in the StudyAI Campus Hub is free to read in full, right here on this page, with no account needed. If you clone the plan into your own dashboard, the free plan shows a preview of each note there; Basic and above unlock the full notes in your dashboard, along with practice quizzes, flashcards and offline study. You can always come back here to read the complete note for free.

Study this next


Get the full ICT curriculum

Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.

Save this course free