How to use Variable in Python

Posted on
How to use Variable in Python

Python Tutorial

Are you new to Python? maybe you need the Variable in Python tutorial.

A variable is a containers to stores data values.

For example:

a = 1

that means, the character “a” has/stores/holds the value “1”.

Note:
If you look at the variables above, they don’t end with a semicolon “;” because python language doesn’t need semicolon to end line.

However, a semicolon is required in this programming language if you want to separate program code on one line, like this:

print("Hi! "); print("I'm "); print("Python")

So the result is like this:

Hi!
I'am
Python

Rules for writing variables

There are several rules for writing variables in Python:

  • Start with a letter or an underscore.
  • Cannot start with a number.
  • Consist of alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Case sensitive.
  • Cannot use keywords that already exist in python such as if, while, for, etc.

Variables in Python don’t need data type

To create variables in Python, you don’t need a data type like other programming languages, as a comparison in the following table:

PythonC++Note
x = 1int x = 1;x is of type int
x = “John”string x = “John”x is of type string

Let’s make Variabel

We can create variables in various ways, with one-by-one initiation, multiple initiation, or initiation of the same value.

One by One Initialization

a = 1
b = 2
c = 3

Multiple Initialization

d, e, f = 4, 5, 6

Equal Value Initialization

g = h = i = 7

How to show result of variabe?

To see the result of the variable initiation above, we can use the print() function. The print() function is used to display whatever we put in the function.

print('a:', a)
print('b:', b)
print('c:', c)
print('d:', d)
print('e:', e)
print('f:', f)
print('g:', g)
print('h:', h)
print('i:', i)

How to Delete a Variabel?

To delete a variable we can use the del() function, seperti:

del(i)

I hope this Variable in Python article is useful for you.