Introduction to Python Programming
Python Basics
Getting Started with Python
Before you can write Python code, you need two things: the Python interpreter and a place to write your code. The interpreter is a program that reads your Python code and carries out its instructions. You can download it for free from the official Python website, python.org.
Your code editor can be a simple text editor like Notepad or TextEdit, but most developers use an Integrated Development Environment (IDE). An IDE is a souped-up text editor with helpful features like syntax highlighting and debugging tools. Popular choices include Visual Studio Code, PyCharm, and Thonny.
The Rules of the Road
Python's syntax is known for being clean and readable. It's designed to look a lot like plain English, which makes it easier to learn than many other programming languages.
One of the most unique and important rules in Python is its use of indentation. Where other languages use brackets or keywords to group code, Python uses whitespace. How far you indent a line of code determines which block of code it belongs to. This might seem strange at first, but it forces you to write clean, organized code.
In Python, indentation isn't just for style, it's a rule. Getting it wrong will cause your program to break.
Storing Information
Programs need to store and manage information. We do this using variables. Think of a variable as a labeled box where you can put a piece of data. You give the box a name, and you can put things in it, take things out, or just see what's inside.
Each piece of data has a specific type. For now, we'll focus on four basic types.
| Data Type | Description | Example |
|---|---|---|
| Integer | A whole number, positive or negative. | 42, -100 |
| Float | A number with a decimal point. | 3.14, -0.5 |
| String | A sequence of characters, like text. Must be in quotes. | "Hello!", 'Python' |
| Boolean | Represents one of two values: True or False. | True, False |
Creating a variable is simple. You use the equals sign (=), which is called the assignment operator. It assigns the value on the right to the variable name on the left.
# This is a comment. Python ignores lines starting with #
# An integer variable
user_age = 25
# A float variable
pi_approx = 3.14159
# A string variable
user_name = "Alex"
# A boolean variable
is_learning = True
Talking to Your Program
A program isn't very useful if you can't interact with it. The two most basic ways to communicate are displaying output and receiving input.
To display information on the screen, you use the print() function. You can give it text directly (in quotes) or pass it a variable to display its value.
# Prints the text directly
print("Hello, World!")
# Prints the value of the user_name variable
user_name = "Alex"
print(user_name)
To get information from the user, you use the input() function. It pauses the program, waits for the user to type something and press Enter, and then returns whatever they typed as a string.
# Asks the user for their name and stores it
name = input("What is your name? ")
# Greets the user by name
print("Hello, " + name + "!")
With just these simple tools, you can start writing basic interactive scripts.
Time to check your understanding of these core concepts.
What is the primary role of the Python interpreter?
In Python, what is the purpose of indentation?
You now have the building blocks for writing simple Python programs. You know how to set up your environment, follow Python's syntax rules, store information in variables, and communicate with the user.
