Python Notes
Course → Topic 01

01 — Introduction to Programming and Python

🎯 Difficulty: Beginner ⏱ Time: 30 min 🧩 Prerequisite: None — start here!

1. Concept — What is Programming?

💡 Concept: Programming is giving instructions

Programming simply means giving a computer step-by-step instructions to do a task. That's it. Nothing more magical than that.

Think about how you explain a recipe to someone who has never cooked:

Recipe (cooking)Program (coding)
Take 2 eggseggs = 2
Break them into a bowlbreak_eggs()
Whisk until fluffywhisk()
If mixture is thick, add milkif thick: add_milk()
Repeat whisking 20 timesfor i in range(20): whisk()
Serve hotprint("Serve hot!")

A computer is extremely fast but also extremely literal. It does exactly what you write — nothing more, nothing less. If you miss a step, the computer won't "guess" it. That is why we must be very careful and precise.

Why do we need programming?

Computers can do calculations and repetitive work millions of times faster than humans. But they cannot think by themselves — they need our instructions. Programming is how we convert our ideas into instructions the computer understands.

🔍 Deep Dive: Programming languages are translators

Humans and computers don't speak the same language. We speak English; the computer's brain (CPU) only understands 0s and 1s (binary). A programming language like Python is a bridge: we write code in a language that's easy for humans, and the computer converts it into 0s and 1s to run it.

2. What is Python?

Python is a programming language — a tool we use to give instructions to a computer. It was created by Guido van Rossum in 1991.

Python was designed with one big goal: be easy for humans to read and write. The creator wanted a language that felt natural — almost like writing English — instead of a language full of confusing symbols.

💡 Concept: Real-life example

If you want to learn driving, you could start with a complicated race car or a simple friendly car. Python is the friendly car — easy to learn, easy to drive, yet powerful enough to go very far.

Why is Python so popular?

  • Beginner friendly: Reads almost like English. Perfect first language.
  • Free and open source: Anyone can use it forever, for free.
  • Huge community: If you are stuck, thousands of people have solved your exact problem before.
  • Used everywhere: Web apps, data science, artificial intelligence, games, automation, scientific research, and more.
  • Works on all platforms: Windows, macOS, Linux — same code everywhere.

Features of Python

Interpreted

Your code runs line by line, immediately. No separate compile step needed — great for beginners.

Dynamically typed

You don't have to declare variable types. Python figures them out for you.

Object-oriented + Functional

Supports multiple styles of writing code — you'll meet these later.

Batteries included

Thousands of ready-made tools (modules) come built in, so you rarely start from scratch.

3. Interpreter and Script

When you write Python, you can use it in two different ways:

Way 1 — Interactive Mode (the Interpreter)

You type one line, press Enter, and Python answers you immediately. It's like talking to someone in a chat.

>>> print("Hello")
Hello
>>> 2 + 3
5

The >>> symbol is called the prompt. It is Python saying: "I'm ready, type something."

Way 2 — Script Mode (the Script)

A script is simply a file containing Python code (file name usually ends with .py, like hello.py). You write all your instructions in the file, then run the whole file at once. This is how real programs are built.

Interactive ModeScript Mode
One line at a timeWhole file at once
Good for quick experimentsGood for real programs
Nothing is savedCode is saved in a file
🔍 Deep Dive: How does Python execute your code?
  1. Source code: You write Python code (human-friendly).
  2. Compiler: Python secretly converts your code into bytecode — a low-level intermediate form the computer can process faster.
  3. Interpreter (Python Virtual Machine): Reads the bytecode line by line and executes it on your machine.
  4. Result: You see the output.

You don't need to remember these details now. Just know the big picture: your code is converted into something the computer can run, then it runs line by line.

4. Your First Python Program

💻 Code Example: Hello, World!

Every programming journey starts with a tradition: make the computer greet the world. Create a file called hello.py and type:

print("Hello, World!")
>>> Hello, World!

Let's explain this line piece by piece:

  • print — a function (a pre-made command) that shows text on the screen.
  • ( ) — the parentheses hold the input we give to the function, called its argument.
  • "Hello, World!" — a string (a piece of text). The quotes tell Python: "this is text, not code."

Python is a very "English-like" language. Try reading print("Hello") aloud: "Print Hello." That is exactly what it does.

5. Examples — Level by Level

Level 1 — Basic understanding
print("Welcome to Python!")

This prints one line of text. Everything inside the quotes is text; nothing more happens.

Level 2 — Concept practice

We can print multiple lines using multiple print() calls:

print("Line one")
print("Line two")
print("Line three")
>>> Line one
>>> Line two
>>> Line three

Each print() automatically moves to the next line after printing. That's why the three messages appear one below the other.

Level 3 — Logic building

Python can do calculations too. Type 2 + 3 — Python computes the answer and print shows it:

print(2 + 3)
>>> 5

Notice: no quotes around 2 + 3. Quotes would make it text ("2 + 3"), but without quotes Python treats it as math and actually calculates it. This small difference is the start of your programming instinct!

Level 4 — Challenge

Can you guess the output of this?

print("2 + 3")
Reveal output
>>> 2 + 3

Because of the quotes, Python prints the text "2 + 3". It does not calculate. Quotes = text, no quotes = code. Remember this!

6. Dry Run — See it happening

Let's watch a small program run step by step. This habit — called a dry run — is how programmers mentally check their code.

print("Start")
print(5 * 2)
print("End")
StepCode being executedWhat it doesScreen
1print("Start")Prints the text "Start"Start
2print(5 * 2)First multiplies 5×2 = 10, then prints it10
3print("End")Prints the text "End"End
💡 Concept: Why this matters

Python reads and runs your code from top to bottom, one line at a time. The order matters. If you swap the lines, the output order changes too. This "top to bottom" idea is the foundation of every program you'll ever write.

7. ⚠️ Common Beginner Traps

⚠️ Common Mistake: Forgetting the quotes

print(Hello)Error! Python thinks Hello is a variable name, not text. Always wrap text in quotes: print("Hello").

⚠️ Common Mistake: Mixing quote types

print('Hello")Error! The opening quote (') and closing quote (") don't match. Python allows either, but they must match each other.

⚠️ Common Mistake: Unbalanced parentheses

print("Hello"Error! Every opening ( needs a matching closing ). Count them whenever you get a strange error.

⚠️ Common Mistake: Case sensitivity

Print("Hello") with a capital PError! Python is case-sensitive. print, Print, and PRINT are three completely different things to Python.

⚠️ Why beginners make these mistakes

Beginners are used to English where small mistakes are forgiven. Python is not forgiving — it needs exact spelling. Think of Python like a strict teacher who needs perfect handwriting. The good news: errors tell you exactly what's wrong. Read them!

8. 🧠 Think Before You Code

Before solving any problem, ask yourself these questions. This habit will save you hours of confusion.

🧠 Think: The problem-solving checklist
  1. What is the input? — What information do I have?
  2. What should the output be? — What do I want to see?
  3. Which concept is required? — Printing? Math? Loops?
  4. Can I break it into smaller steps? — Solve one small piece at a time.

Example: "Print your name and your age on two separate lines."

  • Input: nothing — I already know my name and age.
  • Output: my name, then my age, each on its own line.
  • Concept: print() — I'll need two print() calls, or one print with a \n.
  • Smaller steps: print name first, then print age.
print("Ravi")
print(20)

9. 🎯 Practice Questions

🎯 Practice

Try each question yourself first. Use the Hint button only if stuck, and the Solution button only after you've genuinely tried. Then read the Explanation to truly understand why.

Concept Question

What is programming? Explain in one simple sentence.

Hint: Think about giving instructions to someone who follows them exactly.
Programming is giving a computer a set of step-by-step instructions to perform a task.
Computers don't think on their own. We write instructions (a program) that tell them exactly what to do, step by step. That process of writing instructions is programming.
Predict the Output

What will this print?

print("Hello")
print(10 + 5)
The second line has no quotes — so Python will calculate it.
Hello
15
Line 1 prints the text "Hello". Line 2 has no quotes, so Python treats 10 + 5 as mathematics, calculates 15, and prints the result. Quotes mean text; no quotes means code/math.
Find the Error

What is wrong with this code?

print(Hello World)
Text must be wrapped in quotes. Also, there's a space issue.
print("Hello World")
The text Hello World has no quotes, so Python sees two "words" it doesn't recognize and gets confused. Always wrap text in quotes: print("Hello World").
Fill in the Blank

The file extension for a Python script file is ______.

It's a short three-letter extension starting with "py".
.py (for example: hello.py)
Python scripts are saved with the .py extension so that Python (and humans) know it's a Python file.
Debugging

The code below gives an error. Fix it.

print("I am learning Python")
Look carefully at the ending — count the closing quotes and parentheses.
The string ends with ") — the quote and parenthesis are swapped. Correct: print("I am learning Python")
Reading left to right: " opens the string, the text follows, then " closes the string, and finally ) closes the function call. If the order is wrong, Python raises a syntax error.
Write the Code

Write a Python program that prints your name, your favorite food, and the result of 25 divided by 5 — each on its own line.

You need three print() calls. Remember: text needs quotes, math doesn't.
print("Ravi")
print("Pizza")
print(25 / 5)
Three separate print() statements run top to bottom. Names and foods are text → quotes. 25 / 5 is math → no quotes → Python calculates 5.0 and prints it.
Challenge

Print a simple face using text on one line using only ONE print(). (Hint: it may help to know \n makes a new line inside a string.)

Use \n between the rows of your face inside one set of quotes.
print(" -----\n| o o |\n|  ^  |\n -----")
\n is a special character called a newline — it moves to the next line. Even though it's one print(), the output has 4 lines. This is a sneak peek at strings, which we study deeply in Topic 13.