Variables in Python
Subject: Python Programming
In programming the concept of variables must have been gotten from mathematics which often introduce a name that represent a particular value or expression for easy access and manipulation. In programming a variable is a name you define to hold or store a value or expression when programming for easy access and manipulation.
Examples
a=20
score=5.6
student_name="Chukwu Olamide Abu"
ready=True
In Python when you create a variable name e.g score and assign a value to it that is when it is fully defined or create. And the value you assigned or gave to it determines it's type or "data type".
Hence the variable:
a
is an
integer variable because a whole number e.g. 20 was assigned to it.
score
is a
float variable because a number with decimal point e.g. 5.6 was stored to it.
student_name
is a
string variable because it is holding a text data e.g "Chukwu Olamide Abu" note string data is wrapped around a double or single quotes e.g. 'Lagos State', "Emeka"
ready
is a
Boolean variable. A variable that can store either True or False.
You will see more examples later on how to use this variables in later lessons. Before then it is important we understand:
Rules for naming and using variables
1. Your variables name should start with lower case letter. as Upper case letter are often use for special cases. And they should not start with a number but it may contain number e.g student1 is a valid name
2. Do not use a reserved or Python keywords to name a variable e.g print, sum, if are keywords that the Python Interpreter reserved to special purpose e.g print is use to display a information to the terminal or screen.
3. no space is allowed in variable name, instead use an underscore e.g. student_name (is a valid name), student name (is not a valid name).
4. Use a better name rule as much as possible. For example is you are to store student score, instead of doing this:
num=60
it would have been better to use:
student_score=60
This is more of a personal preference, but it is better to impide a good programming habit, because other developers may have to maintain your program. This is related to the next lesson using
Commenting in PythonBy:
Benjamin Onuorah
Comments
Joshua Fasinu (learner)
Date commented: 08-Aug-2024 07:51:38am
Joshua Fasinu (learner)
Date commented: 13-Aug-2024 08:03:42am
Variable is used in holding values and input of users
Login to comment or ask question on this topic
Previous Topic Next Topic