Introduction to Variables
UNDERSTANDING THE BASICS OF VARIABLES IN
PROGRAMMING
WHAT IS A VARIABLE?
•Definition: A variable is a symbolic name associated
with a value and whose associated value may be changed.
•Purpose: Used to store data that can be manipulated
in a program.
WHY USE VARIABLES?
• Flexibility: Allows for dynamic data storage.
• Readability: Makes code easier to understand and
maintain.
• Reusability: Can be reused in different parts of a
program.
DECLARING VARIABLES
Syntax:
•Different programming languages have different syntax.
•Example in Python: variable_name = value
•Example in Java: int variableName = value;
TYPES OF VARIABLES
• Local Variables: Defined within a function or block.
• Global Variables: Defined outside functions, accessible
anywhere.
• Static Variables: Retain their value between function calls.
NAMING CONVENTIONS
• Rules for Naming:
• Must begin with a letter or underscore.
• Can contain letters, numbers, and underscores.
• Case-sensitive (e.g., var and Var are different).
• Best Practices:
• Use descriptive names.
• Avoid using reserved keywords.
DATA TYPES OF VARIABLES
• Integer: Whole numbers (e.g., 5, -3).
• Float: Decimal numbers (e.g., 3.14, -0.001).
• String: Sequence of characters (e.g., "Hello,
World!").
• Boolean: Represents true/false values.
SCOPE OF VARIABLES
The region in the code where a variable is accessible.
• Example of Local vs. Global Scope:
global_var = "I'm global!"
def my_function():
local_var = "I'm local!“
print(global_var)
REAL-WORLD EXAMPLES:
• User Input: Variables can store user
data.
• Game Development: Store player
scores, levels, and settings.
• Data Analysis: Store and manipulate
datasets.
Thank you