No history yet

Introduction to Programming

Giving Computers Instructions

At its heart, programming is about giving instructions to a computer. We write these instructions in a way the computer can understand, using a programming language. Think of it like a recipe. A recipe lists steps to follow to bake a cake; a program lists steps a computer follows to complete a task.

There are many programming languages, but we'll start with Python. Python is known for its clean, readable syntax, which makes it a great choice for beginners. It reads a bit like English, which helps take the mystery out of coding.

The goal is to break down a big task into a series of small, logical steps.

Storing Information

To perform tasks, programs need to work with information, like numbers, text, or true/false values. To keep track of this information, we use variables.

variable

noun

A storage location with a specific name, which holds a known or unknown quantity of information referred to as a value.

Imagine a variable as a labeled box where you can store something. You give the box a name (the variable name) and put something inside (the value). You can change what's inside the box later.

# In Python, we use the equals sign (=) to assign a value to a variable.

greeting = "Hello, world!"

The information we store comes in different forms, or data types. Here are the most common ones:

  • Strings: Plain text, enclosed in quotes. "Hello!"
  • Integers: Whole numbers. 42, -100
  • Floats: Numbers with a decimal point. 3.14, -0.5
  • Booleans: Represents one of two values: True or False.
# Assigning different data types to variables

user_name = "Alex"
user_age = 25
account_balance = 150.75
is_active = True

Making Decisions and Repeating

Programs aren't just a straight line of instructions. They often need to make decisions based on certain conditions. We handle this with conditional statements. The most common is the if statement, which tells the computer: "If this condition is true, then perform this action."

temperature = 15

if temperature < 20:
    print("It's a bit chilly. You might want a jacket.")
elif temperature > 30:
    print("It's hot outside! Stay hydrated.")
else:
    print("The weather is pleasant.")

Besides making decisions, programs are great at repeating tasks. Instead of writing the same line of code a hundred times, we can use a loop. A for loop repeats an action for each item in a list, while a while loop repeats an action as long as a certain condition remains true.

Loops save time and make code much cleaner and more efficient.

# A 'for' loop to greet each person

names = ["Alice", "Bob", "Charlie"]
for name in names:
    print(f"Hello, {name}!")

# A 'while' loop that counts down

count = 3
while count > 0:
    print(count)
    count = count - 1
print("Blast off!")

Packaging and Reusing Code

As programs get bigger, you'll find yourself wanting to reuse chunks of code. This is where functions come in. A function is a named, reusable block of code that performs a specific action. You can define a function once and then "call" it whenever you need it.

# Define a function to greet a user
def greet_user(name):
    print(f"Welcome, {name}!")

# Call the function with different names
greet_user("Maria")
greet_user("David")

Python also has a rich standard library full of modules, which are files containing pre-written functions and variables that you can import into your program. This saves you from having to write everything from scratch. For example, the math module gives you access to advanced mathematical functions.

# Import the 'math' module to use its functions
import math

# Calculate the square root of 81
result = math.sqrt(81)
print(result)
# Output will be: 9.0

Finally, no programmer is perfect. You will make mistakes, and sometimes your program will encounter unexpected situations. When a program crashes, it raises an error or exception. Learning to read these error messages is a key skill. Python's error messages are usually descriptive and point you to the line of code that caused the problem.

Good programming isn't just about writing code that works. It's also about writing code that is clean, readable, and easy to fix when things go wrong.

Quiz Questions 1/7

In programming, what is the primary role of a variable?

Quiz Questions 2/7

Which of the following is an example of a 'float' data type in Python?