Python Variable and Data Types

Variables are used to store data at reserved memory that can be used in our program. When we create any variable than we reserve a memory space for our data.

We can store integers, characters, strings, floats, etc data types as varibale.

Variable can not be a reserved Keyword. For example print is keyword, so we can’t be named our variable as print.

Assigning a variable

id    = 100      # An integer
item  = "Pen"    # A String
price = 10.5     # A floating point

print (id)
print (item)
print (price)

In above example – id, item and price are variable names and 100, Pen and 10.5 are values or data that can be stored in the variable. Output of above code:-

100
Pen
10.5

Multiple Assignment of Variable

We can assign a single value to multiple variables in Python.

x = y = z = 10

Here , three variables have a single value means assigned same memory location.

We can also assign multiple variables to multiple values in a single line using only one equal (=) operator.

id, item, price  = 100, "Pen", 10.5

print(id, item, price)

Output for above code:

100 Pen 10.5

Some Best Practice to assign Variables:

  • Snake Case: All characters in lowercase and underscore instead of space. For eg. snake_case, form_id, etc.
  • Start with lowercase or underscore. For eg. _user_id.
  • Letters, numbers, and underscores. For eg. user_id_1.
  • Case Sensitive. If we assign variables in capital letters we can’t access it through small letters.

Data Types

  • Numeric Types: int, float, complex
  • Text Type: str
  • Boolean Type: bool
  • Sequence Types: list, tuple, range
  • Set Types: set, frozenset
  • Mapping Type: dict
  • Binary Types: bytes, bytearray, memoryview
  • Special Data Type: none

Numeric Types

**type() is used to find the data type of variable/value.

Int:

id = 100

print(id)
print(type(id))

Output:

100
<class 'int'>

Float:

price = 56.20

print(price)
print(type(price))

Output:

56.2
<class 'float'>

Complex:

z = 5j

print(z)
print(type(z))

Output:

5j
<class 'complex'>

Text Type

String (str)

item_name = "Pen"

print(item_name)
print(type(item_name))

Output:

Pen
<class 'str'>

Boolean Type

a = True

print(a)
print(type(a))

Output:

True
<class 'bool'>

Sequence Types

List

a = ["apple", 56, "alex"]
print(a)
print(type(a))

Output:

['apple', 56, 'alex']
<class 'list'>

Tuple

a = ("apple", 56, "alex")

print(a)
print(type(a))

Output:

('apple', 56, 'alex')
<class 'tuple'>

Range

a = range(5)

print(a)
print(type(a))

Output:

range(0, 5)
<class 'range'>

Set Types

Set

a = {"apple", 56, "alex"}

print(a)
print(type(a))

Output:

{56, 'alex', 'apple'}
<class 'set'>

Frozenset

a = frozenset({"apple", 56, "alex"})

print(a)
print(type(a))

Output:

frozenset({'apple', 56, 'alex'})
<class 'frozenset'>

Mapping Type

Dict (Dictionary)

a = {"name" : "Alex", "id" : 100}

print(a)
print(type(a))

Output:

{'name': 'Alex', 'id': 100}
<class 'dict'>

Binary Types

Bytes

a = b"Hello"

print(a)
print(type(a))

Output:

b'Hello'
<class 'bytes'>

ByteArray

a = bytearray(3)

print(a)
print(type(a))

Output:

bytearray(b'\x00\x00\x00')
<class 'bytearray'>

Scroll to Top