No history yet

Python Basics

Setting Up Your Workspace

Before you can write Python code, you need a place to write and run it. This involves two key components: the Python interpreter and a code editor.

The interpreter is what reads your Python code and translates it into instructions the computer can understand. Think of it as a translator that speaks both Python and your computer's native language. You can download the official Python interpreter for free from python.org. The installation is straightforward and includes a simple built-in editor called IDLE.

Lesson image

IDLE is great for starting out. As you get more experienced, you might move to more advanced code editors like VS Code, PyCharm, or Sublime Text. For now, IDLE has everything we need.

Your First Lines of Code

Let's start with a classic programming tradition: making the computer say hello. In Python, this is incredibly simple. Python's syntax, or its set of rules for how programs are written, is designed to be clean and readable.

You'll often hear that Python code looks a lot like plain English, which is one reason it's so popular with beginners.

To display text on the screen, we use a built-in function called print(). A function is a reusable block of code that performs a specific action. You just put the text you want to display inside the parentheses, wrapped in quotes.

print("Hello, World!")

That's it! Running that single line will output Hello, World! to your screen.

Now, let's try making our program a bit more interactive. We can get information from the user with another function called input(). This function displays a prompt to the user and waits for them to type something and press Enter.

# The text inside input() is the prompt shown to the user.
input("What is your name? ")

Storing and Using Information

Getting input is useful, but we need a way to remember it. For that, we use variables. A variable is like a labeled box where you can store a piece of information. You give it a name and use the equals sign (=) to put something inside it.

# Store the user's name in a variable called 'name'
name = input("What's your name? ")

# Now we can use the 'name' variable
print("Hello, " + name)

In the example above, whatever the user types is stored in the name variable. Then, we use the print() function to combine the string "Hello, " with the value stored in name.

Everything you store in a variable has a specific data type. For now, we'll focus on the three most common ones.

Data TypeDescriptionExample
String (str)A sequence of characters, like text. Always wrapped in quotes."Hello" or 'Python'
Integer (int)A whole number, without a fractional part.42, -100
Float (float)A number that has a decimal point.3.14, -0.5

Python is smart enough to figure out the data type on its own when you create a variable. The value you get from the input() function is always a string, even if the user types a number. We'll explore how to work with different types later on.

Quiz Questions 1/5

What is the primary role of the Python interpreter?

Quiz Questions 2/5

Which line of code will correctly display the text Hello, Python! on the screen?

You've taken your first steps into Python. You know how to set up your environment, display output, get input, and store information in variables. This is the foundation for everything that comes next.