Variables

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Variables

A Variable is a storage location paired with an associated symbolic name.It contains some known or unknown quantity of information referred to as a value. It is basically a container for a particular
type of data.

In [1]:
# This is an Example of a Variable

Sum = "Addition";

Types of Values
Intiger
A number that doesn't contain decimal values

In [2]:
#This is an example of a variable containing an intiger

t = 4;

print(t)

print(type(t));

<class 'int'>

Float
In [ ]:
A number that which contains decimal values.

In [3]:
#This is an example of a variable containing float

y = 12.5;

print(y)

print(type(y))

12.5

<class 'float'>

String
A value containing letters and or characters

In [4]:
#This is an example of a variable containing string

p = "Hello World";

print(p)

print(type(p))

Hello World

<class 'str'>

Bool
A Bool is a True or False Value

In [5]:
#This is an example of a variable containing bool

e = True;

print(e)

print(type(e))

True

<class 'bool'>

Naming Rules
A variable must start with a letter or an underscore.
Only Letters, numbers and underscores can be used.

In [6]:
#Examples of valid variables

t7_34 = "This is Valid";

_3_29 = "This is valid";

#Examples of invalid variables

5_t = "This is Invalid";

4%t = "This is Invalid";

File "<ipython-input-6-84f6ba81cf3e>", line 6

5_t = "This is Invalid";

SyntaxError: invalid decimal literal

Assigning different values to different variables


In [8]:
x,y,z = "Hello World" , False, 2021;

print(x)

print(type(x));

print(y)

print(type(y));

print(z)

print(type(z));

Hello World

<class 'str'>

False
<class 'bool'>

2021

<class 'int'>

Assigning same value to different variables


In [9]:
p=q=r = "Hello World";

print(p)

print(type(p));

print(q)

print(type(q));

print(r)

print(type(r));

Hello World

<class 'str'>

Hello World

<class 'str'>

Hello World

<class 'str'>

Assigning different values to a variable


In [10]:
c = "Hello World";

print(c)

print(type(c));

c = 1994;

print(c)

print(type(c));

Hello World

<class 'str'>

1994

<class 'int'>

Deleting a Variable
Deleting Variables can immensely increase the processing speed.

In [11]:
b = "Hello World";

print(b);

print(type(b));

del (b);

print(b)

print(type(b))

Hello World

<class 'str'>

---------------------------------------------------------------------------

NameError Traceback (most recent call last)

<ipython-input-11-5b687506b312> in <module>

6 del (b);

----> 8 print(b)

9 print(type(b))

NameError: name 'b' is not defined

You might also like