Data Types

🔢 Let’s start with the building blocks! In this section, you’ll explore Python’s basic Data Types. Understanding these is key to writing code that behaves the way you expect. Think of it as learning the alphabet before writing full sentences.

1 Integer (int)

Whole numbers, positive or negative.

print(10)  # 10
print(4 + 3 + 2 + 1)  # 10
print(type(10))  # <class 'int'>
10
10
<class 'int'>
print(-3)  # -3
print(-5 + 2)  # -3
print(type(-3))  # <class 'int'>
-3
-3
<class 'int'>

2 Float (float)

Decimal numbers, positive or negative.

print(3.14159)  # 3.14159
print(type(3.14159))  # <class 'float'>
3.14159
<class 'float'>
print(-3.14159 * 2)  # -6.28318
print(type(-3.14159 * 2))  # <class 'float'>
-6.28318
<class 'float'>
print(1e-01)  # 0.1
print(type(1e-01))  # <class 'float'>
0.1
<class 'float'>

3 Boolean (bool)

Represents True or False values, often used in conditionals.

print(True) # True
print(type(True))  # <class 'bool'>
True
<class 'bool'>
print(False) # False
print(type(False))  # <class 'bool'>
False
<class 'bool'>

4 String (str)

Sequences of Unicode characters.

print("Hello")  # Hello
print(type("Hello"))  # <class 'str'>
Hello
<class 'str'>
print('Hello')  # Hello
print(type('Hello'))  # <class 'str'>
Hello
<class 'str'>

5 None (None)

Represents the absence of a value.

print(None) # None
print(type(None))  # <class 'NoneType'>
None
<class 'NoneType'>

Common Errors

Some frequent errors encountered when dealing with Data Types in Python.

print(Tyler)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[11], line 1
----> 1 print(Tyler)

NameError: name 'Tyler' is not defined
print(int(Tyler))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[12], line 1
----> 1 print(int(Tyler))

NameError: name 'Tyler' is not defined
print(5 + '10')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[13], line 1
----> 1 print(5 + '10')

TypeError: unsupported operand type(s) for +: 'int' and 'str'