Introduction to Python Programming
Python Basics
Setting Up Your Workspace
Before you can write any Python code, you need to have Python installed on your computer. The good news is that many systems, especially those running macOS or Linux, come with Python pre-installed. For Windows users, or if you need to install it yourself, the best place to go is the official Python website, python.org.
Once Python is installed, you have a couple of ways to write and run code. You can use a simple text editor and run your code from the command line, but it's often easier to use an Integrated Development and Learning Environment, or IDLE. This program comes bundled with the standard Python installation and gives you a place to write code and see its output immediately.
Your First Program
Let's start with a classic tradition in programming: making the computer say hello. In Python, this is incredibly straightforward. All you need is the print() function.
print("Hello, World!")
That's it. This one line tells Python to display the text inside the parentheses on the screen. The text is wrapped in double quotes to tell Python that it's a string of characters, not a command to be interpreted.
Syntax
noun
The set of rules that defines how a program must be written to be correctly interpreted by a computer.
One of the most unique aspects of Python's syntax is its use of indentation, meaning the spaces at the beginning of a line. While it doesn't matter for a simple one-line program, it's how Python groups blocks of code together. Other languages might use curly braces {} for this, but Python uses whitespace. This enforces a clean, readable style right from the start.
In Python, indentation isn't just for looks—it's a rule that the interpreter enforces. Consistent spacing is key.
Storing Information
Programs need to work with data, and we need a way to store that data. We do this using variables. Think of a variable as a labeled box where you can keep a piece of information. You give the box a name, and you can put things in it, take them out, or replace them.
# Create a variable named 'message' and store text in it.
message = "Welcome to Python!"
# Print the contents of the 'message' variable.
print(message)
In this example, we created a variable named message and used the assignment operator (=) to store the text "Welcome to Python!" inside it. Then, we passed the variable to the print() function, which displayed the value stored within.
Every piece of data in Python has a specific type. Let's look at the most common ones.
| Data Type | Description | Example |
|---|---|---|
String (str) | A sequence of characters, like text. | "Hello" or 'Python' |
Integer (int) | A whole number, without a decimal. | 42 or -100 |
Float (float) | A number with a decimal point. | 3.14 or -0.5 |
Boolean (bool) | Represents one of two values: True or False. | True or False |
Python is smart enough to figure out the data type on its own when you create a variable. You don't have to declare it explicitly.
Interacting with Users
Besides displaying information, programs often need to get information from the user. Python makes this easy with the input() function. It prompts the user to type something and then captures that text.
# Ask the user for their name and store it in a variable.
name = input("What is your name? ")
# Create a personalized greeting.
greeting = "Hello, " + name + "!"
# Print the greeting.
print(greeting)
Here, the program first prints "What is your name? " and waits. Whatever you type is stored in the name variable. Notice the + sign. When used with strings, it concatenates them, or joins them together, to form a new string.
One important detail: the
input()function always returns a string. If you need to treat the input as a number, you'll have to convert it first. We'll explore how to do that later.
Now that you have a handle on the absolute basics, let's test your knowledge.
What is the official website to download Python?
Which line of code correctly prints the text "Hello Python!" to the screen?
You've taken your first steps into Python. You know how to set up your environment, write a simple program, use variables to store different types of data, and handle basic input and output. These are the fundamental building blocks for everything that comes next.
