No history yet

Python Basics

What is Python?

Python is a high-level programming language known for its readability and simplicity. Think of it as a set of instructions you can give a computer in a language that's much closer to English than to the complex machine code computers actually understand. Its clean syntax makes it a popular first language for beginners, but it's also powerful enough for major companies like Google, Netflix, and NASA to use for everything from web development to data analysis.

Lesson image

Python was created in the late 1980s by Guido van Rossum. His goal was to create a language that was easy to read and write, emphasizing code readability and a design philosophy that allows programmers to express concepts in fewer lines of code than might be used in languages like C++ or Java.

Getting Python on Your Computer

Before you can write Python code, you need to install the Python interpreter on your computer. The interpreter is a program that reads your Python code and translates it into instructions the computer can execute. The official source for Python is the Python Software Foundation's website, python.org.

Lesson image

Head to the website and download the latest version for your operating system. The installation process is typically straightforward. During installation on Windows, make sure to check the box that says "Add Python to PATH." This allows you to run Python from your computer's command line or terminal, which is a common way to run Python scripts.

Writing Your First Program

It's a tradition in programming to start by writing a program that prints "Hello, World!" to the screen. This simple task confirms that your setup is working correctly.

Open a plain text editor (like Notepad on Windows or TextEdit on Mac) and type the following line of code:

print("Hello, World!")

Save the file as hello.py. The .py extension is important as it tells the computer that this is a Python file.

Now, open your terminal or command prompt. Navigate to the directory where you saved your file and type the following command, then press Enter:

python hello.py

If everything is set up correctly, you should see the text Hello, World! printed in the terminal. Congratulations, you've just run your first Python program!

Indentation and Comments

Two fundamental concepts in Python are indentation and comments. Unlike many other languages that use curly braces {} to define blocks of code, Python uses indentation. This means the whitespace at the beginning of a line is not just for style; it's part of the syntax.

Think of indentation like an outline. Sub-points are indented under a main point to show they belong to it. In Python, indented lines of code belong to the line above them that isn't indented as far.

You'll see how this works when we get to control structures like loops and conditional statements. For now, just remember that indentation is meaningful in Python. Incorrect indentation will cause your program to fail.

Comments are notes for human readers, not for the computer. The Python interpreter ignores them completely. You can add a comment to your code by starting a line with the hash symbol (#).

# This is a comment. The interpreter will ignore it.
print("This line will be executed.")

Comments are essential for explaining what your code does, especially as your programs become more complex. They help others (and your future self) understand your thought process.

Quiz Questions 1/6

Who is credited with creating the Python programming language in the late 1980s?

Quiz Questions 2/6

What is the primary purpose of indentation in Python?