Data Types
Data types are classifications that specify the kind of value/data a variable can hold.
They include:
Integer or int: Whole numbers (e.g., 1, 43, 78, 100, 34).
String or str: Text data enclosed in quotes. Depending on the programming language, these can be in single quotes ('') or double quotes (""). (e.g., "Grace", "height", "school")
Boolean or bool: Represents truth values: True or False.
Float: Decimal numbers (e.g., 2.9, 56.9, 0.0001).
Character or char: A single character (e.g., A, d).
Variables
Variables help us reference a piece of data for later use. They can hold any data type (e.g., strings, floats, integers, and Booleans).
Strings must be enclosed in single or double quotes.
Floats are numbers with a decimal point.
Examples:
# String
name = "Russell"
# Integer
age = 45
# Float
height = 170.8
# Boolean
is_customer = False
Here, name, age, and height are variables. For example, when "name" is called, it references "Russell" because "Russell" has been assigned to name.
Rules for Variables:
Variables are defined with the equals sign (=).
Variables must start with a letter.
Variables can include numbers and underscores, but these cannot come at the beginning.
Variables are case-sensitive (e.g., Name and name are two different variables).
Spaces and special characters cannot be used in variable names.
Examples of Valid Variable Names:
footballers_names
ages45
x
Food
*Examples of Invalid Variable Names:
*
footballers-names (hyphen is a special character).
42age (variables cannot start with a number).
snow bunny (spaces are not allowed).
To know the value of a variable, use the print function:
name = "Russell"
age = 45
height = 170
print(height)
# Output: 170
Important: A variable itself does not require quotes.
Top comments (0)