No history yet

Introduction to Programming

Giving Instructions to Computers

At its core, programming is about providing clear, step-by-step instructions for a computer to follow. Since computers are literal and do exactly what they're told, these instructions must be precise. We use programming languages like Python to write these instructions in a way that is both human-readable and understandable by the machine.

The most fundamental concept is storing information. We do this using variables, which are essentially named containers for data. You can think of a variable as a labeled box where you can put a piece of information, retrieve it later, or replace it with something new.

# Storing an integer (a whole number)
age = 30

# Storing a float (a number with a decimal)
price = 19.99

# Storing a string (text)
name = "Alex"

# Storing a boolean (True or False)
is_active = True

Each piece of data has a type. In the example above, age is an integer, price is a float, name is a string, and is_active is a boolean. Python is smart enough to figure out the data type automatically when you assign a value to a variable. Understanding data types is crucial because they determine what kind of operations you can perform on the data.

For instance, you can perform mathematical calculations on integers and floats, but trying to divide two strings of text wouldn't make sense.

Making Decisions and Repeating Tasks

Programs rarely execute a simple list of instructions from top to bottom. They need to make decisions and perform repetitive tasks. This is where control structures come in. They control the flow of the program's execution.

Conditional statements, like if, elif (else if), and else, allow your program to execute different blocks of code based on whether a certain condition is true or false.

temperature = 25

if temperature > 30:
    print("It's a hot day.")
elif temperature < 10:
    print("It's a cold day.")
else:
    print("The weather is pleasant.")

# Output: The weather is pleasant.

Loops are used to repeat a block of code multiple times. A for loop is great for iterating over a sequence of items, like a list of numbers or words. A while loop continues to execute as long as its condition remains true.

# A for loop to print numbers 0 to 4
for i in range(5):
    print(i)

# A while loop that counts down from 3
count = 3
while count > 0:
    print(f"Countdown: {count}")
    count = count - 1 # or count -= 1

Packaging and Reusing Code

As programs grow, you'll find yourself writing the same sequences of instructions over and over. Functions are the solution. A function is a named, reusable block of code that performs a specific task. You can define a function once and then call it whenever you need it.

Functions can take inputs, called arguments or parameters, and can return an output value.

# Define a function to greet a person
def greet(name):
    return f"Hello, {name}!"

# Call the function with different arguments
message_for_maria = greet("Maria")
message_for_sam = greet("Sam")

print(message_for_maria)
print(message_for_sam)

Using functions makes your code more organized, easier to read, and simpler to debug. It's a core principle of writing clean and efficient programs.

Interacting with the User

Finally, most programs need to interact with the outside world. This often involves getting input from a user and displaying output. Python provides simple, built-in functions for these basic input/output (I/O) operations.

The print() function, which we've already used, displays information to the screen. The input() function pauses the program and waits for the user to type something and press Enter.

# Get the user's name
user_name = input("What is your name? ")

# Use the input in the output
print(f"Welcome to programming, {user_name}!")

It's important to remember that the input() function always returns the user's input as a string. If you need to treat it as a number, you must explicitly convert it using functions like int() or float().

Let's check your understanding of these fundamental concepts.

Quiz Questions 1/5

What is the primary role of a variable in a programming language?

Quiz Questions 2/5

Consider the following Python code:

user_age = input("Enter your age: ")
years_to_100 = 100 - user_age
print(years_to_100)

Why will this code result in an error?

These building blocks—variables, control structures, functions, and I/O—form the foundation of virtually every program you will write. Mastering them is the first step toward building complex and powerful applications.