DEV Community

Cover image for Python Basics 1: Variables
Irfan Faisal
Irfan Faisal

Posted on • Updated on

Python Basics 1: Variables

What is Variable?
-> Variable is a reserved memory location to store values. Variables are integral part of python program. Think of variables as containers where you can store values.Your container must have a name, that's the variable name.

year=2024 #here we're storing 2024 value in year container
print(year) #print function to show the value of the variable
#output: 2024
Enter fullscreen mode Exit fullscreen mode

Point to note: We don't need to declare variable types directly before using them in python. The data type is decided by the interpreter during run-time.

Why variable is important?
-> Value or data storage in a variable is important because it protects and retrieves the data whenever you need it; otherwise, your data will be gone.

A variable is variable because its value can vary, not be constant.
You can re-declare the variable even after you have declared it once. The last value it has stored will be shown, multiple values can't be stored in a single value.

car=220
car="toyota"
print(car) #the value of the car variable has been updated
#output: toyota
Enter fullscreen mode Exit fullscreen mode

Variable Naming Convention

There are some rules and regulations that we must follow while naming any variable:

  1. Case sensitive
myName = 'Peter'
Print(myname)
>>> ERROR       #It needs to be same as variable's name
Enter fullscreen mode Exit fullscreen mode

2.Begin with lowercase letters (but it won't be a prob if we use uppercase!?- yes but its better to follow the rules)

3.Use underscore between multiple words in a variable(Snake-casing)
[ To use multiple words without using underscore , we can use capitalization naming convention(Camelcase)]
Ex: myCar = 'Lamborghini'

4.Never start a variable name with digits or dollar signs/ Variable names can't have whitespace and special signs
(e.g: +,-,!,@,$,#,%)

5.Have to be meaningful with the data stored in it.

6.Can't be too lengthy and too general, need to have a balance.

7.Should not be written with single alphabet.

8.Never use the characters 'l' (lowercase L), 'O'(uppercase O) or 'I' ( uppercase I) as single character variable names. In some fonts, these characters are indistinguishable from the numeral one and zero.

Can't be used as variable name
Can't be used as variable name

More facts about variables

  • Every variable in python is an object as python is object oriented.
  • Variables in python are dynamically typed. It means the datatype gets changed automatically.
text="Hello, world!"
print(type(text)) #output: <class 'str'>

text=2024
print(type(text)) #output: <class 'int'>
Enter fullscreen mode Exit fullscreen mode

Deleting a variable
We can also delete variable using the command 'del'

Greet= 'Good Morning'
del greet   # it will clear the data that we stored in variable.
print(greet)
>>>> Name Error

Enter fullscreen mode Exit fullscreen mode

I've decided to make basics of python as a series of posts which I'll try to post every week. In this series I'll go over the basics as well as summary of topics of python so that whenever you need you can just go over the series and have a clear view of each topic. I've tried to make this as concise as possible.

P.s. I'm still learning programming myself. So I may have make mistakes in my notes. If you find any mistake, don't hesitate to point that out. I'm open to any kind of feedbacks. Happy learning!

Top comments (0)