Introduction to Python
What Python is, how the interpreter runs your code, and your first program.
What you will be able to do
- Say what Python is and what it is commonly used for
- Explain what the interpreter does with your file
- Tell the REPL and a .py file apart, and know when to use each
- Write and run your first program
- Use print(), write comments, and indent a block correctly
- Recognise that keywords are reserved and cannot be used as names
The idea, in plain English
Python is a programming language you write as plain text. Nothing you write is understood by your computer directly - something has to translate it, and in Python that something is called the interpreter. The usual one is CPython, which is what you get from python.org, and it reads your file top to bottom and carries out each line as it goes.
That "as it goes" is the difference people mean when they call Python interpreted rather than compiled. There is no separate build step producing an executable. You save a file, you run it, it does something. The cost is that a mistake on line 40 is not discovered until line 40 actually runs.
Python is used for backend APIs, data analysis, machine learning, automation, and scripting - which is a broad enough list that "what is Python for" has no short answer. What those uses share is that Python optimises for the reader rather than the machine, which is why it is a reasonable first language and still a reasonable twentieth one.
One thing surprises almost everyone coming from another language: in Python, indentation is not formatting. It is syntax. The spaces at the start of a line are how the interpreter knows which lines belong to an if or a loop, so getting them wrong is an error rather than an untidy diff.
Worked example: A three-line program that greets you by name.
The REPL and a .py file
Typing python with no arguments opens the REPL - read, evaluate, print, loop. You type one line, it answers immediately, and nothing is saved. It is the right tool for checking what a function does or what type something is.
A .py file is a program you keep. You write the whole thing, save it, then run it with python myfile.py. Everything past this lesson is a file; the REPL stays useful for the ten seconds when you just want to know whether len("hello") is 5.
Tip: In the REPL, an expression prints its value without print(). In a file it does not. That difference confuses a lot of beginners who test in one place and run in the other.
Syntax and examples
python3 --version # should print 3.10 or later
python3 # opens the REPL; exit() to leave# This is a comment. Python ignores it; it is for people.
print("Hello, Python!")python3 hello.pyname = "Chandu"
print("Hello,", name)
print("Your name is", len(name), "characters long.")age = 20
if age >= 18:
print("Adult") # indented: belongs to the if
print("Can vote") # same block
print("Done") # not indented: always runsx = 42
print(type(x)) # <class 'int'> - what kind of thing is it
print(id(x)) # a number identifying this exact object
help(print) # the documentation for print, in your terminalWatch out: On many systems python means Python 2 or nothing at all, while python3 is the one you want. If python3 --version works and python does not, use python3 everywhere in this course.
Built-in functions used in this lesson
Built-ins are always available - you never import them. These four are the ones worth knowing on day one.
print()Writes its arguments to the screen, separated by spaces, then a newline.
print("Hello,", name)type()Tells you what kind of object something is.
type(42) # <class 'int'>
id()The identity of an object - effectively its address. Useful later for understanding when two names point at one object.
id(x)
help()Prints the documentation for anything you pass it.
help(len)
Keywords you cannot use as names
These 35 words are reserved by the language. Trying to use one as a variable name is a syntax error - which is a good error to have seen once.
Control flowif, elif, else, for, while, break, continue, pass, match, case
Functions and classesdef, return, lambda, class, yield
ValuesTrue, False, None
Logicand, or, not, in, is
Errorstry, except, finally, raise, assert
Scope and modulesimport, from, as, global, nonlocal, del, with
Asyncasync, await
Try it yourself
The code does not change. Swap the content string and the program does something else entirely.
“print("Hello,", "world", "!")”
“print(2 + 3 * 4)”
“print(type(3.14))”
“print("unclosed string)”
What usually goes wrong
Python treats them as different, so a block that looks aligned can still be an IndentationError. Set your editor to insert 4 spaces for Tab and never think about it again.
Every line that opens a block - if, for, while, def, class - ends in a colon. Leaving it off gives a SyntaxError pointing at the next line, which is why it takes beginners so long to spot.
✗ if age >= 18
print("Adult")✓ if age >= 18:
print("Adult")Words like class, from, and is belong to the language. Adding a trailing underscore is the usual convention when you really want that word.
✗ class = "Physics"✓ class_name = "Physics"In the REPL, typing 2 + 2 shows 4. In a file it computes 4 and throws it away. In a file you have to print() it.
✗ # in hello.py
2 + 2✓ # in hello.py
print(2 + 2)Best practices
- Use 4 spaces per indentation level. It is what the standard style guide says and what every Python editor defaults to.
- Write comments that say why, not what. print("Hello") needs no comment; a magic number does.
- Name files in lowercase with underscores - user_service.py, not UserService.py.
- Keep the REPL open in a second terminal while you work. Checking something there is faster than adding a print and rerunning.
- Read the last line of an error message first. It names the error and usually the fix.
Practice
Write these yourself before opening anything. Getting them wrong first is most of how this sticks.
Write a program that prints your name and the current year on two separate lines.
Show hintHide hint
Two print() calls is the simplest answer, and simplest is right here.
Show solutionHide solution
print("Chandu")
print(2026)Print the result of 7 divided by 2, then the result of 7 divided by 2 using floor division. Note the difference.
Show hintHide hint
/ and // are different operators.
Show solutionHide solution
print(7 / 2) # 3.5 - true division, always a float
print(7 // 2) # 3 - floor division, drops the remainderUse type() to print the kind of each of these: 5, 5.0, "5", True, None.
Show hintHide hint
Five print() calls, or one with five arguments.
Show solutionHide solution
print(type(5)) # <class 'int'>
print(type(5.0)) # <class 'float'>
print(type("5")) # <class 'str'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>Write an if statement that prints "Even" when a number is divisible by 2 and "Odd" otherwise. Test it with 4 and 7.
Show hintHide hint
The modulo operator % gives the remainder. A remainder of 0 means divisible.
Show solutionHide solution
number = 7
if number % 2 == 0:
print("Even")
else:
print("Odd")A greeting card
Write a single program that introduces someone, using only what this lesson covered - variables, print(), comments, an if statement, and correct indentation.
- Store a name, an age, and a city in three variables
- Print a sentence that uses all three
- Print how many characters are in the name, using len()
- Print "Adult" or "Minor" depending on the age
- Include one comment that explains a decision, not a line
name = ""
age = 0
city = ""
# your code belowShow one solutionHide solution
name = "Chandu"
age = 34
city = "Hyderabad"
print("This is", name, "from", city, "- age", age)
print("The name has", len(name), "characters.")
# 18 is the threshold used here; change it if your rules differ.
if age >= 18:
print("Adult")
else:
print("Minor")Key points
- Python is interpreted: CPython reads your file top to bottom and runs each line as it reaches it.
- The REPL is for quick questions; a .py file is for programs you keep.
- print() is how a program shows you something. In a file, an expression on its own produces nothing visible.
- Indentation is syntax. Four spaces, consistently, is the convention.
- Every line that opens a block ends in a colon.
- Comments start with # and exist for people, not the interpreter.
- type() answers "what is this", id() answers "which object is this", and help() answers "how do I use this".
- 35 keywords are reserved and cannot be used as names.
Quick check before you move on
Interview questions
What does it mean that Python is dynamically typed?
A variable has no declared type; the type belongs to the value it currently refers to, and the same name can refer to a string now and an integer later. Types are still checked - at the moment an operation runs, not before.
Python is often called both interpreted and compiled. Why?
CPython compiles your source to bytecode first - the .pyc files in __pycache__ - and then interprets that bytecode on a virtual machine. There is a compile step; it just happens automatically and produces bytecode rather than machine code.
Why does Python use indentation instead of braces?
It removes the possibility of code that is indented one way and grouped another, which is a real source of bugs in brace languages. The trade-off is that whitespace becomes significant, so mixing tabs and spaces breaks things.
What is PEP 8 and why does it come up so often?
It is the style guide for Python code - 4-space indents, snake_case names, line length, import order. It matters because a language this readable loses most of that benefit if every file looks different.
Quiz
- 1.
Is Python compiled or interpreted, and what does that mean in practice?
- 2.
What is the difference between the REPL and a .py file?
- 3.
What happens if you indent with three spaces on one line and four on the next inside the same block?
- 4.
Why does 2 + 2 show 4 in the REPL but nothing when run in a file?
- 5.
What is CPython?
Comments
Sign in to leave a comment. Your name and photo come from Google; nothing else is shared.
Loading comments...
AI
System Design
Backend
- GraphQL8 modules · 69 lessons planned
- Core Python13 modules · 75 lessons planned
- FastAPI5 sections · 20 lessons
- Node.js14 modules · 206 lessons planned
- Node.js Performance7 chapters · 36 topics
- Event Loop Lifecycle6 phases · 3 scenarios
- Docker & Containerization11 modules · 144 lessons planned
- AWS for Developers14 modules · 219 lessons planned
- CI/CD & DevOps Automation10 modules · 134 lessons planned