beginnerData Types

Data Types

Explore Python built-in data types: int, float, str, bool, and more.

01Numeric Types

Python has three numeric types: int (integers), float (decimal numbers), and complex numbers.

Python
1x = 10 # int
2y = 3.14 # float
3z = 2 + 3j # complex
4
5print(type(x)) # <class 'int'>
6print(type(y)) # <class 'float'>
7print(type(z)) # <class 'complex'>
Output
<class 'int'>
<class 'float'>
<class 'complex'>

02Strings

Strings are sequences of characters enclosed in single or double quotes. They are immutable.

Python
1name = "Python"
2greeting = 'Hello'
3multiline = """This is
4a multiline
5string"""
6
7print(len(name)) # 6
8print(name.upper()) # PYTHON
9print(name[0]) # P
10print(name[-1]) # n
11print(name[0:3]) # Pyt
Output
6
PYTHON
P
n
Pyt

03Booleans

Booleans represent True or False values. They are often used in conditional statements.

Python
1is_active = True
2is_deleted = False
3
4print(bool(0)) # False
5print(bool(1)) # True
6print(bool("")) # False
7print(bool("hello")) # True
8print(bool([])) # False
Output
False
True
False
True
False

04Type Conversion

You can convert between types using built-in functions like int(), float(), str(), and bool().

Python
1x = int("10") # string to int
2y = float("3.14") # string to float
3z = str(42) # int to string
4w = bool(1) # int to bool
5
6print(x, y, z, w)
Output
10 3.14 42 True