Quiz 2

Introduction to Programming & Python

2280 words
11 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Introduction to Programming & Python > **Why read this?** You probably use software every day — WhatsApp, Google Maps, Instagram — but have you ever wondered how those apps actually work? Programming is how we give step-by-step instructions to a computer to make it do useful things.

Introduction to Programming & Python

Why read this? You probably use software every day — WhatsApp, Google Maps, Instagram — but have you ever wondered how those apps actually work? Programming is how we give step-by-step instructions to a computer to make it do useful things. This chapter is your very first step into that world.

🎯 Learning Objectives

By the end of this topic, you will be able to:
  1. Explain what a program is and how computers follow instructions
  2. Set up the Python REPL and write your first line of code
  3. Use print() to display text and numbers on the screen
  4. Understand the difference between source code, interpreter, and output
  5. Recognize syntax errors and fix basic mistakes

📋 Prerequisites

  • None. This course assumes ZERO prior programming experience.
  • Basic computer skills: opening applications, typing, saving files.

📖 Core Content

1.1 What Is Programming?

Intuition: Imagine you're teaching a friend how to make tea. You'd give them a recipe:
pseudo
1. Boil water.
2. Put a tea bag in a cup.
3. Pour hot water into the cup.
4. Wait 3 minutes.
5. Remove the tea bag.
6. Add sugar if desired.
That recipe is a program (a sequence of instructions). The friend is the computer. Programming is just writing recipes that a computer can follow — except computers are extremely literal and need every tiny step spelled out. Why it matters: Every website you visit, every game you play, every app on your phone is nothing but a very long, very detailed list of instructions written by a programmer. By learning to program, you learn to create instead of just consume.

1.2 Computer Architecture (Simplified)

Diagram Rendering diagram
  • Source code: The text you write in Python (a .py file).
  • Interpreter: A program that reads your source code and translates it into machine instructions the CPU can understand.
  • CPU (Central Processing Unit): The "brain" that actually does the work.
  • Memory (RAM): Temporary storage for data while the program runs.

1.3 Your First Python Program

Let's write the simplest possible Python program.
python
# runnable
print("Hello, world!")
Output:
pseudo
Hello, world!
Line-by-line breakdown:
  • print — this is a function (a built-in command in Python). It tells Python "display what's in the parentheses on the screen."
  • ( and ) — parentheses contain the information you want to print.
  • "Hello, world!" — this is a string literal (a piece of text enclosed in quotes). The quotes tell Python "this is text, not code."
  • The string "Hello, world!" is called an argument — the input we give to the print function.
Mental model: Think of print() as a messenger. You hand the messenger a message (inside the parentheses), and they deliver it to the screen.

1.4 The Python REPL

REPL stands for Read-Eval-Print Loop. It's an interactive environment where you type Python code and see results immediately. When you open Python's interactive mode (by typing python in a terminal or using Repl.it), you see >>> — that's the prompt telling you "I'm ready for your next instruction." Example of a REPL session:
python
>>> 2 + 3
5
>>> "Hello" + " " + "World"
'Hello World'
>>> print("Hi!")
Hi!
Why this matters: The REPL is your best friend for learning. You can test small ideas instantly without creating a full program. Experiment fearlessly — you can't break anything.

1.5 Values and Data Types

Every piece of data in Python has a type — a category that tells Python what kind of data it is and what operations are allowed.
Type NameWhat It StoresExampleExplanation
int (integer)Whole numbers (positive, negative, zero)42, -7, 0No decimal point
float (floating point)Numbers with decimals3.14, -0.5, 1.0Has a decimal point
str (string)Text"Hello", 'Python'Enclosed in quotes
bool (boolean)True/False valuesTrue, FalseOnly two possible values
You can check the type of any value using the type() function:
python
# runnable
print(type(42))
print(type(3.14))
print(type("Hello"))
print(type(True))
Output:
java
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
Common confusion: "42" (in quotes) is a string, not an integer. 42 (without quotes) is an integer. They look the same on screen but behave differently. For example, 42 + 1 gives 43, but "42" + "1" gives "421" (it concatenates text).

1.6 Literals — Writing Values in Code

A literal is a value written directly into your code.
python
# runnable
print(5)          # integer literal
print(3.14159)    # float literal
print("Python")   # string literal (double quotes)
print('Python')   # string literal (single quotes)
print(True)       # boolean literal
Output:
pseudo
5
3.14159
Python
Python
True
Note: Both " and ' work for strings in Python. Choose one and be consistent. The most common convention is double quotes "...".

1.7 Comments — Writing Notes in Your Code

Comments are text that Python ignores entirely. They're for humans to read.
python
# runnable
# This is a comment. Python won't run this line.
print("This will run")  # Comments can go after code too.
# Multiple lines of comments
# help explain what's happening.
# Use them liberally!
Output:
pseudo
This will run
Why comments matter: When you come back to your code a week later (or when someone else reads it), comments explain why you did something. They're like sticky notes on your code.

1.8 How Python Runs Your Code

Diagram Rendering diagram Step-by-step:
  1. You write Python code in a text file (e.g., myprogram.py).
  2. You run python myprogram.py in the terminal.
  3. The Python interpreter reads every line from top to bottom.
  4. If any line violates Python's grammar rules (syntax), Python stops immediately and shows an error.
  5. If everything is correct, Python translates the code to bytecode (a low-level intermediate representation).
  6. The Python Virtual Machine (PVM) executes the bytecode.
  7. The result appears on your screen.

1.9 Example 1: Print Multiple Items

python
# runnable
print("The answer is", 42)
print("Pi is approximately", 3.14159)
Output:
pseudo
The answer is 42
Pi is approximately 3.14159
What's happening: The print() function can take multiple arguments separated by commas. It automatically adds a space between them.

1.10 Example 2: Simple Calculations

python
# runnable
print(15 + 3)
print(15 - 3)
print(15 * 3)
print(15 / 3)
Output:
pseudo
18
12
45
5.0
Notice: Division (/) always returns a float (decimal) result, even when the numbers divide evenly.

1.11 Example 3: Combining Text and Calculations

python
# runnable
print("15 + 3 =", 15 + 3)
print("15 * 3 =", 15 * 3)
Output:
pseudo
15 + 3 = 18
15 * 3 = 45
What's happening: The print() function evaluates 15 + 3 (producing 18), then prints both the string and the number.

1.12 Example 4: Using the REPL as a Calculator

python
>>> 200 + 37
237
>>> 50 - 12 * 3   # multiplication happens before subtraction
14
>>> (50 - 12) * 3 # parentheses change the order
114
>>> 2 ** 10       # ** means "to the power of"
1024

1.13 Example 5: Your First Error

Let's intentionally make a mistake:
python
# runnable
print("Hello)
Output (error):
pseudo
SyntaxError: EOL while scanning string literal
What happened: We forgot to close the string with a matching quote. Python reached the end of the line (EOL) while still inside the string, and it didn't know what to do. How to fix: Add the missing closing quote: print("Hello")

📐 Key Concepts Reference

ConceptSyntaxPurposeExample
Print functionprint(value)Display output on screenprint("Hi")
String literal"text" or 'text'Represent text in code"Hello"
Integer literal42Represent whole numbers100
Float literal3.14Represent decimal numbers-0.5
Boolean literalTrue / FalseRepresent truth valuesTrue
Type checktype(value)Find the data typetype(42)int
Comments# textAdd human-readable notes# this is a comment

⚠️ Common Pitfalls

Pitfall 1: Unmatched Quotes

The mistake: print("Hello) or print('Hello") Why it happens: You started with one quote type but ended with another, or forgot to close. The error: SyntaxError: EOL while scanning string literal How to fix: Ensure both opening and closing quotes match: print("Hello") or print('Hello').

Pitfall 2: Forgetting Parentheses

The mistake: print "Hello" (no parentheses) Why it happens: In Python 2, print was a statement. In Python 3 (which we use), print is a function and requires parentheses. The error: SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello")? How to fix: Always use parentheses: print("Hello").

Pitfall 3: Confusing Text and Numbers

The mistake: print(Hello) without quotes around Hello. Why it happens: You think Hello is text that should be displayed directly. The error: NameError: name 'Hello' is not defined How to fix: Text values must be in quotes: print("Hello"). Without quotes, Python thinks Hello is a variable name.

Pitfall 4: Mixing Up Quote Types

The mistake: print('It's a sunny day') Why it happens: The apostrophe in "It's" is the same character as the single quote used to delimit the string. The error: SyntaxError: invalid syntax How to fix: Use double quotes for the string: print("It's a sunny day"), or escape the apostrophe: print('It\'s a sunny day').

📝 Practice Questions

Q1: What will the following code output?
python
print("Hello", "World")
Answer:
pseudo
Hello World
The print() function automatically separates multiple arguments with a space. Q2: What is the difference between print(5 + 3) and print("5 + 3")?
Answer:
  • print(5 + 3) evaluates the expression first (5 + 3 = 8) and outputs 8.
  • print("5 + 3") prints the literal text 5 + 3 because it's in quotes (a string).
  • Output: 8 vs 5 + 3 Q3: What error does this code produce? How would you fix it?
python
print("Welcome to Python)
Answer: Error: SyntaxError: EOL while scanning string literal Fix: Add a closing double-quote: print("Welcome to Python") The error happens because the string was started with " but never closed before the end of the line. Q4: Write code to display "My age is 20" using a print statement.
Answer:
python
print("My age is 20")
Or using the comma syntax:
python
print("My age is", 20)
Both produce the same output: My age is 20 Q5: What does the type() function return for each of these values?
python
type(3.0)
type("3")
type(3)
type(True)
Answer:
  • type(3.0)<class 'float'> (has a decimal point)
  • type("3")<class 'str'> (in quotes)
  • type(3)<class 'int'> (whole number, no quotes)
  • type(True)<class 'bool'> (boolean literal) Q6: Predict the output:
python
print(10, 20, 30)
print(10 + 20 + 30)
print("10 + 20 + 30")
Answer:
pseudo
10 20 30
60
10 + 20 + 30
Line 1: prints three numbers separated by spaces. Line 2: evaluates the sum (60) and prints the result. Line 3: prints the string literally. Q7: Which of these variable names are VALID in Python?
pseudo
1st_place  |  first_place  |  first-place  |  FirstPlace  |  first place
Answer:
  • first_placeValid (starts with letter, uses underscore)
  • FirstPlaceValid (starts with letter, case matters though)
  • 1st_placeInvalid (starts with a digit)
  • first-placeInvalid (hyphen is not allowed)
  • first placeInvalid (space is not allowed)
Variable names must start with a letter or underscore, followed by letters, digits, or underscores. Q8: Fix the errors in this code:
python
print("Hello)
print('How are you?)
print(5 + "3")
Answer:
python
print("Hello")      # missing closing quote
print('How are you?')  # missing closing apostrophe
print(5 + 3)        # can't add int and str; remove quotes from 3
Fixed output:
pseudo
Hello
How are you?
8
Q9: What is the output of this code?
python
print("Result:", 2 * 5, "and", 2 ** 5)
Answer:
pseudo
Result: 10 and 32
  • 2 * 5 evaluates to 10
  • 2 ** 5 (2 to the power 5) evaluates to 32 Q10: Write a program that prints your name, your age, and the result of "50 divided by 7" on three separate lines.
Answer:
python
# runnable
print("My name is Rahul")
print("My age is 25")
print("50 divided by 7 is", 50 / 7)
Output:
pseudo
My name is Rahul
My age is 25
50 divided by 7 is 7.142857142857143

🔗 Cross-References

  • Next Topic: Variables, Data Types & Expressions — Learn how to store data in named containers.
  • BSCS1001 Computational Thinking: Algorithms are the foundation of programming — see how flowcharts connect to code.
  • BSCS2002 PDSA: Later in the degree, you'll explore how programs manage data efficiently.
  • Reference: Python for Everybody, Chapter 1 — "Why should you learn to write programs?"
  • Video: L1: A quick introduction to variables | Programming in Python Join Discord Next2. Variables & Types
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.