Introduction to Python Programming
Python Basics
Getting Set Up
Before you can write Python code, you need to have Python installed on your computer. Think of it like needing flour and an oven before you can bake a cake. Python is the main ingredient, and your computer is the oven.
The best place to get Python is from its official website, python.org. The installation is straightforward, and it includes a simple program called IDLE (Integrated Development and Learning Environment). IDLE gives you a place to write and run your code right away.
Once installed, you can open IDLE or a terminal (like Command Prompt or PowerShell on Windows, or Terminal on Mac/Linux) and type python. If you see a >>> prompt, you're in Python's interactive shell. This is a great place to experiment and run code one line at a time.
Writing Your First Code
A computer program is just a list of instructions for the computer to follow. We'll start with the most classic first instruction in programming: telling the computer to say hello.
In Python, we use the print() command to display information on the screen. Whatever you put inside the parentheses will be shown as output.
print("Hello, World!")
Let's break this down. print is the command. The parentheses () hold what you want to print. The text "Hello, World!" is called a string—a sequence of characters. The quotation marks tell Python that this is text.
You can also leave notes in your code that Python will ignore. These are called comments, and they start with a # symbol. They're for you or other programmers to understand what the code is doing.
# This is a comment. Python ignores it.
print("This line will run.")
Storing and Using Data
Writing the same information over and over is tedious. Instead, we can store it in a variable. Think of a variable as a labeled box where you can keep a piece of information to use later.
You create a variable by giving it a name and using the equals sign (=) to assign it a value.
greeting = "Welcome to programming!"
print(greeting)
Here, greeting is the name of our variable, and it holds the string "Welcome to programming!". When we print greeting, Python looks up the value stored in that variable and displays it.
Variables can hold different types of data, not just text. These are called data types.
| Data Type | Description | Example |
|---|---|---|
String (str) | Textual data | "Python" |
Integer (int) | Whole numbers | 42 |
Float (float) | Numbers with decimals | 3.14159 |
Boolean (bool) | True or False values | True |
Python is smart enough to figure out the data type on its own. You don't have to declare it explicitly.
year = 2024 # An integer
pi_approx = 3.14 # A float
is_learning = True # A boolean
print(year)
print(pi_approx)
Interacting with the User
Programs become much more interesting when they can interact with a user. Just as print() sends information out, the input() command takes information in.
When you use input(), the program will pause and wait for the user to type something and press Enter. You can store their response in a variable.
name = input("What is your name? ")
print("Hello, " + name + "!")
The text inside the input() parentheses is the prompt shown to the user. In the print() statement, we use the + sign to combine, or concatenate, several strings into one.
One important detail:
input()always returns the user's entry as a string, even if they type numbers. If you need to treat the input as a number, you have to convert it first.
age_string = input("How old are you? ")
age_number = int(age_string) # Convert the string to an integer
# Now you can do math with it!
year_born = 2024 - age_number
print(year_born)
Here, int() is a function that converts a string into an integer. Similarly, float() converts to a float, and str() converts a value back into a string.
Which line of Python code correctly prints the message 'Ready to code!' to the screen?
What is the primary purpose of a variable in programming?
These are the first building blocks of Python. With just these tools, you can already write simple programs that display information, store data, and interact with a user.
