Introduction to Python Programming
Python Basics
Getting Your Tools Ready
Before you can write Python code, you need to have the Python interpreter installed on your computer. Think of the interpreter as a translator that converts your Python code into instructions the computer can understand. The best place to get it is from the official Python website, python.org. Be sure to download the latest version of Python 3, as Python 2 is no longer supported.
Once Python is installed, you can write code in any plain text editor. However, most developers use an Integrated Development Environment, or IDE. An IDE is a software application that provides comprehensive facilities to programmers for software development. An IDE normally consists of at least a source code editor, build automation tools, and a debugger. Popular choices include Visual Studio Code, PyCharm, and Spyder. They offer helpful features like syntax highlighting, which colors your code to make it easier to read, and code completion.
You can test your installation by opening your computer's terminal or command prompt and typing python --version or python3 --version. If it's installed correctly, you'll see the version number printed back to you.
Syntax and Structure
Python's syntax is known for being clean and readable. One of its most distinctive features is the use of indentation to mark blocks of code. Unlike other languages that use curly braces {} or keywords, Python uses whitespace. This might seem strange at first, but it enforces a clean, consistent coding style.
In Python, indentation is not just for looks; it's a rule. A block of code, like the body of a function or a loop, must be indented with the same number of spaces.
The standard is to use four spaces for each level of indentation. Most code editors can be configured to insert four spaces automatically when you press the Tab key. Consistent indentation is mandatory. If you mix tabs and spaces, or indent inconsistently, your code will produce an error.
# This is a comment. It's ignored by the interpreter.
print("This line is not indented.")
if True:
# This block is indented.
print("This line is indented by four spaces.")
print("So is this one.")
print("This line is back at the first level.")
Storing Information
In programming, we constantly need to store and manage information. We do this using variables. A variable is like a labeled container where you can store a piece of data. You give it a name, and then you can refer to that data by its name throughout your program.
# Assigning the value 10 to a variable named 'age'
age = 10
# Assigning the text "Alice" to a variable named 'name'
name = "Alice"
# Now we can use the variable names to access the data
print(name)
print(age)
Data comes in different forms, called data types. Python is smart and usually figures out the data type on its own when you assign a value to a variable. Here are the most common basic types:
| Data Type | Description | Example |
|---|---|---|
int | Integer, for whole numbers. | 42, -100 |
float | Floating-point number, for decimals. | 3.14, -0.5 |
str | String, for sequences of text characters. | "Hello, world!", 'Python' |
bool | Boolean, for true or false values. | True, False |
You can check the type of any variable using the type() function.
temperature = 98.6
print(type(temperature)) # This will output: <class 'float'>
Interacting with Programs
A program isn't very useful if it can't communicate. The most basic way to show information is with the print() function. It displays whatever you put inside its parentheses on the screen.
print("Welcome to the program!")
planet = "Earth"
print("We are currently on planet", planet)
To get information from a user, you can use the input() function. It prompts the user to type something and then returns their input as a string.
user_name = input("What is your name? ")
print("Hello,", user_name)
One important detail: the input() function always gives you a string, even if the user types a number. If you need to treat the input as a number, you have to convert it first.
age_string = input("How old are you? ")
# Convert the string to an integer to do math
age_number = int(age_string)
years_to_100 = 100 - age_number
print("You will be 100 in", years_to_100, "years.")
We've just introduced a few new concepts. Let's take a moment to test your understanding.
What is the primary role of the Python interpreter?
In Python, what is used to define a block of code (e.g., the body of a loop or function)?
Finally, let's look at operators. These are the symbols that perform operations on variables and values. You're already familiar with many of them from math class.
| Operator Type | Symbols | Purpose |
|---|---|---|
| Arithmetic | +, -, *, /, % | Perform addition, subtraction, multiplication, division, and modulus (remainder). |
| Comparison | ==, !=, <, >, <=, >= | Compare two values and return True or False. |
| Logical | and, or, not | Combine boolean expressions. |
Expressions are combinations of values, variables, and operators that Python evaluates to produce a new value.
# Arithmetic expression
area = 12.5 * 4
# Comparison expression
is_adult = age_number >= 18
# Logical expression
can_enter = is_adult and (user_name != "Voldemort")
print(can_enter)
Understanding these building blocks—syntax, variables, types, I/O, and operators—is the first major step toward writing powerful Python programs.
