Primitives, variables, and expressions are the basic elements of the Python language that form the basis for most programming operations. To program effectively in Python, it is important to understand and use these elements.
Primitives
Primitives are basic, built-in data types in Python that serve as the basis for more complex data structures. Here they are:
Integers (int)
They are integers, both positive and negative. Example:
42, -7
Floating point numbers (float)
These are real numbers with a fractional part, represented in the floating point system. Example:
3.14, -0.5
Character Strings (str)
They are strings of characters that can contain letters, numbers, special characters, spaces, and so on. Character strings are enclosed in single or double quotation marks. Example:
"Hello, World!", 'Python'
Boolean types (bool)
They represent True and False values and are used in conditions, conditional statements, and logical operations.
None
This is a special type that has only one value (‘None‘) and is used to represent empty values or missing data.
Note that Python also supports more complex data structures, such as lists, tuples, dictionaries, and collections, but these are not considered primitive because they consist of other elements that can be of different types.
Variables
Variables are the second most important element in Python. A variable is a symbolic name assigned to a value.
For example:
x = 5
means that the value 5 is assigned to a variable called x.
This information is described in the chapter “IDE – Installing VSCodium on macOS Ventura”.
Expressions
Expressions are combinations of variables, values, operators, and function calls that Python interprets to produce values.
For example:
x + 2 * 3
is an expression that Python will calculate by performing multiplication and addition to get the final value.
Expressions can be as simple as a single variable or value, or as complex as complex mathematical equations.
Example
This program, using the day, month and year of birth, sums these values and displays the result. It uses primitives (int for numbers, string for messages), variables (day, month, year, sum_of_dates) and expressions (addition operations).
# We give the day, month and year of birth
day = 12
month = 10
year = 1988
# We add up the given values
sum_of_dates = day + month + year
# We display the result
print("Sum of day, month and year of birth: ", sum_of_dates)