Using variables and functions
In Python, we use variables to store a value. We can then use the value to perform operations, evaluate expressions, or use them in functions. Functions give sets of instructions for the algorithm to follow when they are called in an algorithm. Many functions include variables within them. So, let’s first look at how we define and use variables, then take a look at Python functions.
Variables in Python
Python does not have a command for declaring variables. We can create variables by naming them and setting them equal to whatever value we’d like. Keep in mind that there are some rules for naming variables:
- The name must begin with a letter or underscore. (Some examples of valid names are
animaland_animal.) - The name cannot begin with a number. (The
1animalvariable is invalid, butanimal1is valid.) - The name cannot contain any symbols other than an underscore—so, only alphanumeric characters A through Z, the...