Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Variables and Data Types

What is a Variable?

In Python, a variable is a symbolic name that acts as a reference or pointer to an object. Once you assign an object to a variable, you can refer to that object by its name throughout your script.

For example, if we want to store the number of households in a barangay:

Now, num_households holds the value 120. We can use this name in calculations.

Output
120

Naming Rules

When naming variables for the Census or surveys, follow these strict rules:

  • Start with a letter or underscore: _farm_id or farm_id (Valid). 2nd_farm (Invalid).

  • Letters, numbers, underscores only: No spaces or special symbols like $, @, or -.

  • Case-sensitive: Crop_Type and crop_type are different variables.

  • Descriptive is better: Use total_production instead of t.

  • Avoid Keywords: Don’t use Python reserved words like print, list, str, int, if, or else.

Data Types

Python supports various data types.

TypeDescriptionExample
Integer (int)Whole numbersnum_carabaos = 15
Float (float)Decimal numbersland_area_ha = 2.5
String (str)Text sequencescrop_variety = "RC-216"
Boolean (bool)True or False valuesis_irrigated = True
ListOrdered collection of itemsprovinces = ["Cavite", "Bohol"]
Dictionary (dict)Key-value pairsfarm_stats = {"yield": 4.5, "type": "Rainfed"}

Note on Strings: You can use single quotes (') or double quotes ("). To include a special character like a new line, use \n.

Non-code text

Comments

Comments start with #. Python ignores them. se them to explain your thought process or clarify complex logic.

Docstrings

Docstrings are used to document functions, classes, modules, and methods. They are enclosed in triple quotes (“”") and provide detailed explanations of what the code does, its parameters, return values, and usage examples.

You can access docstrings using help(function_name) or function_name.__doc__

What does the function below show?

Output
Help on function calculate_yield in module __main__:

calculate_yield(area, avg_yield_per_ha)
    Calculate total crop yield based on area and average yield per hectare.

    Parameters:
    area (float): The area in hectares.
    avg_yield_per_ha (float): Average yield per hectare.

    Returns:
    float: Total yield.

    Example:
    >>> calculate_yield(2.5, 4.5)
    11.25

Escape Characters

Escape characters are used to insert characters that are illegal in a string such as new lines, tabs, or quotes.

You can insert a new line using \n.

Output
Kumusta
Mundo!

You can insert a tab using \t.

Output
Kumusta
	Mundo!

You can add a single quote ' by wrapping the string in double quotes " or using the \ escape character.

Output
Mundo'y magiging ikaw.
Output
Mundo'y magiging ikaw.

Scope

Variable scope

  • refers to the region of a program where a variable can be accessed or modified

  • crucial for preventing naming conflicts and managing data effectively

Global and local scope

Global scope

  • variables declared outside of any function have a global scope

  • can be accessed from any part of the program

  • uses ALLCAPS naming by convention

Local scope

  • variables declared inside a function have a local scope

  • are only accessible within that function

Output
100
Output
50

When a global and local variable share the same name

  • the local variable takes precedence

What value is printed in the code snippet below?

Output
50

Scope hierarchy

  • variables in an inner scope can access variables in an outer scope, but not vice versa

What values are printed in the code snippet below?

Output
100 50 10