๐ŸIntroduction to PythonLESSON

Introduction to Python

Python is one of the world's most popular programming languages, and for good reason. Created by Guido van Rossum and first released in 1991, Python was designed with a clear philosophy: code should be readable, and there should be one obvious way to do things. Today, Python powers everything from Instagram's backend to NASA's data analysis pipelines.

What Makes Python Special?

Python stands out from other languages in several key ways:

Readability first. Python uses indentation to define code blocks instead of curly braces. This forces you to write visually clean code by default.

Batteries included. Python ships with an enormous standard library. Need to parse JSON, send HTTP requests, work with dates, or do matrix math? There's almost certainly a built-in module for it.

Versatile. Python excels at web development (Django, FastAPI), data science (NumPy, pandas), machine learning (PyTorch, TensorFlow), automation/scripting, and system administration.

Dynamic typing. You don't declare variable types. Python figures out types at runtime, making exploratory coding fast and flexible.

Python vs Other Languages

FeaturePythonJavaScriptJava
TypingDynamicDynamicStatic
SyntaxIndentationCurly bracesCurly braces
ExecutionInterpretedJIT/InterpretedCompiled to bytecode
Primary useGeneral purposeWeb/NodeEnterprise

Anatomy of a Python Script

Let's look at a simple Python script and break down each piece:

The print() Function

print() is your primary tool for output. It accepts multiple arguments separated by commas:

Comments

Comments explain your why, not your what. Python ignores everything from # to the end of the line:

Indentation Rules

Python is strict about indentation. Use 4 spaces (not tabs) as the Python community standard:

Mixing tabs and spaces causes a TabError. Configure your editor to insert spaces when you press Tab.

The Python REPL

REPL stands for Read-Eval-Print Loop. Open a terminal and type python3 to start it:

The REPL is perfect for experimenting. Each expression you type is evaluated immediately and its result displayed. Use exit() or Ctrl+D to quit.

Running Python Scripts

Save your code in a .py file and run it from the terminal:

Python 3 vs Python 2

Python 2 reached end-of-life on January 1, 2020. Always use Python 3. Key differences:

  • print is a function in Python 3: print("hello") not print "hello"
  • Division works correctly: 7 / 2 = 3.5 in Python 3, 3 in Python 2
  • Strings are Unicode by default in Python 3
  • range() returns an iterator in Python 3, a list in Python 2

Check your version with:

The Standard Library

Python's standard library is massive. Here are some essential modules you'll use frequently:

You'll explore many of these modules as you progress through this course. The standard library is one of Python's greatest strengths โ€” most common programming tasks don't require any third-party packages.

Your First Real Script

Here's a script that demonstrates several Python concepts working together:

This script imports a module, uses conditionals, takes user input, and formats output โ€” all core Python concepts you'll master in this course.

Knowledge Check

What character does Python use to mark the beginning of a comment?

What does REPL stand for in the context of Python's interactive shell?

Which Python version should you use for new projects today?