What is Variable in Python?

  • Variables are containers for storing data values.
  • Python has no command for declaring a variable.
  • A variable is created as soon as we assign a value to it.

Rules of declaring variable

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _)
  • Variable names are case-sensitive (school, School and SCHOOL are three different variables)

For Example,

a = 89
b = \"myitschools\"
print(a)
print(b)
  1. Variables do not need to be declared with any particular type, and can even change type after they have been set.

For Example,

Variable a as int (integer)

a = 4      

Variable a as str (string)  

a = \" myitschool \"  

print(a)
  • If you want to specify the data type of a variable, this can be done with casting.
a = str(3)                              

b = int(3)                              

c = float(3)                             

print(a)                      # a will be \'3\'

print(b)                     # b will be 3

print(c)                     # c will be 3.0
  • You can get the data type of a variable with the type () function.
a = 5

b = \" myitschool \"

print(type(a))

print(type(b))
  • String variables can be declared either by using single or double quotes:
a = \" myitschool \"

# is the same as

a = ‘myitschool’
  • Variable names are case-sensitive.
a = 4

A = \" myitschool \"

Here a and A both are different variable

  • You can assign the same value to multiple variables in one line:
a = b = c = \" myitschool \"
print(a)
print(b)
print(c)
  • If you have a collection of values in a list, tuple etc. Python allows you extract the values into variables. This is called unpacking.
numbers = [\"one\", \"two\", \"three\"]

a, b, c = numbers

print(a)

print(b)

print(c)
  • The Python print statement is often used to output variables and to combine both text and a variable, Python uses the + character:
a = \"awesome\"

print (\"Python is \" + a)
  • The + character to add a variable to another variable:
a = \"Python is \"

b = \"awesome\"

c = a+b

print(c)
  1. The + character works as a mathematical operator for numbers:
a = 5

b = 10

print(a + b)
  1. If you try to combine a string and a number, Python will give you an error:

a = 5

b = \"John\"

print(a + b)

Python Data Types

Variables can store data of different types, and different types can do different things.

In Python, numeric data type represents the data which has numeric value. Numeric value can be integer, floating number or even complex numbers. These values are defined as int, float and complex class in Python.

Python has the following data types built-in by default, in these categories:

  1. Numeric                    : int, float, complex
  2. Text                            :str
  3. Sequence Type       : list, tuple, range
  4. Boolean                     : bool
  5. Set                              : set, frozenset
  6. Binary Types            : bytes, bytearray, memoryview