Applied Statistics  ·  Level 6
Manage Statistical Data
Chapter 4: Manage statistical data in Python
📚 21 Topics

Applied statistics professionals in Kenya require robust programming skills to manage, analyze, and visualize data efficiently. Python has emerged as the leading language for data science due to its simplicity, versatility, and powerful libraries. This chapter equips you with practical Python techniques essential for handling statistical data, focusing on foundational concepts and hands-on coding skills relevant to real-world data problems in the Kenyan context.

4.1 Python Basics on Python

Python is a high-level programming language widely used in statistics for its readability and extensive library support. Mastery of Python basics enables statisticians to automate data cleaning, perform calculations, and develop reliable scripts for data management tasks.

4.1.1 Running Python Literals

Python literals are fixed values assigned directly in code. Common types include integers, floats, strings, and booleans. Running Python literals is the starting point for performing calculations and manipulating data.

Worked Examples

Example 1: Print an integer, a float, and a string literal in Python.

Given: Integer \(10\), Float \(3.14\), String "Kenya"

Python code:

print(10)
print(3.14)
print("Kenya")

Answer:
10
3.14
Kenya


Example 2: Assign a boolean literal and display it.

Given: Boolean value \(True\)

Python code:

is_data_valid = True
print(is_data_valid)

Answer:
True


Example 3: Add two numeric literals and print the sum.

Given: Literals \(25\) and \(60\)

Python code:

print(25 + 60)

Step-by-step:

  • \(25 + 60 = 85\)

Answer:
85


Example 4: Concatenate two string literals representing two counties.

Given: "Nairobi" and "Mombasa"

Python code:

print("Nairobi" + " and " + "Mombasa")

Answer:
Nairobi and Mombasa


Example 5: Multiply two float literals representing rainfall in mm.

Given: \(12.5\) mm and \(3.2\) mm

Python code:

print(12.5 * 3.2)

Step-by-step:

  • \(12.5 \times 3.2 = 40.0\)

Answer:
40.0


Practice Questions

  1. Write Python code to print the integer literal 100 and the string literal "Data Analysis". (2 marks)
  2. Assign the boolean value False to a variable and print it. (2 marks)
  3. Calculate and print the sum of the literals 45 and 55. (2 marks)
  4. Concatenate the strings "Kitale" and "Eldoret" with " to " in between and print the result. (2 marks)
  5. Multiply the float literals 6.7 and 8.2 and print the result. (2 marks)

4.1.2 Python Comments

Comments are statements in Python code ignored by the interpreter. They are essential for explaining code, making it easier to understand and maintain statistical scripts.

Worked Examples

Example 1: Write a single-line comment in Python explaining code purpose.

Given: Code to add two numbers

Python code:

# This code adds two numbers
print(7 + 5)

Answer:
12


Example 2: Use a comment to label a data input section.

Given: Inputting the age of a respondent

Python code:

# Input respondent's age
age = 24
print(age)

Answer:
24


Example 3: Comment out a line of code to prevent execution.

Given: Only print the value of x

Python code:

x = 15
# print(x + 5)
print(x)

Answer:
15


Example 4: Write a multi-line comment describing a script.

Given: Script calculates average age

Python code:

"""
This script calculates
the average age of respondents
"""
ages = [20, 22, 24]
print(sum(ages) / len(ages))

Step-by-step:

  • \(20 + 22 + 24 = 66\)
  • \(66 / 3 = 22.0\)

Answer:
22.0


Example 5: Place a comment beside a line of code.

Given: Indicate units of measurement

Python code:

rainfall = 120  # mm
print(rainfall)

Answer:
120


Practice Questions

  1. Add a comment above a line of code that prints "Survey Complete". (2 marks)
  2. Write code that assigns 50 to a variable and comments "Sample size". (2 marks)
  3. Comment out the line that prints 200 and only print 100. (2 marks)
  4. Write a multi-line comment describing a script that sums sales figures. (2 marks)
  5. Place a comment indicating "years" next to an age assignment. (2 marks)

4.1.3 Data Types

Python has several basic data types used in statistics, such as integers, floats, strings, lists, and booleans. Knowing the correct data type ensures accurate computations and data handling.

Worked Examples

Example 1: Identify the data type of a value.

Given: \(x = 45\)

Python code:

x = 45
print(type(x))

Answer:


Example 2: Assign a float and display its type.

Given: \(y = 12.75\)

Python code:

y = 12.75
print(type(y))

Answer:


Example 3: Store a name and check its data type.

Given: \(name = "Mary"\)

Python code:

name = "Mary"
print(type(name))

Answer:


Example 4: Create a list of values representing survey scores and check its type.

Given: \(scores = [60, 75, 80]\)

Python code:

scores = [60, 75, 80]
print(type(scores))

Answer:


Example 5: Assign a boolean value to indicate data validity.

Given: \(is_valid = False\)

Python code:

is_valid = False
print(type(is_valid))

Answer:


Practice Questions

  1. Assign the value 300 to a variable and print its data type. (2 marks)
  2. Assign 28.9 to a variable and display its data type. (2 marks)
  3. Store "Nakuru" in a variable and print its data type. (2 marks)
  4. Create a list of three years [2018, 2019, 2020] and check its type. (2 marks)
  5. Assign True to a variable called completed and print its data type. (2 marks)

4.1.4 Variables

Variables are names that store data values. In statistics, variables hold data such as sample sizes, means, and frequencies which are manipulated during analysis.

Worked Examples

Example 1: Assign the number of respondents to a variable.

Given: 150 respondents

Python code:

respondents = 150
print(respondents)

Answer:
150


Example 2: Store the average rainfall in mm in a variable.

Given: Average rainfall is 85.2 mm

Python code:

avg_rainfall = 85.2
print(avg_rainfall)

Answer:
85.2


Example 3: Assign a string variable for county name.

Given: "Kisumu"

Python code:

county = "Kisumu"
print(county)

Answer:
Kisumu


Example 4: Update a variable value and print new value.

Given: Initial sample size \(n = 100\), then increase by 20

Python code:

n = 100
n = n + 20
print(n)

Step-by-step:

  • \(n = 100 + 20 = 120\)

Answer:
120


Example 5: Assign a list of percentages to a variable and print.

Given: [45, 30, 25]

Python code:

percentages = [45, 30, 25]
print(percentages)

Answer:
[45, 30, 25]


Practice Questions

  1. Store the value 500 in a variable called total and print it. (2 marks)
  2. Assign the float 92.5 to a variable named mean_score and print it. (2 marks)
  3. Store "Machakos" in a variable named town and display it. (2 marks)
  4. Increase a variable sample by 10 if it was initially 40, then print the new value. (2 marks)
  5. Assign [60, 70, 80] to a variable marks and print it. (2 marks)

4.1.5 Writing a Python Module

A Python module is a file containing Python code, such as functions and variables, that can be imported and reused. Modules help organize statistical scripts and functions for large data projects.

Worked Examples

Example 1: Create a module with a function to calculate mean.

Given: Filename is stats_module.py

Python code in stats_module.py:

def calculate_mean(data):
    return sum(data) / len(data)

Usage:

import stats_module
print(stats_module.calculate_mean([10, 20, 30]))

Step-by-step:

  • \(10 + 20 + 30 = 60\)
  • \(60 / 3 = 20.0\)

Answer:
20.0


Example 2: Write a module with a function to compute median.

Given: Filename is median_module.py

Python code in median_module.py:

def median(data):
    data = sorted(data)
    n = len(data)
    mid = n // 2
    if n % 2 == 0:
        return (data[mid - 1] + data[mid]) / 2
    else:
        return data[mid]

Usage:

import median_module
print(median_module.median([12, 15, 10, 20]))

Step-by-step:

  • Sorted: [10, 12, 15, 20]
  • Median = (12 + 15) / 2 = 13.5

Answer:
13.5


Example 3: Save a function for variance calculation in a module.

Given: Filename is variance_module.py

Python code in variance_module.py:

def variance(data):
    mean = sum(data) / len(data)
    return sum((x, mean) ** 2 for x in data) / len(data)

Usage:

import variance_module
print(variance_module.variance([5, 7, 9]))

Step-by-step:

  • Mean: (5 + 7 + 9) / 3 = 7.0
  • Variance: ((5-7)^2 + (7-7)^2 + (9-7)^2) / 3 = (4 + 0 + 4)/3 = 8/3 ≈ 2.67

Answer:
2.67


Example 4: Create a module with a string variable.

Given: Filename is info_module.py

Python code in info_module.py:

stat_project = "Kenya Census 2019"

Usage:

import info_module
print(info_module.stat_project)

Answer:
Kenya Census 2019


Example 5: Write a module with a function to sum values.

Given: Filename is sum_module.py

Python code in sum_module.py:

def total(data):
    return sum(data)

Usage:

import sum_module
print(sum_module.total([7, 14, 21]))

Step-by-step:

  • \(7 + 14 + 21 = 42\)

Answer:
42


Practice Questions

  1. Write a function in a module called add_module.py that returns the sum of two numbers. (2 marks)
  2. Create a module mean_module.py with a function to calculate the mean of a list. (2 marks)
  3. Write a module mode_module.py with a function to find the mode of data [2, 2, 3, 3, 3, 4]. (2 marks)
  4. Save a variable called project_name with value "Household Survey" in a module and print it. (2 marks)
  5. Create a module that contains a function to multiply two numbers. (2 marks)

4.1.6 print() Function

The print() function outputs data to the screen. It is used to display results, debug code, and communicate findings in statistical scripts.

Worked Examples

Example 1: Print a simple message.

Given: "Data collection complete"

Python code:

print("Data collection complete")

Answer:
Data collection complete


Example 2: Print the result of a calculation.

Given: \(100 + 250\)

Python code:

print(100 + 250)

Step-by-step:

  • \(100 + 250 = 350\)

Answer:
350


Example 3: Print multiple values in one statement.

Given: "Sample size:", 75

Python code:

print("Sample size:", 75)

Answer:
Sample size: 75


Example 4: Print formatted text using variables.

Given: Variable county = "Embu", respondents = 120

Python code:

county = "Embu"
respondents = 120
print("County:", county, "- Respondents:", respondents)

Answer:
County: Embu - Respondents: 120


Example 5: Print list of values.

Given: [10, 20, 30]

Python code:

values = [10, 20, 30]
print(values)

Answer:
[10, 20, 30]


Practice Questions

  1. Print "Survey underway" using the print() function. (2 marks)
  2. Print the result of 80 minus 35. (2 marks)
  3. Print "Participants:" and the value 60 in one line. (2 marks)
  4. Print "Region: Nairobi, Respondents: 200" using variables. (2 marks)
  5. Print the list [5, 15, 25]. (2 marks)

4.1.7 Named Arguments

Named arguments allow you to specify parameter names in function calls for clarity. They are useful in statistics for formatting output and customizing function behavior.

Worked Examples

Example 1: Use named argument 'sep' in print().

Given: Print "Nairobi" and "Mombasa" separated by " - "

Python code:

print("Nairobi", "Mombasa", sep=" - ")

Answer:
Nairobi - Mombasa


Example 2: Use named argument 'end' in print().

Given: Print "Data Entry" and then "Complete" on the same line.

Python code:

print("Data Entry", end=" ")
print("Complete")

Answer:
Data Entry Complete


Example 3: Format number of items with named arguments.

Given: Items = 3, item_type = "Surveys"

Python code:

print("Number of", item_type, ":", items, sep="")

Given variables:

items = 3
item_type = "Surveys"

Answer:
Number ofSurveys:3


Example 4: Print percentages with custom separator.

Given: Values 40 and 60, separator = "% | "

Python code:

print(40, 60, sep="% | ")

Answer:
40% | 60


Example 5: Use both sep and end in print().

Given: Print "Start" and "End" with sep=" --> " and end as "!"

Python code:

print("Start", "End", sep=" --> ", end="!")

Answer:
Start --> End!


Practice Questions

  1. Print "Kisii" and "Kericho" with a comma and space using sep. (2 marks)
  2. Print "Input" and "Received" on the same line with a dash in between. (2 marks)
  3. Print "Total:", 120, with no space between using sep. (2 marks)
  4. Print "Done" and "Saved" with sep=" | " and end as "." (2 marks)
  5. Print 10 and 20 separated by " + " using named arguments. (2 marks)

4.1.8 Collecting User Input

The input() function allows users to enter data at runtime. It is frequently used in statistical scripts to collect sample sizes, values, or labels interactively.

Worked Examples

Example 1: Collect a respondent's name.

Python code:

name = input("Enter respondent's name: ")
print("Name:", name)

Suppose user enters: Mary

Answer:
Name: Mary


Example 2: Enter age and display it.

Python code:

age = input("Enter age: ")
print("Age:", age)

Suppose user enters: 28

Answer:
Age: 28


Example 3: Input a number and perform a calculation.

Python code:

num = input("Enter a number: ")
num = int(num)
print(num * 5)

Suppose user enters: 7

Step-by-step:

  • \(7 \times 5 = 35\)

Answer:
35


Example 4: Input two numbers and print their sum.

Python code:

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a + b)

Suppose user enters: 10 and 15

Step-by-step:

  • \(10 + 15 = 25\)

Answer:
Sum: 25


Example 5: Input a list of marks separated by spaces and print the average.

Python code:

marks = input("Enter marks separated by space: ")
marks = [int(x) for x in marks.split()]
print(sum(marks) / len(marks))

Suppose user enters: 40 50 60

Step-by-step:

  • \(40 + 50 + 60 = 150\)
  • \(150 / 3 = 50.0\)

Answer:
50.0


Practice Questions

  1. Write code to input a county name and print it. (2 marks)
  2. Input a value for sample size and display it. (2 marks)
  3. Input a number, convert it to integer, and print its square. (2 marks)
  4. Input two numbers and print their product. (2 marks)
  5. Input three scores separated by spaces and print their mean. (2 marks)

4.1.9 Getting Help

Python provides built-in ways to access documentation and help about functions and objects. This is crucial for statisticians exploring new libraries or functions.

Worked Examples

Example 1: Use help() to get information about print().

Python code:

help(print)

Answer:
Displays documentation for the print() function.


Example 2: View help for the int type.

Python code:

help(int)

Answer:
Displays documentation for the int class.


Example 3: Get help for a list.

Python code:

help(list)

Answer:
Displays documentation for the list class.


Example 4: Access help for a user-defined function.

Given:

def add(a, b):
    """Returns the sum of two numbers."""
    return a + b

help(add)

Answer:
Displays: Returns the sum of two numbers.


Example 5: Use dir() to list attributes of an object.

Python code:

dir(str)

Answer:
Displays all methods and attributes of the string class.


Practice Questions

  1. Use help() to get documentation for the float type. (2 marks)
  2. Call help() for the input function. (2 marks)
  3. Use dir() to list attributes of a list object. (2 marks)
  4. Define a function with a docstring and use help() to display it. (2 marks)
  5. Use help() to view information about the len function. (2 marks)
The rest of this chapter
🔒

Create a free account to open more of this chapter.

Free: practical guides, quick cards, workplace scenarios and more.

Create a free account
🔒4.2 Functions and Modules

In applied statistics using Python, functions and modules are fundamental for structuring code, automating repetitive calculations, and reusing statistical routines. Mastery of these concepts enables professionals to handle large datasets, perform custom analy…

🔒4.3 Math in Python

Python provides robust mathematical capabilities that are essential for applied statistics work in Kenya. These range from basic arithmetic operations to using advanced mathematical functions and generating random numbers for simulations. Understanding how to…

🔒4.4 Python Strings

In applied statistics, handling and manipulating text data is frequent, especially when working with datasets containing names, categories, or coded values. In Python, strings are used to represent and work with text data efficiently. Understanding how to defi…

🔒4.5 Sequences, Dictionaries, and Sets

Sequences, dictionaries, and sets are fundamental data structures in Python, essential for managing statistical data efficiently. Applied statistics professionals in Kenya use these structures to organize, analyze, and manipulate datasets in projects ranging f…

🔒4.6 Flow Control

Flow control structures allow applied statistics professionals to automate data handling tasks in Python, making analysis faster and more reliable. Conditional statements, loops, and comprehensions are essential for processing frequency tables, calculating sta…

🔒4.7 File Processing

Reading files is a core task in applied statistics, as professionals often work with data stored in CSV, TXT, or Excel formats. Python provides several methods for reading files, enabling statisticians to process and analyze data efficiently. The most common f…

🔒4.8 Opening Files

Opening files in Python involves specifying the mode, which determines how the file is accessed: reading, writing, or appending. Applied statistics professionals must select the correct mode to prevent data loss and ensure integrity of statistical records. Exa…

🔒4.9 The os and os.path Modules

The os and os.path modules in Python are essential for managing files, directories, and paths programmatically. Applied statistics professionals in Kenya often work with large datasets stored in folders, requiring automated file operations such as reading data…

🔒4.10 Exception Handling

Exception handling in Python is essential for writing robust statistical scripts that can handle unexpected errors, such as missing files, wrong data formats, or permission issues. In Kenyan applied statistics work, automated data pipelines must not crash when…

🔒4.11 Wildcard except Clauses

In Python, exception handling is crucial for applied statistics work, especially when automating data processing or cleaning. The "wildcard except" clause, written as except:, catches all exceptions, regardless of their type. While this can be convenient, it c…

🔒4.12 Getting Information on Exceptions

When handling exceptions in Python, applied statistics professionals often need information about the error to debug scripts or to log issues in data processing. Python provides ways to access details about exceptions, such as the error message and error type,…

🔒4.13 The else Clause

The else clause in Python is often used with loops and exception handling to specify a block of code that runs only if no errors occur, or if a loop completes without interruption. In applied statistics, using the else clause can help ensure that data processi…

🔒4.14 The finally Clause

The finally clause in Python is used to ensure that certain code always executes after a try-except block, regardless of whether an exception occurred. In applied statistics, this is especially useful for closing files, releasing database connections, or clean…

🔒4.15 Using Exceptions for Flow Control

In applied statistics, managing errors during data processing is crucial, especially when handling large datasets in Python. Exception handling allows statisticians to control what happens when unexpected situations occur, such as invalid input or missing data…

🔒4.16 Exception Hierarchy

Understanding Python’s exception hierarchy is essential for applied statistics professionals to catch and handle errors accurately. Python organizes exceptions in a tree-like structure, allowing statisticians to handle specific or general errors during statist…

🔒4.17 Dates and Times

Handling dates is crucial in applied statistics, especially when analyzing time series data from Kenyan banks, hospitals, or county offices. Python's datetime module and pandas library are commonly used for parsing and manipulating date information. The main f…

🔒4.18 Understanding Time

Time data is essential in applied statistics, especially for analyzing transaction timestamps or event logs. In Python, the datetime and time modules provide ways to represent and manipulate time. The main formula for creating a time object is: $$ text{timeobj…

🔒4.19 The time Module

The time module in Python is essential for handling time-related tasks in statistical data management, such as measuring elapsed time, creating time stamps, and simulating delays in data collection or processing. It is especially useful for applied statistics…

🔒4.20 The date-time Module

The datetime module in Python provides powerful tools for handling dates and times, including creating, manipulating, and formatting date and time objects. This is essential for applied statistics professionals in Kenya who work with time-stamped data, perform…

🔒4.21 Running Python Scripts from the Command Line

In applied statistics, running Python scripts from the command line allows professionals to automate data processing, analysis, and reporting without opening an interactive environment. This skill is essential when handling large datasets, scheduling statistic…

🔒4.21.1 The sys Module

The sys module in Python provides access to system-specific parameters and functions. It is essential in applied statistics for handling command-line arguments, managing input/output, and integrating scripts with other software used by organizations. To use th…

Chapter Summary

This chapter introduced the fundamentals of managing statistical data using Python, starting with essential concepts such as running Python code, understanding comments, data types, variables, creating modules, and utilizing the print function. It then explored the structure and use of functions, including scope, parameters, returning values, and importing modules to organize code efficiently. The chapter covered mathematical operations, including arithmetic, modulus, assignment operators, built-in math functions, and the use of the math and random modules, along with techniques for managing randomness through seeding. Learners examined strings, quotation marks, special characters, and advanced data structures like sequences, dictionaries, sets, and the use of *args and **kwargs. The flow control section detailed conditional statements, identity operators, ternary operations, looping mechanisms, enumeration, generators, and list comprehensions for powerful data manipulation. File processing was addressed through techniques for opening files and working with the os and os.path modules. Exception handling was thoroughly discussed, including wildcard exceptions, retrieving exception information, the else and finally clauses, and leveraging exceptions for flow control, along with the exception hierarchy. The chapter concluded by working with dates and times using the time and datetime modules, and demonstrated how to run Python scripts from the command line using the sys module and command-line arguments.

Self-Assessment

🔒 PDFDownload this self-assessment, with answers

Written Assessment

  1. You are analysing rainfall data for Nairobi using Python. If the rainfall amounts in millimeters for a week are stored in a list: [14, 22, 18, 25, 16, 20, 19], calculate the total rainfall for the week using Python arithmetic operators. (2 marks)
Show Worked Solutions

Step 1: Write the Python code to sum the list:

rainfall = [14, 22, 18, 25, 16, 20, 19]
total = sum(rainfall)
print(total)

Step 2: Calculate step by step:$$ 14 + 22 = 36 $$$$ 36 + 18 = 54 $$$$ 54 + 25 = 79 $$$$ 79 + 16 = 95 $$$$ 95 + 20 = 115 $$$$ 115 + 19 = 134 $$

Final Answer:
Total rainfall = 134 mm


  1. A Python script collects user input for age and stores it as a variable. If the ages entered are 23, 28, 19, 34, and 27, calculate the average age. (2 marks)
Show Worked Solutions

Step 1: Write the formula:$$ \text{Average Age} = \frac{\text{Sum of Ages}}{\text{Number of Ages}} $$

Step 2: Substitute values:$$ \text{Sum of Ages} = 23 + 28 + 19 + 34 + 27 = 131 $$$$ \text{Number of Ages} = 5 $$

Step 3: Calculate average:$$ \text{Average Age} = \frac{131}{5} = 26.2 $$

Final Answer:
Average age = 26.2 years


🔒8 more in this section.

Chapter Examination Questions

🔒 PDFDownload these examination questions, with worked answers

SECTION A (40 Marks) - Answer ALL Questions

  1. A statistics officer at the Kenya National Bureau of Statistics (KNBS) needs to collect monthly rainfall data from county offices and process the data using Python. List the steps they should follow to open a CSV file, read its contents, and handle any errors that may occur during file opening. (4 marks)

  2. Write a Python statement that prints the mean and standard deviation of the following list of daily customer counts at a Nairobi supermarket: [120, 135, 150, 110, 145]. (4 marks)

🔒18 more in this section.
Flashcards 20 cards Study deck ▾
Question
1

↻ Tap card to reveal answer
🔒

18 more in this section.

Create a free account
Test Yourself 20 questions Start quiz ▾
0%
0 / 2
🔒

18 more in this section.

Create a free account