Variables & Data Types
Understanding how to store and manipulate data in SAPL
Introduction
Variables and data types form the foundation of any programming language. In SAPL, variables allow you to store and manipulate data, while data types define the kind of data that can be stored. This documentation provides a comprehensive overview of variables and data types in SAPL.
Variables
Variables in SAPL are containers for storing data values. Unlike some other programming languages, SAPL uses dynamic typing, which means you don't need to declare the type of a variable when you create it.
Naming Rules
When naming variables in SAPL, follow these rules:
- Variable names can contain letters (including Amharic characters), numbers, and underscores.
- Variable names must start with a letter or an underscore, not a number.
- Variable names are case-sensitive (
ስምandስምare considered different variables). - Variable names cannot be the same as SAPL keywords (like
አሳይ,እውነት, etc.).
# Valid variable names
ስም = "አበበ"
_ስም = "ከበደ"
ስም1 = "አልማዝ"
የተማሪ_ስም = "ፍቅርተ"
# Invalid variable names (these would cause errors)
# 1ስም = "ሰሎሞን" # Cannot start with a number
# አሳይ = "ሰሎሞን" # Cannot use a keyword as a variable name
# ስም-ሙሉ = "ሰሎሞን" # Cannot use hyphens in variable namesDeclaration & Assignment
In SAPL, you create a variable by assigning a value to it using the equals sign (=). The variable is created the first time a value is assigned to it.
# Variable declaration and assignment
ቁጥር = 42 # Number (integer)
ስም = "አብርሃም" # String (text)
ነጥብ = 3.14 # Number (floating-point)
እውነት_ነው = እውነት # Boolean (true)
# Displaying variable values
አሳይ(ቁጥር)
አሳይ(ስም)
አሳይ(ነጥብ)
አሳይ(እውነት_ነው)Output:
42 አብርሃም 3.14 እውነት
You can also assign multiple variables in a single line, which can be useful for swapping values or initializing related variables:
# Multiple assignment
ሀ, ለ, ሐ = 1, 2, 3
አሳይ(ሀ, ለ, ሐ)
# Swapping values
ሀ, ለ = ለ, ሀ
አሳይ(ሀ, ለ)Output:
1 2 3 2 1
Variable Scope
The scope of a variable determines where in your code the variable can be accessed. SAPL has two main types of variable scope:
- Global scope: Variables defined outside any function or block are accessible throughout the entire program.
- Local scope: Variables defined inside a function or block are only accessible within that function or block.
# Global variable
ዓለም_ቁጥር = 100
# Function with local variables
ስራ ምሳሌ():
# Local variable
ውስጥ_ቁጥር = 50
አሳይ("ውስጥ ቁጥር: " + ውስጥ_ቁጥር)
አሳይ("ዓለም ቁጥር: " + ዓለም_ቁጥር)
# Call the function
ምሳሌ()
# This would cause an error because ውስጥ_ቁጥር is not accessible here
# አሳይ(ውስጥ_ቁጥር)
# But ዓለም_ቁጥር is still accessible
አሳይ("ዓለም ቁጥር እንደገና: " + ዓለም_ቁጥር)Global vs. Local Variables
ዓለም keyword before the variable name to indicate that you're referring to the global variable, not creating a new local variable.# Global variable
ቁጥር = 10
ስራ ቁጥር_ቀይር():
# This creates a new local variable, not modifying the global one
ቁጥር = 20
አሳይ("ውስጥ ቁጥር: " + ቁጥር)
ስራ ዓለም_ቁጥር_ቀይር():
# This modifies the global variable
ዓለም ቁጥር
ቁጥር = 30
አሳይ("ውስጥ ቁጥር: " + ቁጥር)
አሳይ("መጀመሪያ: " + ቁጥር) # 10
ቁጥር_ቀይር()
አሳይ("ከመጀመሪያው ፋንክሽን በኋላ: " + ቁጥር) # Still 10
ዓለም_ቁጥር_ቀይር()
አሳይ("ከሁለተኛው ፋንክሽን በኋላ: " + ቁጥር) # Now 30Data Types
SAPL supports several built-in data types that allow you to store different kinds of values. Understanding these data types is essential for effective programming.
Numbers
SAPL supports two types of numbers: integers (whole numbers) and floating-point numbers (decimal numbers).
# Integers
ሙሉ_ቁጥር = 42
አሉታዊ = -10
ዜሮ = 0
# Floating-point numbers
ነጥብ = 3.14
ሳይንስ = 2.5e3 # Scientific notation: 2.5 × 10³ = 2500
# Arithmetic operations
ድምር = 10 + 5 # Addition: 15
ልዩነት = 10 - 5 # Subtraction: 5
ብዜት = 10 * 5 # Multiplication: 50
ክፍፍል = 10 / 5 # Division: 2.0 (always returns a float)
ሙሉ_ክፍፍል = 10 // 3 # Integer division: 3
ቀሪ = 10 % 3 # Modulus (remainder): 1
ኃይል = 2 ** 3 # Exponentiation: 8
አሳይ(ድምር, ልዩነት, ብዜት, ክፍፍል)
አሳይ(ሙሉ_ክፍፍል, ቀሪ, ኃይል)Output:
15 5 50 2.0 3 1 8
Division in SAPL
/) always returns a floating-point number, even if the result is a whole number. If you need integer division, use the floor division operator (//).Strings
Strings are sequences of characters, used to represent text. In SAPL, strings can be enclosed in either single quotes (') or double quotes (").
# String creation
ስም1 = 'አበበ'
ስም2 = "ከበደ"
ሙሉ_ስም = ስም1 + " " + ስም2 # String concatenation
# String methods
አሳይ(ሙሉ_ስም.ርዝመት()) # Length of string
አሳይ(ሙሉ_ስም.ከፍተኛ()) # Convert to uppercase
አሳይ(ሙሉ_ስም.ዝቅተኛ()) # Convert to lowercase
አሳይ(ሙሉ_ስም.ቅየር("በ", "ብ")) # Replace characters
# String indexing and slicing
ሀረግ = "ሰላም ዓለም"
አሳይ(ሀረግ[0]) # First character
አሳይ(ሀረግ[2:5]) # Characters from index 2 to 4
አሳይ(ሀረግ[:5]) # Characters from start to index 4
አሳይ(ሀረግ[6:]) # Characters from index 6 to end
አሳይ(ሀረግ[-1]) # Last character
# String formatting
እድሜ = 25
መልእክት = "ስሜ {} ነው፣ እድሜዬ {} ዓመት ነው።".ቅየር(ሙሉ_ስም, እድሜ)
አሳይ(መልእክት)Output:
7 አበበ ከበደ አበበ ከበደ አበበ ከበደ አበብ ከበደ ሰ ላም ሰላም ዓለም ም ስሜ አበበ ከበደ ነው፣ እድሜዬ 25 ዓመት ነው።
Booleans
Boolean values represent truth values with two constants: እውነት (true) and ሐሰት (false). They are commonly used in conditional statements and logical operations.
# Boolean values
ትክክል = እውነት
ስህተት = ሐሰት
# Comparison operators
ከ_በላይ = 5 > 3 # Greater than: እውነት
ከ_በታች = 5 < 3 # Less than: ሐሰት
እኩል = 5 == 5 # Equal to: እውነት
እኩል_አይደለም = 5 != 5 # Not equal to: ሐሰት
ከ_በላይ_እኩል = 5 >= 5 # Greater than or equal to: እውነት
ከ_በታች_እኩል = 5 <= 4 # Less than or equal to: ሐሰት
# Logical operators
እና_ኦፕሬተር = እውነት እና እውነት # Logical AND: እውነት
ወይም_ኦፕሬተር = እውነት ወይም ሐሰት # Logical OR: እውነት
አይደለም_ኦፕሬተር = አይደለም ሐሰት # Logical NOT: እውነት
አሳይ(ከ_በላይ, ከ_በታች, እኩል, እኩል_አይደለም)
አሳይ(እና_ኦፕሬተር, ወይም_ኦፕሬተር, አይደለም_ኦፕሬተር)Output:
እውነት ሐሰት እውነት ሐሰት እውነት እውነት እውነት
Lists
Lists are ordered collections that can store items of different data types. They are mutable, meaning their content can be changed after creation.
# Creating lists
ቁጥሮች = [1, 2, 3, 4, 5]
ስሞች = ["አበበ", "ከበደ", "አልማዝ"]
ድብልቅ = [1, "ሁለት", እውነት, 4.5]
# Accessing list elements
አሳይ(ቁጥሮች[0]) # First element: 1
አሳይ(ስሞች[1]) # Second element: ከበደ
አሳይ(ቁጥሮች[-1]) # Last element: 5
# List slicing
አሳይ(ቁጥሮች[1:4]) # Elements from index 1 to 3: [2, 3, 4]
# List methods
ቁጥሮች.ጨምር(6) # Add element to the end
አሳይ(ቁጥሮች)
ቁጥሮች.አስገባ(0, 0) # Insert element at index 0
አሳይ(ቁጥሮች)
ቁጥሮች.አስወግድ(3) # Remove element with value 3
አሳይ(ቁጥሮች)
የተወገደ = ቁጥሮች.አውጣ() # Remove and return the last element
አሳይ(የተወገደ, ቁጥሮች)
# List operations
ሁለት_ዙር = ቁጥሮች * 2 # Repeat list
አሳይ(ሁለት_ዙር)
ተዋሃደ = ቁጥሮች + ስሞች # Concatenate lists
አሳይ(ተዋሃደ)
# List comprehensions
ካሬዎች = [x * x for x in range(1, 6)]
አሳይ(ካሬዎች)Output:
1 ከበደ 5 [2, 3, 4] [1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 4, 5, 6] 6 [0, 1, 2, 4, 5] [0, 1, 2, 4, 5, 0, 1, 2, 4, 5] [0, 1, 2, 4, 5, "አበበ", "ከበደ", "አልማዝ"] [1, 4, 9, 16, 25]
Dictionaries
Dictionaries are unordered collections of key-value pairs. They are optimized for retrieving data when you know the key.
# Creating dictionaries
ተማሪ = {
"ስም": "አበበ",
"እድሜ": 20,
"ትምህርት_ቤት": "አዲስ አበባ ዩኒቨርሲቲ"
}
# Accessing dictionary values
አሳይ(ተማሪ["ስም"]) # Access by key
አሳይ(ተማሪ.አግኝ("እድሜ")) # Access using get() method
# Modifying dictionaries
ተማሪ["ክፍል"] = "ኮምፒውተር ሳይንስ" # Add new key-value pair
ተማሪ["እድሜ"] = 21 # Modify existing value
አሳይ(ተማሪ)
# Dictionary methods
ቁልፎች = ተማሪ.ቁልፎች() # Get all keys
ዋጋዎች = ተማሪ.ዋጋዎች() # Get all values
ዝርዝሮች = ተማሪ.ዝርዝሮች() # Get all key-value pairs as tuples
አሳይ(ቁልፎች)
አሳይ(ዋጋዎች)
አሳይ(ዝርዝሮች)
# Check if key exists
አሳይ("ስም" በ ተማሪ) # እውነት
አሳይ("ውጤት" በ ተማሪ) # ሐሰት
# Remove items
የተወገደ = ተማሪ.አስወግድ("እድሜ") # Remove key and return its value
አሳይ(የተወገደ, ተማሪ)Output:
አበበ 20{"ስም": "አበበ", "እድሜ": 21, "ትምህርት_ቤት": "አዲስ አበባ ዩኒቨርሲቲ", "ክፍል": "ኮምፒውተር ሳይንስ"}["ስም", "እድሜ", "ትምህርት_ቤት", "ክፍል"] ["አበበ", 21, "አዲስ አበባ ዩኒቨርሲቲ", "ኮምፒውተር ሳይንስ"] [("ስም", "አበበ"), ("እድሜ", 21), ("ትምህርት_ቤት", "አዲስ አበባ ዩኒቨርሲቲ"), ("ክፍል", "ኮምፒውተር ሳይንስ")] እውነት ሐሰት 21 {"ስም": "አበበ", "ትምህርት_ቤት": "አዲስ አበባ ዩኒቨርሲቲ", "ክፍል": "ኮምፒውተር ሳይንስ"}None Type
The ምንም type represents the absence of a value or a null value. It's often used as a default return value for functions that don't explicitly return anything.
# None type
ባዶ_ተለዋዋጭ = ምንም
አሳይ(ባዶ_ተለዋዋጭ)
# Functions that don't return anything implicitly return None
ስራ ሰላምታ():
አሳይ("ሰላም!")
ውጤት = ሰላምታ()
አሳይ(ውጤት)
# Checking for None
አሳይ(ባዶ_ተለዋዋጭ == ምንም) # እውነት
አሳይ(ውጤት is ምንም) # እውነትOutput:
ምንም ሰላም! ምንም እውነት እውነት
Type Conversion
SAPL allows you to convert values from one data type to another. This is known as type conversion or type casting.
# Converting to integer
ወደ_ቁጥር1 = ቁጥር("42") # String to integer: 42
ወደ_ቁጥር2 = ቁጥር(3.14) # Float to integer: 3 (truncates decimal part)
ወደ_ቁጥር3 = ቁጥር(እውነት) # Boolean to integer: 1 (እውነት becomes 1, ሐሰት becomes 0)
# Converting to float
ወደ_ነጥብ1 = ነጥብ("3.14") # String to float: 3.14
ወደ_ነጥብ2 = ነጥብ(42) # Integer to float: 42.0
# Converting to string
ወደ_ፊደል1 = ፊደል(42) # Integer to string: "42"
ወደ_ፊደል2 = ፊደል(3.14) # Float to string: "3.14"
ወደ_ፊደል3 = ፊደል(እውነት) # Boolean to string: "እውነት"
# Converting to boolean
ወደ_ቡሊያን1 = ቡሊያን(1) # Non-zero number to boolean: እውነት
ወደ_ቡሊያን2 = ቡሊያን(0) # Zero to boolean: ሐሰት
ወደ_ቡሊያን3 = ቡሊያን("ሰላም") # Non-empty string to boolean: እውነት
ወደ_ቡሊያን4 = ቡሊያን("") # Empty string to boolean: ሐሰት
አሳይ(ወደ_ቁጥር1, ወደ_ቁጥር2, ወደ_ቁጥር3)
አሳይ(ወደ_ነጥብ1, ወደ_ነጥብ2)
አሳይ(ወደ_ፊደል1, ወደ_ፊደል2, ወደ_ፊደል3)
አሳይ(ወደ_ቡሊያን1, ወደ_ቡሊያን2, ወደ_ቡሊያን3, ወደ_ቡሊያን4)Output:
42 3 1 3.14 42.0 42 3.14 እውነት እውነት ሐሰት እውነት ሐሰት
Type Conversion Errors
ቁጥር("አበበ")) will result in an error.Constants
SAPL doesn't have a built-in way to declare constants (variables whose values cannot be changed). However, it's a common convention to use ALL_UPPERCASE names for values that should be treated as constants.
# Constants (by convention)
ፒ_ዋጋ = 3.14159
ግራቪቲ = 9.8
ዝቅተኛ_እድሜ = 18
# Using constants
ዙር = 2 * ፒ_ዋጋ * 5
አሳይ("ዙር: " + ዙር)
እድሜ = 16
አሳይ("በቂ እድሜ አለው: " + (እድሜ >= ዝቅተኛ_እድሜ))Output:
ዙር: 31.4159 በቂ እድሜ አለው: ሐሰት
Best Practices
Use descriptive variable names
Choose names that clearly indicate what the variable represents. For example, use
የተማሪ_ስምinstead of justስ.Be consistent with naming conventions
If you start using snake_case (words_separated_by_underscores), stick with it throughout your code.
Initialize variables before using them
Always assign a value to a variable before trying to use it in operations or display it.
Keep variable types consistent
While SAPL allows changing a variable's type, it's generally clearer to maintain consistent types.
Use ALL_UPPERCASE for constants
This makes it clear which values should not be changed during program execution.
Avoid global variables when possible
Global variables can make code harder to understand and debug. Use function parameters and return values instead.
Use type conversion functions explicitly
When you need to convert between types, use the appropriate conversion function (
ቁጥር(),ፊደል(), etc.) explicitly to make your intentions clear.
Code Readability
Summary
Key Concepts
- Variables are containers for storing data values in SAPL
- SAPL is dynamically typed, meaning variable types are determined by the values assigned to them
- SAPL supports various data types including numbers, strings, booleans, lists, dictionaries, and None
- Variables have scope (global or local) that determines where they can be accessed
- Type conversion functions allow you to convert values from one data type to another
- Following best practices for variable naming and usage makes your code more readable and maintainable
