Zum Inhalt springen

English:Variables and Data Types

Aus MOOCsWiki Staging
Version vom 13. August 2026, 11:08 Uhr von Glanz (Diskussion | Beiträge) (aiMOOC über GPT aiMOOC Action erstellt)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Variables and Data Types



Introduction

Programs work with information. A game may store a player's score, a weather app may store a temperature, and a quiz may store whether an answer is correct. In programming, a variable gives a useful name to a value, while a data type describes what kind of value it is and what operations make sense for it.

You can think of a variable as a labeled place for information. The label helps you refer to the information later. The value can often change while a program runs, which is why the word "variable" is used.

In this aiMOOC, you will learn the ideas behind variables, data types, assignment, and type conversion. The examples use simple Python because it is readable, but the main ideas apply to many programming languages.


Learning Goals

By the end of this course, you should be able to:

  1. Variables: Explain why programs use named values and predict how a variable changes during a program.
  2. Data types: Distinguish common types such as integers, floating-point numbers, strings, and Booleans.
  3. Assignment: Read and write simple assignment statements.
  4. Type conversion: Explain why input sometimes needs to be converted before calculation.
  5. Debugging: Find simple mistakes caused by wrong values, wrong types, or unclear variable names.


Variables


Names, Values, and Assignment

A variable name helps a program refer to a value. In the Python statement below, the name score is bound to the integer value 10.

score = 10

The equals sign in this statement means assignment: store or bind the value on the right using the name on the left. It does not mean exactly the same thing as an equals sign in a mathematics equation.

A variable can later be assigned a new value:

score = 10
score = 15

After these two lines run, score refers to 15.


Updating a Variable

Programs often calculate a new value from an old one. For example:

score = 10
score = score + 1

The computer first reads the current value of score, adds 1, and then assigns the result back to score. The new value is 11. This pattern is common in counters, scores, timers, and loops.


Choosing Good Variable Names

Useful names make code easier to understand. A name such as player_score is usually clearer than x when the value really is a player's score. In Python, variable names can contain letters, digits, and underscores, but they cannot begin with a digit.

Compare these examples:

s = 12
player_score = 12

Both can work, but the second name tells you more about the purpose of the value.


Data Types

A data type groups values that are treated in a similar way. Different programming languages use different type systems, but several basic ideas appear often.

Data type Example value Typical use
Integer 42 Whole-number counts, scores, ages, quantities
Floating-point number 3.5 Measurements and values with decimal parts
String "Hello" Text such as names, messages, and labels
Boolean True Logical choices with two possible values
Character 'A' A single symbol in languages that have a separate character type

Python has integers, floating-point numbers, strings, and Booleans. Python does not use a separate character type; a single character such as "A" is a string of length one.

The diagram above shows examples of variables, literal values, and data types in a visual programming context. The exact type names can vary from language to language, but the purpose is similar: the program needs a meaningful way to interpret data.


Strings and Characters

A string is a sequence of characters used to represent text. Spaces and punctuation can be part of a string too.

For example:

first_name = "Maya"
message = "Welcome!"

Even when text contains digits, it can still be a string. The value "25" is text, while 25 is an integer. That difference matters when you calculate.


Numbers

An integer represents a whole number such as -3, 0, or 27. A floating-point number represents a number that can contain a fractional part, such as 2.5 or -0.75.

Examples:

lives = 3
temperature = 18.5

Programs can perform arithmetic with numeric values:

points = 8
bonus = 2
total = points + bonus

After this code runs, total has the value 10.


Booleans

A Boolean value has two possibilities: True or False. Booleans are useful for decisions.

game_over = False
has_key = True

A comparison can produce a Boolean value:

score = 12
passed = score >= 10

Here, passed becomes True because 12 is at least 10.


A Python View of Types

In Python, you usually do not declare a variable's type before assigning a value. The value itself has a type, and a name is bound to that value. A name can later be rebound to another value, although changing the kind of value stored under the same name can make beginner programs harder to understand.

The full Python type hierarchy is more detailed than you need at first. For this course, focus on int, float, str, and bool.


Input, Output, and Type Conversion

Programs often receive input from a user. In Python, input() returns a string.

age_text = input("How old are you? ")

If you want to perform arithmetic with the entered age, convert the text to an integer:

age_text = input("How old are you? ")
age = int(age_text)
next_year = age + 1
print(next_year)

This process is called type conversion. Other common Python conversion functions include float() for floating-point numbers and str() for strings.

A conversion can fail if the text does not match the expected type. For example, int("blue") cannot produce a valid integer.


Why Types Matter

Types help programmers understand what operations are meaningful. Adding two integers performs arithmetic, while combining strings joins text.

number_result = 2 + 3
text_result = "2" + "3"

The first result is the integer 5. The second result is the string "23". The same plus symbol is being used with different types, so the program behaves differently.

Knowing the type of a value helps you predict program behavior, choose suitable operations, and find errors.


Debugging Type and Variable Errors

When code behaves unexpectedly, ask these questions:

  1. Variable name: Is the program using the intended variable?
  2. Value: Does the variable contain the value you expect at this point?
  3. Type: Is the value text, a number, or a Boolean?
  4. Assignment: Was the value updated in the correct order?
  5. Conversion: Does user input need to be converted before calculation?

A useful debugging method is to print both a value and its type:

value = "12"
print(value)
print(type(value))

This can reveal that something that looks like a number is actually stored as text.


Worked Example: A Simple Score Program

Consider this program:

player_name = input("Player name: ")
score = 0
score = score + 5
bonus = 2
score = score + bonus
print(player_name)
print(score)

Trace it step by step. player_name stores text entered by the user. score starts as the integer 0, then changes to 5, then to 7. bonus is an integer with the value 2. The final output includes the player's name and the final score.

A trace table can help:

Step player_name score bonus
After input user text not assigned not assigned
After score = 0 user text 0 not assigned
After adding 5 user text 5 not assigned
After bonus = 2 user text 5 2
After adding bonus user text 7 2


Interactive Tasks


Quiz: Test Your Knowledge

What is the main purpose of a variable in a program? (To give a useful name to a value) (!To draw graphics automatically) (!To connect a computer to the internet) (!To translate every program into English)




Which value is an integer? (27) (!3.5) (!Hello) (!True)




Which value is a string? ("Blue") (!18) (!4.2) (!False)




Which pair shows the two Boolean values used in Python? (True and False) (!Yes and No) (!One and Zero) (!Start and Stop)




What does assignment do in a statement such as score = 10? (It binds the name score to the value 10) (!It asks whether score equals 10) (!It deletes the value 10) (!It prints score on the screen)




If score starts at 8 and then score = score + 1 runs, what is the new value? (9) (!8) (!1) (!81)




What type of value does Python input return? (String) (!Integer) (!Boolean) (!Character)




Which Python function can convert suitable text into an integer? (int) (!print) (!input) (!bool)




Why can 2 + 3 behave differently from text values that contain the digits 2 and 3? (The values have different data types) (!The keyboard changes the digits) (!Variables can store only one digit) (!Computers cannot add whole numbers)




Which variable name is clearest for storing a player's score? (player_score) (!x) (!thing) (!data)





Memory Game

Variable A named reference used to work with a value
Integer A whole-number data type
String A data type used for text
Boolean A type with true and false values
Conversion Changing a value from one data type to another
Assignment Binding or storing a value under a variable name





Drag and Drop

Match the correct terms. Topic
Stores whole-number counts Integer
Stores text such as a name String
Represents a true-or-false condition Boolean
Gives a value a reusable name Variable
Changes suitable text into another type Type conversion




Match each description on the left with the programming concept on the right.


Crossword Puzzle

Variable What named programming element can refer to a value that may change?
Integer What data type stores whole numbers?
Boolean What data type represents true or false?
String What data type is commonly used for text?
Assignment What operation gives or binds a value to a variable name?
Conversion What process changes a value from one data type to another?





LearningApps


Cloze Text

Complete the text.

A

gives a useful name to a value in a program. A value's

tells the program what kind of information it represents. Whole numbers are commonly stored as

. Text is commonly stored as a

. A true-or-false value is called a

. In Python, user input is first returned as a

. Turning suitable text into a number requires

. Clear names and careful tracing make

easier.




Open-Ended Tasks


Easy

  1. Variable Hunt: Find five pieces of information in a game, app, or website that could be stored in variables. Give each one a clear variable name and a likely data type.
  2. Human Trace Table: On paper, trace a short program in which a score starts at zero and changes three times. Record the value after every assignment.
  3. Data Type Cards: Create illustrated cards for integer, floating-point number, string, and Boolean. Put one definition and two original examples on each card.
  4. Naming Challenge: Rewrite a set of vague variable names such as a, b, and x as clear names for a school timetable, sports score, or shop program.


Standard

  1. Mini Quiz Program: Build a short text-based quiz with a name variable, a score variable, at least one Boolean condition, and clear output.
  2. Input Investigator: Write a small Python program that asks for age, height, and name. Predict the type returned by each input, test your prediction, and explain any conversions you need.
  3. Programming Interview: Interview a classmate or programmer about how they choose variable names and avoid type mistakes. Summarize three useful strategies.
  4. Debugging Poster: Design a poster or infographic that explains four common mistakes involving variables or data types and shows how to fix each one.


Advanced

  1. Type Experiment: Create a Python experiment that applies the plus operator to different combinations of integers, floats, and strings. Record which combinations work and explain the results.
  2. Small Data Logger: Design a program that stores several measurements such as daily temperatures or exercise times. Choose suitable variable names and types, then justify each choice.
  3. Cross-Language Comparison: Compare how two beginner-friendly programming languages represent at least four common data types. Present similarities and differences without claiming that all languages use identical names.
  4. Explainer Video: Produce a two- to four-minute teaching video that uses an original example to explain variables, assignment, data types, and conversion to another Grade 7–8 learner.



Learning Assessment

  1. Program Trace Assessment: Trace a short program with at least four assignments and explain how each variable's value changes, including one update that uses the variable's previous value.
  2. Type Choice Assessment: For a student-registration program, choose suitable data types for age, name, average score, and attendance status, then justify every choice.
  3. Debugging Assessment: Diagnose a program that tries to add user input directly to an integer, explain the type problem, and propose a working correction.
  4. Design Assessment: Plan a simple game-score program with meaningful variable names, initial values, updates, and one Boolean condition, then explain how the types support the program's behavior.
  5. Transfer Assessment: Compare variables in programming with variables in mathematics, identifying one useful similarity and one important difference.
  6. Explanation Assessment: Given the expressions 2 + 3 and "2" + "3", predict both results and explain how data types account for the difference.




Evidence of Learning

Strong evidence of learning includes several kinds of achievement:

Area Evidence
Knowledge You can accurately explain variables, values, assignment, common data types, and type conversion.
Skills You can trace changing values, choose useful variable names, identify value types, and debug simple type-related mistakes.
Products You can create working beginner programs, trace tables, diagrams, posters, or short explanations that use variables and types correctly.
Reasoning You can justify why a certain type fits a certain kind of information and predict how type affects an operation.
Transfer You can apply the same ideas to a new program, a different programming language, or a real-world data problem.




OERs on the Topic

For a deeper reference, explore the English Wikipedia article on data types:


You can also review these related topics inside MOOCwiki: Variable, Data type, Boolean data type, String, Integer, Assignment, and Type conversion.


Linked Learning Areas

The topic connects programming syntax with problem solving, mathematics, digital literacy, and logical reasoning. Variables help you model changing information, while data types help you decide how that information should be interpreted and processed.


aiMOOC Projects