What is Variable in Python?

Python Variables:

Variable is a container to store data.

Variable is a name of memory location where data stored in a particular address.

Creating Variables:

Python has no syntax for declaring a variable.

When we declare the variable the value or data assign on it directly.

x = 5
0
y = "John"
print(x)
print(y)

The above code the type of x means here is integer and type of y is string.

When you assign data directly any variable in python the type of variable is defined on that time.

x = 4       # x is of type int
x = "Sally" # x is now of type str
print(x)

The above code output is:

Sally 

Because x shows me recent value or data.

Python Casting:

If you want to specify the data type of a variable, then we are using inbuilt python functions.

x = str(4)    # x will be '4'
y = int(7)    # y will be 7
z = float(9)  # z will be 9.0

print(x)
print(y)
print(z)

The output of above code:

4
7
9.0

Python Return The Type Of Variable:

In python if you want to check the type of variable is it when assign some values on it, then how?

x = 5
y = "John"
print(type(x))
print(type(y))

Here in above code returns the type of variable by help of this function called type(var_name).

So, the output is:

<class 'int'>
<class 'str'>

What is Single Quote and Double Quote Means?

In python string variables are declared in single and double quotes:

x = "John"
print(x)
#double quotes are the same as single quotes:
x = 'John'
print(x)

The output of above code:

John
John

Case-Sensitive In Python:

In python the variable names are case-sensitive, means:

//Example
a = 2
A = 4

The above code plays, will not overwrite with data because both are different on python point.

But, in some case we write:

//Example
a = 56
a = "asdf"

Then here a first play the role of Integer then it play the role of String.

So, in coding the recent value will be assigned on it and take granted.

Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 228

Newsletter Updates

Enter your email address below to subscribe to our newsletter