Lesson 1 of 25

Introduction to Python

What Python Actually Is

Python is a general-purpose programming language: a set of rules for writing instructions, plus a program that reads those instructions and carries them out. It was created by Guido van Rossum and released in 1991, and its guiding idea has never really changed — code is read far more often than it is written, so the language should optimise for the reader.

That sounds like a slogan until you compare two lines that do the same job. In many languages, printing a message means remembering a type declaration, a semicolon, and a pair of braces. In Python it is print("Hello"). Nothing has been hidden from you; the language simply refuses to make you write ceremony that the machine could work out on its own.

The practical consequence for you, learning this as a first language, is that you will spend your first few weeks thinking about the problem rather than about the language. That is genuinely rare. The trade-off is that Python hides some machinery — memory, types, how values are stored — and a few of the classic beginner bugs in this course come from that machinery leaking back into view. We will meet each of them head-on rather than pretending they do not exist.

  • High-level — you never allocate or free memory yourself; Python tracks which objects are still in use and reclaims the rest
  • Interpreted — you run a source file directly with python file.py; there is no separate compile step to produce an executable
  • Dynamically typed — a variable has no declared type, so x = 5 followed by x = "five" is legal
  • Strongly typed — but Python will not silently mix types: "5" + 5 raises a TypeError instead of guessing
  • Multi-paradigm — you can write plain step-by-step scripts, organise code into classes, or pass functions around like values

Your First Program, Read Line by Line

Type the code below into a file called hello.py and run it from a terminal with python hello.py. Four small ideas are packed into it, and it is worth naming all four before you move on.

A comment starts with # and is ignored entirely by Python; it exists for humans. Assignment with = creates a name and points it at a value. print() is a function call — the parentheses are what actually runs it. And the f before a string makes it an f-string, where anything inside curly braces is evaluated and its result dropped into the text.

F-strings are how you should build text in modern Python. The older habits — gluing pieces together with +, or using the % operator — still work, but concatenation breaks the moment one of the pieces is a number, because Python refuses to add a string to an integer. An f-string converts for you and reads in the same order as the sentence it produces.

Example
# hello.py — your first Python program

print("Hello, World!")

name = "Ananya"
lessons = 25

# f-string: expressions inside {} are evaluated and inserted
print(f"Welcome, {name}!")
print(f"This course has {lessons} lessons, so about {lessons * 20} minutes a week.")

# The old way — works, but fragile
print("Welcome, " + name + "!")
# print("You have " + lessons + " lessons")  # TypeError: can only concatenate str
Notes
  • Run a file with python hello.py. Typing just python opens the REPL, an interactive prompt marked >>> where each line runs as soon as you press Enter. The REPL is excellent for testing one idea; save real programs to a file.

What "Interpreted" Costs and What It Buys

When you run python hello.py, Python does not produce a standalone program the way a C or Java build does. It translates your source into an intermediate form called bytecode and then executes that bytecode step by step inside the Python interpreter. You may notice a __pycache__ folder appear next to your files; that is Python storing the bytecode so it does not have to redo the translation next time.

The cost is speed. A tight numerical loop written in pure Python is genuinely slower than the same loop in C — often by a large factor — because every operation goes through the interpreter. The benefit is the edit-run cycle: change a line, run again, see the result immediately, with no build to wait for. For the vast majority of programs you will write, the bottleneck is a database query, a network call, or a file on disk, and the interpreter's overhead is invisible next to those.

Where raw number-crunching genuinely matters, the Python ecosystem solved the problem a different way. Libraries such as NumPy and pandas keep their data in compact arrays and do the heavy arithmetic in compiled C code, while you keep writing readable Python at the top. This is why Python dominates data work despite being a slow interpreter — you are writing the instructions, not doing the arithmetic.

Notes
  • You will hear that "Python is slow". Treat it as a statement about tight loops, not about programs. Measure before you worry: a script that finishes in two seconds does not need rewriting because a benchmark says C is faster.

Indentation Is Syntax, Not Decoration

Most languages mark a block of code with curly braces and treat indentation as a courtesy to the reader. Python has no braces. The indentation is the block. Lines indented by the same amount under an if or a for belong to it; the first line that steps back out has left it.

This is the single most common source of errors in a beginner's first week, and the errors come in two flavours. IndentationError means the shape of your code does not make sense — you indented a line for no reason, or you wrote a colon and then forgot to indent the line under it. TabError means you mixed tab characters and spaces in the same block. Both lines can look identical on screen while being completely different to Python, which is why the fix is a rule rather than a judgement call: use four spaces, never tabs, and let your editor insert spaces when you press Tab.

The deeper trap is the code that runs but is wrong. Move a line one level in or out and Python will not complain — it will simply run that line at a different time. In the example below, the only difference is the indentation of the last print, and it changes the program from "report once at the end" to "report after every single item".

Example
total = 0
marks = [78, 84, 91]

# Correct: the summary runs once, after the loop
for m in marks:
    total += m
print(f"Total: {total}")        # Total: 253

# Same lines, one indented further: now it runs on every pass
total = 0
for m in marks:
    total += m
    print(f"Total: {total}")    # prints 78, then 162, then 253

# This is an error, not a style problem:
# if total > 200:
# print("Good")   -> IndentationError: expected an indented block
Notes
  • In VS Code, turn on "Render Whitespace" and set the Python indentation to 4 spaces. Seeing the difference between a tab and four spaces on screen removes an entire category of bug before it happens.

Where Python Is Actually Used

Python is not one thing. It is a general language that several very different communities settled on for very different reasons, and the version of "Python developer" you become depends on which of those communities you join. That matters when you are studying for placements, because the interview for a backend role and the interview for a data role overlap far less than the job titles suggest.

For campus hiring in India, two paths dominate. The first is data and analytics: pandas for tabular data, NumPy for arrays, matplotlib for charts, and scikit-learn once you move into machine learning. The second is backend web development: Django or Flask or FastAPI serving an API that a website or mobile app calls. Underneath both sits a third use that nobody advertises but everybody relies on — small automation scripts that rename files, clean a messy spreadsheet, or pull a report from an internal system every morning.

  • Data analysis and machine learning — pandas, NumPy, scikit-learn, PyTorch; the reason Python is the default language of data science
  • Backend web development — Django for large applications with a lot of built-in structure, Flask and FastAPI when you want something lighter
  • Automation and scripting — renaming thousands of files, scraping a page, generating reports on a schedule
  • Testing and DevOps — test suites with pytest, deployment and infrastructure tooling
  • Teaching and research — Jupyter notebooks, where code, output and explanation sit in one document

Python 3 Only — and Why Old Answers Mislead You

Everything in this course is Python 3. Python 2 reached end of life in January 2020 and receives no updates, not even security fixes. You will still meet it, though, because a great deal of the tutorial content and Stack Overflow answers on the internet were written before 2020 and nobody went back to update them.

Two differences catch people out immediately. In Python 2, print was a statement, so answers written then say print "hello" with no parentheses — that is a SyntaxError in Python 3. And in Python 2, dividing two integers threw away the remainder, so 5 / 2 gave 2. In Python 3, / always produces a float, and 5 / 2 is 2.5. If you want the old behaviour you ask for it explicitly with //.

The practical rule when you search for help: check the date on the answer, and if the code contains print without parentheses, treat the whole answer as suspect. It may still be conceptually right, but the syntax will not run.

Example
# Python 3 — what this course uses
print("hello")      # function call, parentheses required
print(5 / 2)        # 2.5   — true division, always a float
print(5 // 2)       # 2     — floor division, ask for it explicitly

# Python 2 style — SyntaxError in Python 3
# print "hello"
# 5 / 2  ->  2

# Check which version you are running
import sys
print(sys.version)  # 3.x.x ...
Notes
  • On many systems the command python may still point at an old install while python3 points at the current one. If python --version reports a 2.x number, use python3 and pip3 instead for every command in this course.
Ask AI