paint-brush
How to Correctly Use Variables When Coding in Pythonby@kamalsamaila
889 reads
889 reads

How to Correctly Use Variables When Coding in Python

by Kamal samailaSeptember 14th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A variable is a named location in memory that is defined to store data when writing a program. A variable name must start with a letter or the underscore character. Variable names are case-sensitive (load, Load and LOAD are three different variables) Don’t use reserve keywords in naming a variable. Change the type of the variable when assigning a value to a variable to a value in python. All programs make operations on data and most of this data will be defined and stored using variables.
featured image - How to Correctly Use Variables When Coding in Python
Kamal samaila HackerNoon profile picture

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