No history yet

Python Basics

Getting Started with Python

Python is a popular programming language known for its clear and readable syntax. It's often described as being close to plain English, which makes it a great choice for beginners. Before you can start writing code, you need to set up a place for it to run on your computer.

Setting Up on Windows

First, you'll need to install the Python interpreter. This is the program that reads your Python code and carries out its instructions. You can download it directly from the official Python website, python.org.

During installation, you'll see several options. The most important one is a checkbox that says "Add Python to PATH." Make sure you check this box. It allows you to run Python from any folder on your computer using the command prompt, which is a simple text-based interface for your operating system.

Lesson image

After the installation finishes, you can check that everything is working. Open your command prompt (you can find it by searching for "cmd" in the Start Menu) and type the following command, then press Enter.

python --version

If you see a version number like Python 3.11.3, the installation was successful. You're now ready to write some code.

Writing and Running Code

The simplest way to run Python code is through the interactive interpreter. Just type python in your command prompt and press Enter. You'll see a >>> prompt, which means Python is ready to take your commands.

The classic first program is one that just prints a message to the screen. To do this, we use the print() function.

print("Hello, Python!")

Type that line into the interpreter and press Enter. Python will immediately execute it and display Hello, Python!. This interactive mode is great for testing small snippets of code.

For anything more complex, you'll save your code in files. Python files always end with the .py extension. You can create one using any simple text editor, like Notepad. Save a file named my_first_script.py with the same print() command inside. To run it, navigate to the correct folder in your command prompt and type:

python my_first_script.py

You’ll see the same output as before. One critical rule in Python is indentation. The spacing at the beginning of a line is meaningful and organizes your code. For now, just make sure your commands start at the very beginning of the line, with no extra spaces.

Storing Information

Programming involves working with data. To keep track of it, we use variables. A variable is like a labeled box where you can store a piece of information. You create one by giving it a name and assigning it a value with the equals sign (=).

message = "Welcome to programming"
year = 2024
price = 19.99

Here, we've created three variables. Each one holds a different type of data:

  • String: A sequence of characters, like text. You must enclose strings in either single (') or double (") quotes. "Welcome to programming" is a string.
  • Integer: A whole number, like 2024.
  • Float: A number with a decimal point, like 19.99.

Once you've stored data in a variable, you can use the variable's name to access it.

print(message)
print(year)

This code would print the string and the integer we stored earlier. You can also change a variable's value by assigning something new to it.

Working with Operators

Operators are symbols that perform operations on values and variables. You already know many of them from basic math.

OperatorDescriptionExample
+Addition5 + 3
-Subtraction10 - 4
*Multiplication6 * 7
/Division20 / 5

You can use these operators with numbers to form expressions. Python evaluates the expression and gives you back a single value.

apples = 12
oranges = 8

total_fruit = apples + oranges
print(total_fruit) # This will print 20

The + operator also works on strings. When used with strings, it concatenates them, which means it joins them together end-to-end.

first_name = "Grace"
last_name = "Hopper"

full_name = first_name + " " + last_name
print(full_name) # This will print "Grace Hopper"

Notice we added a string containing a single space (" ") to separate the first and last names. Trying to add a string to a number will cause an error, as Python doesn't know how to combine different data types in that way.