Variables in Python

A variable in Python is a name of memory location that holds data.

In Python, we do not need to declare the types of variables. When we assign any value to the variable, that variable is declared automatically. The equal (=) operator is used to assign value to a variable.

Rules for defining variables in Python:
(i) A variable name must start with a letter or the underscore character.
(ii) A variable name cannot start with a number.
(iii) Variable name must not contain any white-space, or special character (!, @, #, %, ^, &, *).
(iv) Variable names are case-sensitive.


Declaring integer and float variables in Python:
x=1 # x is a integer type variable
y=1.2 # y is a float type variable
print(x)
print(y)

Output will be
1
1.2


Declaring string variables in Python:
String variables can be declared either by using single or double quotes.
z="Neoogy" # z is a string type variable.
print(z)

Output will be
Neoogy


Variable Casting:
We can specify the data type of a variable, which can be done with casting.
x=int(1) # x is a integer
y=float(1) # y is a float
z=str('Neoogy') # z is a string
print(x)
print(y)
print(z)

Output will be
1
1.0
Neoogy


0 Comments