Python – Variables

Python Variable Data Types

Variables are nothing but reserved memory for storing data. Based on the data type interpreter allocates the memory. Python Variable are  similar to JavaScript Variables. Unlike Python is Loosely-Coupled which means we can declare a variable without defining it’s data type.

Python have different data types:

  1. Number : Integer, Float, Complex
  2. String
  3. List
  4. Tuple
  5. Dictionary

Assigning Values to Variables

x = 3        # Float
y = "StudyGyaan"    # String
z = 2.15 # Float print(x) print(y)

Variables do not need to be declared with any particular data type and can even change type after they have been declared and stored.

x = 5
x = "StudyGyaan"

Multiple Assignment 

In python variable values can be assign multiple time

x = y = z = 786

Rules for Initialising Variables or Identifiers in Python

A python identifiers starts with A to Z or a to z or an underscore( _ ) followed by zero or more letters, underscores and digits (0 to 9).

Allowed Identifiers: A, a, _a, _A123, _123, etc.

Not Allowed Identifiers: 123, $ads, etc.

Output Variables

Python variables are basically output using print statement. We can use + symbol to concate two strings and add two number variables in Python.

x = "awesome"        # String
print("Python is " + x)

# Output: Python is awesome

x = 5.5           # Float
y = 10            # Integer
print(x + y)

# Output: 15.5

If you try to combine string and number, it will throw error:

x = "Beginners Guide"
y = 10
print(x + y)

#Output: TypeError: unsupported operand type(s)
# for +: 'int' and 'str'
Filed under: Python

No comment yet, add your voice below!

Leave a Reply