python variables

A beginners guide to understanding programming in python

Outline

  • What is a variable

  • How to assign a variable to a value in python

  • Naming 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 of 10 using the assignment operator

Naming 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 reserve 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 a variable

load= 30

print(type(load)) #type in-built function to check the type assign to the variable

# int
load = "KN"
print(type(load))

# 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.