Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 55 ยท Logic and Programs

Programming Concepts: Input, Decisions, Loops, Functions and Classes

Learn the ideas shared by every programming language: variables, input and output, conditionals, loops, functions and classes, with short runnable Python examples for beginners.

Programming languages look different, but they share a small set of ideas. If you understand variables, input and output, decisions, loops, functions and classes, you can read the basic shape of programs in almost any language: Python, JavaScript, Java, C, Go. This lesson introduces each idea in plain language and shows a tiny runnable example in Python, a language chosen for its readable syntax.

You do not need to memorize the syntax. The goal is to see that every program is built from the same few building blocks. It follows Algorithms and Problem Solving (the plan) and Programming Languages and Translation (how programs run). A full course is in the Python tutorial.

Variables and data types

A variable is a named place that holds a value. You choose the name, and the program can read or change the value later.

Text
age = 14           # a whole number (integer)
price = 4.99       # a number with a fraction (float)
name = "Sam"       # text (string)
is_student = True  # true or false (boolean)

The data type tells the computer what kind of value it is and what you can do with it: you can add numbers, join strings, and ask a boolean whether it is true. Different types behave differently: 2 + 3 is 5 but "2" + "3" is "23". See Bits and Bytes for how those values are stored.

Python Variables and types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Input and output

Output is anything the program shows: text on the screen, a sound, a file. Input is anything it receives: keys you type, a click, a file, a sensor. Almost every program reads input, processes it and prints output, just like the cycle in Where Computers Are Used.

In Python, input() asks the user for text and print() shows output. Input arrives as text, so to calculate with it you convert it to a number with int() or float().

Python Ask for input and print a result

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two lines in the Input box are the answers the program reads. Change them and run again.

Conditionals: making decisions

A conditional runs some code only if a condition is true. The condition is a boolean expression: a comparison such as age >= 18, possibly combined with and, or and not. This is the logic you saw in Boolean Logic and Gates.

Text
if condition:
    do this
elif another condition:
    do that
else:
    do something else
Python if, elif and else

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that Python marks the code that belongs to an if with indentation. Other languages use braces { } instead; the idea is the same.

Loops: repeating work

A loop repeats a block of code. A for loop goes through a collection or a range of numbers; a while loop repeats as long as a condition is true. Loops are why a computer can do a million steps from a few lines of code.

Python for and while loops

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Be careful with while: if the condition never becomes false, the loop never ends (an infinite loop). Make sure something inside the loop changes the condition.

Collections

Real programs handle many values at once. A list (or array) holds an ordered sequence; a dictionary (map) looks up a value by a key. Loops and collections go together.

Python Lists and dictionaries

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Functions: reusable named steps

A function is a named block of code you can run whenever you need it. It can take parameters (inputs) and return a result. Functions avoid repeating yourself, let you test small pieces, and give names to ideas, so a program reads like an outline: read_scores(), calculate_average(), print_report().

Python Writing and calling functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Variables created inside a function exist only inside it (their scope), which keeps different parts of a program from interfering with each other.

Classes and objects

Sometimes data and the actions that go with it belong together. A class is a blueprint that bundles data (attributes) and actions (methods). An object is one thing built from the blueprint. This approach is called object-oriented programming (OOP).

Python A class and two objects

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each object keeps its own data, but both share the same methods. Classes also support inheritance (a new class that extends an old one) and are used to model things in games, shops and user interfaces.

Comments, errors and debugging

  • Comments (# ... in Python) explain code for humans and are ignored by the computer.
  • Syntax errors break the rules of the language; the program will not start. The message points to the line.
  • Runtime errors happen while the program runs, for example dividing by zero or converting "abc" to a number.
  • Logic errors run fine but give the wrong answer; they are the hardest to find. Debugging means finding and fixing them, by reading the error message, printing values, and testing small pieces.
Python Catching an error instead of crashing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Putting it together

Almost every program is a combination of these blocks. The tiny example below keeps asking, uses a loop, a conditional, a function and a collection.

Python All the building blocks in one program

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is a variable?

    A variable gives a name to a value so that the program can read or change it later.
  2. 2. Which structure repeats code while a condition is true?

    A while loop repeats its block as long as the condition stays true; if runs code at most once.
  3. 3. Why does 'input()' usually need int() or float()?

    What a user types is text; converting it to a number allows arithmetic.
  4. 4. What is the main benefit of functions?

    Functions package a step with a name, so you can reuse, test and understand it.
  5. 5. What is the relationship between a class and an object?

    A class defines the data and actions; each object is an individual instance with its own data.

Summary

  • Programs are built from a few shared ideas: variables and types, input and output, conditionals, loops, collections, functions and classes.
  • Input arrives as text and must be converted before you do arithmetic.
  • Conditionals choose between paths using boolean expressions; loops repeat work and need a condition that eventually ends.
  • Lists and dictionaries hold many values; functions name reusable steps with parameters and return values.
  • Classes bundle data and methods; objects are individual instances.
  • Errors come in three kinds (syntax, runtime, logic); debugging is reading messages, printing values and testing small pieces.

Next: What an Operating System Does. To go deeper into code, try the Python tutorial.