Getting Started with Python
Python is one of the most popular and versatile programming languages in the world. It is used by beginners learning to code for the first time and by professional engineers at companies like Google, NASA, Netflix, and Spotify. This chapter gives you the foundation you need to start writing real Python code.
Why This Chapter Matters
Understanding where Python runs, how to write your first programs, and how to use the console effectively will make every chapter that follows easier to understand.
- it gives you a working development environment
- it teaches you to read and write Python syntax
- it shows you how to run code and observe results
- it builds habits that reduce bugs from the start
What Python Is
Python is a high-level, interpreted programming language. That means:
- you write human-readable code
- Python translates and runs it line by line, without a separate compilation step
- errors appear quickly because you can test small pieces of code interactively
Python is designed for readability. It uses indentation to define structure instead of curly braces or keywords like end.
# This is readable Python
if temperature > 30:
print("It is hot today")
Why Python Is So Popular
- Beginner-friendly: Clean syntax that looks like plain English
- Versatile: Used for web dev, data science, machine learning, automation, scripting, games
- Massive ecosystem: Hundreds of thousands of packages on PyPI (Python Package Index)
- Cross-platform: Runs on Windows, macOS, Linux
- Strong community: Huge number of tutorials, forums, and open-source projects
Where Python Runs
Python can run in several environments:
- Terminal / Command Prompt: Run
.pyscript files directly - Interactive REPL: Type and execute code line by line
- Jupyter Notebooks: Popular for data science and exploration
- IDEs: VS Code, PyCharm, Thonny
- Online: Replit, Google Colab, Python.org/shell
Installing Python
Download Python from the official site:
After installing, verify it works:
python --version
# or on some systems:
python3 --version
You should see something like Python 3.12.0.
The Python REPL
The REPL (Read–Eval–Print Loop) is an interactive Python session.
Start it in your terminal:
python
You will see a prompt:
>>>
Try it:
>>> 2 + 2
4
>>> "hello".upper()
'HELLO'
>>> print("I am learning Python!")
I am learning Python!
Type exit() or press Ctrl+D to leave the REPL.
Your First Script
Create a file called hello.py:
# My first Python script
print("Hello, World!")
print("I am learning Python!")
Run it from the terminal:
python hello.py
Output:
Hello, World!
I am learning Python!
The print() Function
print() is the most fundamental output tool in Python.
print("Simple text")
print(42)
print(3.14)
print(True)
print("Name:", "Asha")
print("Score:", 95, "Grade:", "A")
Key behaviors:
- By default,
print()adds a newline after each call - You can separate multiple values with commas
- Use
end=""to suppress the newline - Use
sep=","to change the separator
print("Hello", "World", sep="-") # Hello-World
print("Loading", end="...") # Loading...
Comments
Comments help you explain your code. Python ignores them at runtime.
Single-Line Comments
# This is a single-line comment
x = 10 # inline comment on a code line
Multi-Line Comments (Docstrings)
Python does not have dedicated multi-line comment syntax, but triple-quoted strings are commonly used:
"""
This is a multi-line description.
Often used as module or function documentation.
"""
Good comments explain why, not what:
# Bad comment:
x = x + 1 # adds 1 to x
# Good comment:
x = x + 1 # compensate for the zero-based index offset
Indentation
Python uses indentation (whitespace) to define code blocks. This is not optional — it is a core part of the language syntax.
if 5 > 3:
print("Five is greater") # indented — part of the if block
print("This also runs") # same block
print("This always runs") # not indented — outside the if
Standard indentation is 4 spaces per level. Tabs work too but mixing tabs and spaces causes errors.
Values, Expressions, and Statements
- A value is a piece of data:
42,"hello",True - An expression produces a value:
2 + 3,len("hello") - A statement performs an action:
print(...),x = 5
42 # value
2 + 3 # expression
print(2 + 3) # statement
x = 10 # assignment statement
Python Version Notes
This course uses Python 3, which is the current version. Python 2 is no longer supported. Key differences you may notice in older examples:
- Python 2:
print "hello"(no parentheses) - Python 3:
print("hello")(parentheses required)
Always verify you are running Python 3 with python --version.
Common Beginner Mistakes
- Running code in the wrong directory
- Mixing tabs and spaces for indentation
- Forgetting the colon
:at the end ofif,for,deflines - Using Python 2 syntax when running Python 3
- Not reading error messages carefully
Debugging Tips
- Read the full error message — Python tells you the line number and what went wrong
- Use
print()statements to inspect values at key points - Test one small piece of code at a time
- Use the REPL to quickly experiment
x = "hello"
print(type(x)) # <class 'str'> — always check your types when confused
Mini Exercises
- Install Python and confirm the version in your terminal.
- Open the REPL and type five different mathematical expressions.
- Write a script that prints your name, age, and favorite hobby.
- Add three different comments: a file-level docstring, a section comment, and an inline comment.
- Intentionally write code that causes an indentation error and read the error message carefully.
Review Questions
- What makes Python different from many other languages in terms of syntax?
- What does REPL stand for and why is it useful?
- Why does Python use indentation instead of curly braces?
- What is the difference between a value, an expression, and a statement?
- How do you run a Python script from the terminal?
Reference Checklist
- I can install Python and verify the version
- I can open and use the Python REPL
- I can write and run a
.pyscript file - I understand how Python uses indentation to define blocks
- I can write single-line and multi-line comments
- I know how to use
print()with multiple values and separators