Introduction to Python Programming
Python Basics
Getting Started with Python
Before you can write Python code, you need to set up your workshop. This means installing the Python interpreter, which is the program that understands and runs your code. You can download it for free from the official Python website, python.org. The installation is straightforward, just like any other software.
Next, you need a place to write your code. While you can use a basic text editor, most programmers use an Integrated Development Environment, or IDE. An IDE is like a supercharged text editor with helpful features like syntax highlighting, which colors your code to make it easier to read, and debugging tools to help you find errors. For beginners, an IDE called Thonny is a great choice because it's designed specifically for learning.
Python's Clean Syntax
One of Python's most famous features is its clean, readable syntax. It's designed to be uncluttered and look a lot like plain English.
A key part of this syntax is indentation. In many other programming languages, curly braces {} are used to group blocks of code. Python uses whitespace instead. This isn't just a style suggestion; it's a rule. Correct indentation tells the Python interpreter how your code is structured.
Consistent indentation is mandatory in Python. It's how the language groups statements together.
This might feel strange at first, but it forces you to write clean, organized code that's easy for you and others to read. Most IDEs will automatically handle indentation for you as you type.
Storing Information
Programming involves working with data. To keep track of this data, we use variables. Think of a variable as a labeled box where you can store a piece of information. You give the box a name and put something inside it.
message = "Hello, World!"
print(message)
Here, we created a variable named message and stored the text "Hello, World!" in it. The second line uses the print() function to display the contents of the variable.
Every piece of data in Python has a type. Let's look at the most common ones.
integer
noun
A whole number, positive or negative, without decimals.
user_age = 30
items_in_cart = -5 # This probably shouldn't happen!
float
noun
A number that has a decimal point.
price = 99.50
temperature = -10.5
string
noun
A sequence of characters, like text. Strings are enclosed in single (' ') or double (" ") quotes.
user_name = "Alice"
email_subject = 'Meeting Reminder'
boolean
noun
A type that can only have one of two values: True or False.
is_logged_in = True
has_permission = False
Python is a dynamically typed language. This means you don't have to explicitly state the data type of a variable. Python figures it out automatically based on the value you assign.
Making Things Happen
Once you have data in variables, you can perform operations on it. Python uses operators, which are special symbols that represent computations.
First up are arithmetic operators, which you'll recognize from math class.
| Operator | Name | Example |
|---|---|---|
+ | Addition | 5 + 2 |
- | Subtraction | 5 - 2 |
* | Multiplication | 5 * 2 |
/ | Division | 5 / 2 |
** | Exponent | 5 ** 2 |
% | Modulus (remainder) | 5 % 2 |
// | Floor Division | 5 // 2 |
apples = 10
oranges = 4
total_fruit = apples + oranges # Result is 14
apples_per_person = apples / 4 # Result is 2.5 (float division)
apples_divided_evenly = apples // 4 # Result is 2 (floor division)
apples_left_over = apples % 4 # Result is 2 (remainder)
Comparison operators are used to compare two values. The result of a comparison is always a boolean: True or False.
| Operator | Description | Example |
|---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
age = 21
is_adult = age >= 18 # Result is True
is_exact_age = age == 20 # Result is False
Finally, logical operators (and, or, not) are used to combine boolean values.
has_key = True
knows_password = False
can_open_door = has_key and knows_password # Result is False
can_enter_building = has_key or knows_password # Result is True
is_locked_out = not can_enter_building # Result is False
Interacting with Your Program
A program isn't very useful if it can't communicate with the outside world. We've already seen the print() function, which is the standard way to display output to the user.
item = "laptop"
price = 1200
print("The price of the", item, "is", price)
To get input from a user, you can use the input() function. It prompts the user to type something and press Enter. The text the user enters is then returned as a string.
# The text inside input() is the prompt shown to the user
user_name = input("Please enter your name: ")
print("Hello,", user_name, "!")
An important thing to remember is that
input()always returns a string. If you need to treat the input as a number, you must convert it first using functions likeint()orfloat().
age_string = input("How old are you? ")
age_number = int(age_string) # Convert the string to an integer
years_until_100 = 100 - age_number
print("You will be 100 in", years_until_100, "years.")
Now you have the fundamental building blocks. Let's see if you can put them to use.
What is the primary program responsible for understanding and running Python code?
In Python, how are blocks of code (like the body of a loop or function) defined?
These are the first steps on your programming journey. With these basics of variables, operators, and I/O, you can already write simple, useful programs.
