Introduction to Python Programming
Python Basics
Setting Up Your Workspace
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. You can download it for free from the official website, python.org.
While you can write Python code in any plain text editor, most developers use an Integrated Development Environment, or IDE. An IDE is a special text editor with helpful tools like syntax highlighting and debugging. Python comes with a simple one called IDLE, but beginner-friendly options like Thonny or VS Code are popular choices.
Your First Lines of Code
A long-standing tradition in programming is to make your first program display the text "Hello, World!". In Python, this is incredibly simple. You use the built-in print() function.
# This line of code will display a message in the output.
print("Hello, World!")
One of the first things you'll notice about Python is its clean and readable style. It achieves this by enforcing a strict rule: indentation matters. Unlike other languages that use brackets or keywords to group code, Python uses whitespace. This isn't just for looks; it's a fundamental part of the syntax. We'll see why this is critical when we get to more complex code structures.
Getting your program to communicate back to you is a core skill. The
print()function is your go-to tool for displaying information, variables, and results.
Variables and Data Types
Variables are like labeled boxes where you can store information. You give a variable a name and then place a value inside it. This makes your code more readable and allows you to work with data that can change.
# Assigning a string value to a variable named 'greeting'
greeting = "Welcome to Python!"
# Assigning an integer value to a variable named 'user_count'
user_count = 100
# You can then use the variable name to access its value
print(greeting)
print(user_count)
Python automatically figures out the type of data you store in a variable. The most common basic types are:
| Data Type | Description | Example |
|---|---|---|
String (str) | A sequence of characters (text) | "Alice" or 'Python' |
Integer (int) | A whole number | 42 or -10 |
Float (float) | A number with a decimal point | 3.14 or -0.5 |
Boolean (bool) | Represents True or False | is_active = True |
You can also ask the user for information using the input() function. This function always gives you back a string, so you might need to convert it to another type, like an integer, if you want to do math with it.
# input() captures user input as a string
name = input("What is your name? ")
# We convert the input string to an integer using int()
age_str = input("How old are you? ")
age = int(age_str)
print("Hello, " + name)
print("Next year you will be", age + 1)
Working with Operators
Operators are special symbols that perform operations on values and variables. Python has several types, but we'll start with the three most common ones.
Arithmetic Operators
adjective
Used to perform mathematical calculations.
These are your standard math symbols. You can add (+), subtract (-), multiply (*), and divide (/). There are also operators for exponentiation (**) and getting the remainder of a division, called the modulo operator (%).
a = 10
b = 3
print(a + b) # Addition: 13
print(a * b) # Multiplication: 30
print(a / b) # Division: 3.333...
print(a % b) # Modulo: 1 (10 divided by 3 is 3 with a remainder of 1)
Comparison operators are used to compare two values. The result of a comparison is always a Boolean value: True or False.
| Operator | Meaning | Example (a=5, b=8) | Result |
|---|---|---|---|
== | Equal to | a == 5 | True |
!= | Not equal to | a != b | True |
> | Greater than | a > b | False |
< | Less than | a < b | True |
>= | Greater than or equal to | b >= 8 | True |
<= | Less than or equal to | a <= 5 | True |
Finally, logical operators (and, or, not) are used to combine Boolean values. They are the foundation of decision-making in your programs.
andisTrueonly if both sides are true.orisTrueif at least one side is true.notinverts a value (TruebecomesFalse, and vice-versa).
x = 12
# Is x greater than 10 AND less than 20?
print(x > 10 and x < 20) # True
# Is x less than 5 OR greater than 10?
print(x < 5 or x > 10) # True
# Is it NOT true that x is equal to 12?
print(not x == 12) # False
With these building blocks—variables, types, input/output, and operators—you have the tools to start writing simple but powerful scripts.
