Variable and Basic data types

Comprehensive study notes, diagrams, and exam preparation for Variable and Basic data types.

Variable and Basic Data Types

Definition

A variable is a named memory location used to store data that can change during program execution.

A data type is a classification that specifies the kind of value a variable can hold and the operations that can be performed on it.

In simple terms:

  • A variable is the container or label.
  • A data type is the kind of content allowed inside that container.

Example:

age = 20
name = "Amina"
isStudent = true

Here:

  • age is a variable storing an integer
  • name is a variable storing text
  • isStudent is a variable storing a boolean value

Main Content

1. Variables

Meaning and purpose

  • A variable stores data in memory and gives it a meaningful name.
  • It allows programs to use, update, and retrieve values easily.
  • Instead of writing a value again and again, programmers assign it to a variable and use the variable name wherever needed.
  • Example:
    totalMarks = 85
    print(totalMarks)

Characteristics of variables

  • A variable usually has a name, a value, and a data type.
  • The value of a variable can change while the program runs.
  • Good variable names should be clear and meaningful, such as studentName, salary, or temperature.
  • Variable names should follow language rules, such as avoiding spaces and reserved keywords.
  • Example:
    first_name = "Sara"

Types of variable usage

  • Variables may store user input, computed results, constants, counters, or temporary values.
  • They help in making programs flexible and interactive.
  • Example:
    length = 10
    width = 5
    area = length * width
  • Here, area stores the calculated result.

Memory concept of variables

  • A variable acts like a label attached to a memory space.
  • When a value is assigned, the computer reserves memory for it.
  • The same variable can later be given a different value.
  • Example:
    x = 5
    x = 12
  • Now x contains 12, not 5.

Visual idea of a variable:

Variable Name   Stored Value
-------------   ------------
age             20
name            "John"
isPassed        true

2. Basic Data Types

Integer, Float, String, Boolean

  • These are the most common basic data types used in programming.
  • Integer: whole numbers without decimal points, such as -3, 0, 25.
  • Float: numbers with decimal points, such as 3.14, -0.5, 98.6.
  • String: a sequence of characters used for text, such as "Hello" or "ABC123".
  • Boolean: logical values representing true or false.
  • Example:
    age = 18        # integer
    price = 45.75   # float
    city = "Lagos"  # string
    isOpen = True   # boolean

Why data types matter

  • Data types determine how data is stored in memory and how the computer interprets it.
  • They affect which operations are valid. For example, addition works naturally on numbers but not on booleans in the same way.
  • They help prevent errors by ensuring correct usage of values.
  • Example:
    • 5 + 3 gives 8
    • "5" + "3" gives "53" in many languages because these are strings, not numbers

Other common basic types in some languages

  • Some programming languages also include:
    • Character: a single symbol such as 'A'
    • Double: a more precise floating-point number
    • Null/None: absence of value
  • Example:
    middle_initial = 'K'
    result = None

Type-specific behavior

  • Different data types behave differently during operations.
  • Numeric types support arithmetic.
  • Strings support concatenation and text manipulation.
  • Booleans support logical comparisons and conditions.
  • Example:
    first = "Good"
    second = "Morning"
    message = first + " " + second
  • Output:
    Good Morning

Table for basic data types:

Data Type   Example Value     Purpose
---------   -------------     -------
Integer     100               Whole numbers
Float       12.5              Decimal numbers
String      "Hello"           Text
Boolean     true              Logical yes/no

3. Declaration, Assignment, and Type Conversion

Declaration and assignment

  • Declaring a variable means creating it in a program.
  • Assigning a value means putting actual data into that variable.
  • In some languages, both happen together.
  • Example:
    score = 90
  • Here, score is created and assigned the value 90.

Reassignment

  • Variables can be updated with new values.
  • This is useful when values change during program execution.
  • Example:
    count = 1
    count = count + 1
  • Now count becomes 2.

Type conversion

  • Sometimes data must be changed from one type to another.
  • This is called type conversion or casting.
  • It is useful when input comes as text but must be treated as a number.
  • Example:
    age_text = "21"
    age_number = int(age_text)
  • Now age_number can be used in calculations.

Implicit and explicit conversion

  • Implicit conversion happens automatically in some languages.
  • Explicit conversion is done by the programmer using functions or casting methods.
  • Example of explicit conversion:
    num = float("3.5")
  • Example of implicit conversion may occur in expressions depending on language rules.

Importance of correct typing

  • Choosing the right type improves accuracy, performance, and readability.
  • Using the wrong type may cause logical errors or crashes.
  • Example:
    • Storing age as a string may make mathematical operations difficult.
    • Storing phone numbers as numbers can remove leading zeros, which may be incorrect.

Flow of using variables and types:

Input/Value -> Variable Name -> Data Type -> Operation/Processing -> Output

Example:

"25" -> ageText -> string -> convert to int -> 25 -> calculate result

Working / Process

1. Identify the information needed

  • Decide what data the program must store.
  • Determine whether the data is text, number, true/false, or another type.
  • Example: A student record may need name, age, marks, and isPresent.

2. Choose appropriate variable names and data types

  • Select meaningful names that describe the purpose of each value.
  • Match each variable with the correct data type.
  • Example:
    • name → string
    • age → integer
    • marks → float or integer
    • isPassed → boolean

3. Assign values and use them in operations

  • Store data in the variables.
  • Perform calculations, comparisons, or text operations as needed.
  • Update variables when values change.
  • Example: python marks = 78 passing_mark = 50 isPassed = marks >= passing_mark

  • Here, isPassed stores a boolean result.


Advantages / Applications

Makes programs organized and readable

  • Variables give meaningful names to values, making code easier to understand.
  • Data types help programmers know what kind of data is expected.

Supports calculations and decisions

  • Numbers can be used for arithmetic operations.
  • Booleans are useful in conditions like if statements.
  • Strings help display messages and collect user details.

Used in nearly every real-world program

  • Applications use variables to store names, passwords, prices, dates, sensor readings, and more.
  • Without variables and data types, dynamic and interactive software would not be possible.

Summary

  • Variables store values that can change.
  • Basic data types define what kind of value a variable can hold.
  • Choosing the correct type helps programs work correctly and efficiently.
  • Variables and data types are essential for storing and processing information.

Important terms to remember

  • Variable
  • Data type
  • Integer
  • Float
  • String
  • Boolean
  • Assignment
  • Type conversion