I like to envision variables as a container that will hold specific information in your program. A more formal definition is that a variable is an object that refers to a value.
Assignment Statements
The information is “put into” a variable using an assignment statement. In an assignment statement the object (in this case variable) that is receiving the information is always on the left.
Variable Names
There are some specific rules for naming variables in python. Naming conventions and rules for variables in Python are as follows:
- Use lower case letters only
- Use descriptive names instead of single letters
- Multiple word names should have an underscore (_) between words
- Must begin with an underscore or letters — cannot begin with number or other symbols
- Do not use python key words
- All uppercase like MAX_SPEED are allowed in a special circumstance where we are declaring a variable as a constant.
Illegal variable names will generate an error in your code. Legal but not proper will work but they aren’t good style and the other Python programmers will think you are a n00b if you use them.
Data Types
In Python, when we assign a value to a variable object the data type is automatically set. Note: In other programming languages we actually have to identify the data type in a variable declaration.
In this course we will primarily be working with the string (str), integer (int), float, dictionary (dict), boolean (bool), list, and tuple data types. However there are more available.