No history yet

Python Basics

What is Python?

Python is a popular programming language created by Guido van Rossum and first released in 1991. Its design philosophy emphasizes code readability with its notable use of significant indentation. Think of it as a language that wants you to write clean, logical code, whether you're building a small script or a massive application.

Lesson image

It's considered a high-level language, which means it handles a lot of the computer's complexity for you, like memory management. This lets you focus on what you're trying to build. It's also an interpreted language. Instead of compiling your code into machine language beforehand, an interpreter runs through the code line by line and executes each command. This makes the development process faster and more flexible.

One of Python's guiding principles is "Readability counts." Clean, easy-to-read code is a hallmark of the language.

Getting Set Up

Before you can write Python, you need two things: the Python interpreter and a place to write your code. The interpreter is the program that understands and executes your Python commands.

The easiest way to get started is to download the official installer from python.org. It's available for Windows, macOS, and Linux. The installer includes everything you need, including Python's standard library and a simple development environment called IDLE.

Lesson image

IDLE (Integrated Development and Learning Environment) is perfect for beginners. It gives you an interactive shell to test code snippets in real-time and a text editor to write and save longer programs. As you get more advanced, you might switch to a more powerful code editor or IDE like VS Code or PyCharm, but IDLE is the best place to start.

Your First Program

Let's write your first line of Python code. It's a tradition in programming to start with a program that simply displays "Hello, World!" on the screen. In Python, this is incredibly simple. We use the print() function.

print("Hello, World!")

You can run this in two ways using IDLE:

  1. Interactive Shell: Open IDLE, and you'll see a prompt that looks like >>>. Type the code directly into the shell and press Enter. It will execute immediately.
  2. Script File: In IDLE, go to File > New File. This opens a text editor. Type your code, then save the file with a .py extension, like hello.py. You can then run the script by pressing F5 or selecting Run > Run Module.
Lesson image

As your programs get more complex, you'll want to leave notes for yourself or for other programmers. These are called comments. In Python, any line that starts with a hash symbol (#) is a comment and is completely ignored by the interpreter.

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

Variables and Data Types

A variable is like a container for storing a value. You give it a name and use the equals sign (=) to assign a value to it. This makes your code more readable and allows you to work with data that can change.

message = "Hello, Python learner!"
print(message)

In the example above, message is the variable, and it holds the text "Hello, Python learner!". When we print the variable, the program outputs the value it contains.

Python automatically understands what type of data a variable holds. Here are a few basic types:

Data TypeDescriptionExample
String (str)A sequence of characters."Hello" or 'Python'
Integer (int)A whole number.10, -5, 12345
Float (float)A number with a decimal point.3.14, -0.5, 2.0

You can combine and manipulate these types. For instance, you can perform math with numbers. Let's see it in action.

# Assigning some numbers to variables
apples = 5
price_per_apple = 1.25

# Calculating the total cost
total_cost = apples * price_per_apple

print(total_cost)

When you run this code, it will calculate 5 * 1.25 and print the result, which is 6.25. You've just written a program that uses variables, performs a calculation, and displays the result. That's a huge step!

Quiz Questions 1/5

Who is credited with creating the Python programming language?

Quiz Questions 2/5

What does it mean that Python is a high-level language?

You've now covered the absolute basics: what Python is, how to set it up, and how to write and run simple programs using functions, comments, and variables. This is the foundation for everything that comes next.