01. Python Variables
Variables in Python.¶
In python, a variable is a name that is used to hold/store a value or an object.
A value in this case, is the information that is stored/linked to a variable.
A value can be a string of letters (string) or a number (integer.)
A variable is given a value using the equal sign/ assignment operator.
For an assignment statement
We need a variable name,assignment operator and the value.
Example:
text = 'Hello World' (i)
message = 'Welcome to the Data Armory' (ii)
score = 12 (iii)
NB:
- The type of variable will be the type of value that it refers to.
- Once Stored you can use it anywhere else.
text = "Hello World"
message = "Welcome to the Data Armory" # = sign is the assignment operator
score = 12 #12 is the value
# In line 1 `Text` is the variable name
# In line 2 `=` sign is the assignment operator
# In line 3 `12` is the value
NB:
- The type of variable will be the type of value that it refers to.
- Once Stored you can use it anywhere else.
- You can always assign different values to different variables.
#Code Example
a,b,c = 10,'come','again!'
- You can also assign one value to different variables.
#Code Example
a,b,c = 10,'come','again!'
- You can also assign one value to different variables.
#Code Example
d = e= f = 100
Variable Names Rules and Guidelines¶
We have to agree that variable names are an essential part of the source code and meaningful names are important when it comes to program comprehension.
They serve as an implied documentation that conveys to the reader;
The meaning of the code.
The intention of the writer.
Considering that sometimes names are the only documentation, then clean code approach should be highly regarded.
Therefore, it is important to devote a considerable amount of time to the issue of variable naming and therefore the variable names and rules. Source
A variable name CAN be one word, though not a Must
#Code Example
student = 'Ken'
print(student)
Ken
- You SHOULD separate words with underscores and not spaces.
#Code Example
student_name = 'Ken'
print(student_name)
Ken
#Code Example
student_name = 'George'
print(student_name)
George
- When naming variable's we SHOULD only use alpha-numeric characters (letters and numbers) and the underscore character.
#Code Example
student_1 = 'Mary'
print(student_1)
Mary
- You SHOULD NOT start a variable name with a number, but you CAN start with a letter or an underscore character.
#Code Example
student* = 'Caro
File "C:\Users\USER\AppData\Local\Temp/ipykernel_8464/1576765152.py", line 3 student* = 'Caro ^ SyntaxError: invalid syntax
- Breaking this rule will give you an invalid decimal literal or syntax error
#Code Example
1_student = 'Henry'
_student = 'John'
_student
File "C:\Users\USER\AppData\Local\Temp/ipykernel_8464/3822651473.py", line 3 1_student = 'Henry' ^ SyntaxError: invalid decimal literal
- A variable name SHOULD start with a letter or the underscore character.
- You SHOULD NOT use python keywords and function names as variable names.
pass = 'John'
File "C:\Users\USER\AppData\Local\Temp/ipykernel_8464/1289231500.py", line 1 pass = 'John' ^ SyntaxError: invalid syntax
Variable names are case sensitive
message, Message, MESSAGE and MeSsAge are different
#Code Example
message = 'Come here'
Message = 'Welcome home'
MESSAGE = 'This is the data armory'
MeSSaGe = 'Thank for watching'
print(message)
print(Message)
print(MESSAGE)
print(MeSSaGe)
Come here Welcome home This is the data armory Thank for watching
Ensure that variable names are;
Short: though they can be long.
Descriptive: That is can tell what the variable is used for.
# code Example
weather_df = "Australia.csv"
data = "Australia.csv"
Take note that some letters like lowercase l and uppercase letter O can easily be confused with number 1 and 0.
While it is legal to use upper case letters;
Others think it is a good idea to start variable names with lower case letter.
Others believe that they better use camel case than separate names with
Other avoid starting variables names with an underscore, unless when they are writing library code.