Class1: Different types of Data

Open In Colab

In the field of data science, we often talk about different types of data. Understanding these types is crucial for proper data analysis and interpretation. Here are some of the most common types of data:

  1. Numerical Data: This type of data is quantitative and can be divided into two types: discrete and continuous. Discrete numerical data is countable (e.g., the number of employees in a company), while continuous numerical data can take any value within a range (e.g., the weight of a person).

  2. Categorical Data: This type of data is qualitative and describes categories or groups (e.g., the color of a car). It can be further divided into nominal data (no order implied) and ordinal data (order is significant).

  3. Binary Data: A special case of categorical data where the variable has only two categories or groups (e.g., yes/no, true/false).

  4. Time-series Data: This type of data is collected over time and the order of the data points is important (e.g., stock prices over time).

  5. Text Data: This type of data is unstructured and is typically in the form of sentences or paragraphs (e.g., customer reviews).

In Python, we have several data types that are commonly used. Here are some of them with examples:

  1. Integers: These are whole numbers. For example, 5, 10, -1.

  2. Floats: These are real numbers (i.e., numbers that can have a decimal point). For example, 5.0, 1.23, -0.5.

  3. Strings: These are sequences of characters. For example, 'Hello, World!', 'Python'.

  4. Booleans: These represent truth values True and False.

  5. Lists: These are ordered collections of items (which can be of different types). For example, [1, 2, 3], ['apple', 'banana', 'cherry'].

  6. Tuples: These are like lists, but they are immutable (i.e., they can’t be modified after they are created). For example, (1, 2, 3), ('apple', 'banana', 'cherry').

  7. Dictionaries: These are collections of key-value pairs. For example, {'name': 'John', 'age': 30}.

Python has a variety of operators that can be used with different data types. Here are some examples:

  1. Arithmetic Operators: These are used with numeric values to perform common mathematical operations.
    • Addition: 5 + 3 returns 8
    • Subtraction: 5 - 3 returns 2
    • Multiplication: 5 * 3 returns 15
    • Division: 5 / 3 returns 1.6666666666666667
    • Modulus: 5 % 3 returns 2
    • Exponentiation: 5 ** 3 returns 125
    • Floor division: 5 // 3 returns 1
  2. Comparison Operators: These are used to compare two values.
    • Equal: 5 == 3 returns False
    • Not equal: 5 != 3 returns True
    • Greater than: 5 > 3 returns True
    • Less than: 5 < 3 returns False
    • Greater than or equal to: 5 >= 3 returns True
    • Less than or equal to: 5 <= 3 returns False
  3. Logical Operators: These are used to combine conditional statements.
    • and: Returns True if both statements are true
    • or: Returns True if one of the statements is true
    • not: Reverse the result, returns False if the result is true
  4. Membership Operators: These are used to test if a sequence is presented in an object.
    • in: Returns True if a sequence with the specified value is present in the object
    • not in: Returns True if a sequence with the specified value is not present in the object
  5. String Operators: Python also has a set of operators that are specifically designed to work with strings. These include the + operator for concatenation and the * operator for repetition.

Python Data Types: Exercises

# Exercise 1: Working with integers and floats
num1 = 10
num2 = 3

# Addition
print('Addition:', num1 + num2)

# Subtraction
print('Subtraction:', num1 - num2)

# Multiplication
print('Multiplication:', num1 * num2)

# Division
print('Division:', num1 / num2)

# Floor Division
print('Floor Division:', num1 // num2)

# Modulus
print('Modulus:', num1 % num2)

# Exponentiation
print('Exponentiation:', num1 ** num2)
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Floor Division: 3
Modulus: 1
Exponentiation: 1000
# Exercise 2: Working with strings
str1 = 'Hello'
str2 = 'World'

# String concatenation
print('Concatenation:', str1 + ' ' + str2)

# String repetition
print('Repetition:', str1 * 3)

# String slicing
print('Slicing:', str1[1:4])

# String length
print('Length:', len(str1))
Concatenation: Hello World
Repetition: HelloHelloHello
Slicing: ell
Length: 5
# Exercise 3: Working with lists
list1 = [1, 2, 3, 4, 5]

# Accessing elements
print('First element:', list1[0])
print('Last element:', list1[-1])

# List slicing
print('Slicing:', list1[1:4])

# List length
print('Length:', len(list1))

# Adding elements
list1.append(6)
print('After adding an element:', list1)

# Removing elements
list1.remove(1)
print('After removing an element:', list1)
First element: 1
Last element: 5
Slicing: [2, 3, 4]
Length: 5
After adding an element: [1, 2, 3, 4, 5, 6]
After removing an element: [2, 3, 4, 5, 6]
# Exercise 4: Working with dictionaries
dict1 = {'name': 'John', 'age': 30, 'city': 'New York'}

# Accessing elements
print('Name:', dict1['name'])
print('Age:', dict1['age'])

# Adding elements
dict1['job'] = 'Engineer'
print('After adding an element:', dict1)

# Removing elements
del dict1['age']
print('After removing an element:', dict1)
Name: John
Age: 30
After adding an element: {'name': 'John', 'age': 30, 'city': 'New York', 'job': 'Engineer'}
After removing an element: {'name': 'John', 'city': 'New York', 'job': 'Engineer'}

Python Functions

In Python, a function is a block of reusable code that performs a specific task. Functions help break our program into smaller and modular chunks, making it organized and manageable. They also prevent repetition and make the code reusable.

Here’s the basic syntax of a Python function:

def function_name(parameters):
    """docstring"""
    statement(s)
  1. The def keyword is a statement for defining a function in Python.
  2. function_name is a unique identifier for the function.
  3. parameters (arguments) through which we pass values to a function. These are optional.
  4. A colon (:) to mark the end of the function header.
  5. Optional documentation string (docstring) to describe what the function does.
  6. One or more valid python statements that make up the function body. Statements must have the same indentation level (usually 4 spaces).
  7. An optional return statement to return a value from the function.
# Example of a Python function

def greet(name):
    """
    This function greets the person passed in as a parameter
    """
    print(f'Hello, {name}. Good morning!')

# Calling the function
greet('John')
Hello, John. Good morning!
# Example of a Python function with a return statement

def add_numbers(num1, num2):
    """
    This function adds the two numbers passed in as parameters
    """
    return num1 + num2

# Calling the function and printing the result
result = add_numbers(5, 3)
print(f'The sum is {result}')
The sum is 8

Exercises

Exercise 1: Data Types

Given the following variables, convert each one to a different data type:

num = '123'
str_num = 456

Convert num to an integer and str_num to a string. Print the new variables and their types.

Exercise 2: Functions

Write a function named calculate_average that takes a list of numbers as a parameter and returns their average.

Then, call your function with the list [1, 2, 3, 4, 5] and print the result.

Exercise 3: Lists and Dictionaries

Given the following list of dictionaries:

data = [
    {'name': 'John', 'age': 30, 'job': 'Engineer'},
    {'name': 'Anna', 'age': 27, 'job': 'Doctor'},
    {'name': 'Mike', 'age': 35, 'job': 'Artist'}
]

Write a function named get_ages that takes the list as a parameter and returns a list of all ages. Then, call your function with the data list and print the result.