Python Beginner Tutorials: Variables and Data Types
What Are Variables?
Variables are labels we use to temporarily store information in a computers memory.
Example
When the Python interpreter runs this code, it will allocate some memory, then store “August” in that memory and attach month as the label to that memory location.
So Variable is our label and August is our value.
Let’s command the computer to print month;
The computer has printed the variable month for us and has returned the value August.
As month is a variable we do not put it in between quotation marks (“”), the quotation marks are for strings, which we will discuss in another article.
When assigning a variable we start with the name of the variable, then (=) sign, then the value of a variable.
Year is our name, then the (=) sign and 2020 is our value.
When naming our variables we use lowercase letters, it is year and not Year or YEAR.
We also use underscore(_) to combine variable which are separate words.
Example
Student Registration becomes student_registration
We always want to be specific when we name our variable, when we design a program to handle student registration we want to name our variable; student_registration and not sr.
When your program is given to another programmer they will not understand what sr means, so be specific when naming variables.
Back to our month variable
Before August is stored in the memory it gets converted to binary, remember in our previous lesson I explained computers only understand binary which is 1s and 0s.
We can also update values of variables.
Example
Let’s print the updated value.
Notice how the value has updated from August to September.
The Python Interpreter reads code line by line starting from the top.
So from the top the value of month was August, then we updated it and the latest which is September.
Data Types
Information flowing inside of a computer is called data, we have different types of data and the computer classifies this data and stores it according to its classification.
We have whole numbers (1,76,1009,-74,-9,-109) we refer to these whole numbers as integer data types.
Example
The other data type concerning numbers are float data types.
Float data types is how the computer stores numbers with decimal points.
Example
Texts and characters are stored as string data types.
Strings are always in between quotation marks (“”) to indicate they are strings.
Example
True or False values are referred to as boolean data types.
They are very limited, the value of the variable is either True or False.
Example
The T in True and the F in False have to be uppercase.
Boolean, String, Integer and Float data types are known as simple values.
Activity
Lets write a program that registers students at the beginning of the academic year.
We have a student named Jeff, he is 19 years old and he is a returning student, let’s define three variables, for his name, age and is he a new or returning student or not.
Summary
1. Computers use variable to store information.
2. Information is classified into different types this is known as data type.
3. We have four data types for single values namely; strings, integers, boolean and floats.
4. Variable names should be lower case
5. T in True and F in False must always be uppercase values.
6. When combining two words to make a variable we use underscore(_).