How to Correctly Use Variables When Coding in Python

Written by kamalsamaila | Published 2022/09/14
Tech Story Tags: python-programming | python-tutorials | learn-python | programming | productivity | computer-science | coding | software-development | web-monetization

TLDRA 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.via the TL;DR App


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


Written by kamalsamaila | A technology advocate with passion in writing programs related to finite element method for engineering analysis
Published by HackerNoon on 2022/09/14