Python Data Types

Data types define the kind of values a variable can hold.
Understanding data types is essential for performing correct operations and avoiding errors.

Common data types we will cover:

  • Numbers: intfloatcomplex → for calculations and measurements
  • Strings: sequence of characters → for text and messages
  • Booleans: True or False → for decision-making
  • Type casting: converting one type to another
  • Checking types: using type() function

Why data types matter:

  • Different data types support different operations (e.g., adding numbers vs. concatenating strings).
  • They help in validating inputs and ensuring data integrity.
  • Used in real-world applications like calculations, storing user info, and decision-making.

Examples of real-time usage:

  • Numbers: Calculating total sales, age, or temperature
  • Strings: Storing usernames, file paths, messages
  • Booleans: Checking if a user is logged in or if a condition is met
  • Type casting: Converting string input to integer for computations
  • Checking types: Debugging or validating data before processing

We will cover:

  1. Numbers (intfloatcomplex)
  2. Strings
  3. Booleans
  4. Type casting (conversion between types)
  5. Checking types with type()

Numbers

# ---- Numbers ----

# Integer: whole numbers without decimal point
a = 10
print("a:", a)                  # prints value of a
print("Type of a:", type(a))    # prints <class 'int'>

# Float: numbers with decimal points
b = 3.14
print("\nb:", b)                 # prints value of b
print("Type of b:", type(b))     # prints <class 'float'>

# Complex: numbers with real and imaginary part (j denotes sqrt(-1))
c = 2 + 3j
print("\nc:", c)                 # prints value of c
print("Type of c:", type(c))     # prints <class 'complex'>

a: 10
Type of a: <class ‘int’>

b: 3.14
Type of b: <class ‘float’>

c: (2+3j)
Type of c: <class ‘complex’>

Strings

# ---- Strings ----

# Strings can be written with single (' ') or double (" ") quotes
name = "W3Buddy"
greeting = 'Hello'

print("\nname:", name)
print("Type of name:", type(name))   # <class 'str'>

print("\ngreeting:", greeting)
print("Type of greeting:", type(greeting))  # <class 'str'>

# Concatenation (joining strings using +)
message = greeting + ", " + name
print("\nConcatenation:", message)

# Repetition (repeating strings using *)
repeat_msg = "Hi! " * 3
print("Repetition:", repeat_msg)

name: W3Buddy
Type of name: <class ‘str’>

greeting: Hello
Type of greeting: <class ‘str’>

Concatenation: Hello, W3Buddy
Repetition: Hi! Hi! Hi!

Booleans

# ---- Booleans ----

# Boolean values are either True or False
is_python_fun = True
is_sky_green = False

print("\nis_python_fun:", is_python_fun)
print("Type:", type(is_python_fun))   # <class 'bool'>

print("\nis_sky_green:", is_sky_green)
print("Type:", type(is_sky_green))    # <class 'bool'>

# Boolean operations (AND, OR)
print("\nTrue AND False:", True and False)   # returns False
print("True OR False:", True or False)       # returns True

is_python_fun: True
Type: <class ‘bool’>

is_sky_green: False
Type: <class ‘bool’>

True AND False: False
True OR False: True

Type Casting

# ---- Type Casting ----

# Convert int to float
x = 5
y = float(x)
print("\nint to float:", y, "| type:", type(y))

# Convert float to int (decimal part is removed, not rounded)
z = 3.9
w = int(z)
print("float to int:", w, "| type:", type(w))

# Convert string to int (string must contain only digits)
num_str = "100"
num = int(num_str)
print("string to int:", num, "| type:", type(num))

# Convert int to string
num2 = 123
num2_str = str(num2)
print("int to string:", num2_str, "| type:", type(num2_str))

int to float: 5.0 | type: <class ‘float’>
float to int: 3 | type: <class ‘int’>
string to int: 100 | type: <class ‘int’>
int to string: 123 | type: <class ‘str’>

Checking Data Types

# ---- Checking Data Types ----

# type() function shows the data type of a value
print("\nType of 10:", type(10))         # int
print("Type of 10.5:", type(10.5))       # float
print("Type of 'hello':", type("hello")) # str
print("Type of True:", type(True))       # bool

Type of 10: <class ‘int’>
Type of 10.5: <class ‘float’>
Type of ‘hello’: <class ‘str’>
Type of True: <class ‘bool’>

Summary

  • intfloatcomplex → Numeric data types
  • str → For text data
  • bool → True/False values
  • Used type casting (int()float()str())
  • Used type() to check variable data type

Next notebook: 03_operators.ipynb