How to Correctly Use Variables When Coding in Python
source link: https://hackernoon.com/how-to-correctly-use-variables-when-coding-in-python
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
A technology advocate with passion in writing programs related to finite element method for engineering analysis
Credibility
Outline
- What is a variable
- How to assign a variable to a value in python
- Convention for defining python variable
- Change the type of a variable
- Conclusion
what is a variable
A variable is a named location in memory that is defined to store data when writing a program. you can think of variables as a container that holds data that can be changed later in the program.
How to assign a variable to a value in python
you can use the assignment operator = to assign a value to a variable name like in the code below
price = 10 # we use the variable name price and assign it a value 10
convention for defining python variable
- A variable name must start with a letter or the underscore character
myname = "kamal"
_my_name = "kamal"
- A variable name cannot start with a number
2bridgeType = "truss"
- Variable names are case-sensitive (load, Load and LOAD are three different variables)
load = 5
Load = 10
LOAD = 15
#non will be overwritten
- don’t use reserved keywords in naming a variable.
for=10
if=15
def= "kamal"
Check the list of the reserved keywords by typing help(“keywords”) to the Python interpreter
you can change the type of the variable
load= 30
print(type(load)) #type in-built function to check the type assign to the variable
#Output is int
load = "KN"
print(type(load))
#output is str
Conclusion
All programs make operations on data and most of this data will be defined and stored using variables, hence we must understand the concept of using variables before we learn other things in our programming journey.
Originally published at https://engineerxp.com
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK