By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
By the end of this topic, students will be able to: - Define and explain the purpose of functions in Python programming. - Create and use functions to modularise code. - Explain the concept of data structures and their importance in programming. - Implement and use lists, dictionaries, and sets to store and manipulate data. - Read and write data to files using Python. - Use functions to process and manipulate data from files.
Functions are reusable blocks of code that perform a specific task. They are used to modularise code, making it easier to understand, maintain, and reuse. Functions can take arguments, which are values passed to the function when it is called, and return values, which are the results of the function's execution.
Data structures are collections of data that are organised in a way that allows for efficient storage and retrieval. There are several types of data structures, including:
Files are used to store and retrieve data. Python provides several functions for reading and writing data to files, including open(), read(), write(), and close().
open()
read()
write()
close()
Suppose we want to write a function that calculates the area of a rectangle. We can define a function called rectangle_area() that takes two arguments, length and width, and returns the area.
rectangle_area()
length
width
def rectangle_area(length, width): return length * width print(rectangle_area(5, 3)) # Output: 15
Suppose we want to store a list of student names and their corresponding ages. We can use a list to store this data and then use a loop to print out the names and ages.
students = [ {"name": "John", "age": 15}, {"name": "Alice", "age": 16}, {"name": "Bob", "age": 17} ] for student in students: print(f"{student['name']} is {student['age']} years old")
Suppose we want to write a program that reads a list of numbers from a file and then writes the sum of those numbers to another file. We can use the open() function to open the files and the read() and write() functions to read and write data.
numbers = [1, 2, 3, 4, 5] with open("numbers.txt", "w") as f: for number in numbers: f.write(str(number) + "\n") with open("numbers.txt", "r") as f: total = 0 for line in f: total += int(line.strip()) with open("total.txt", "w") as f: f.write(str(total))
What is the purpose of a function in Python programming?
A) To perform a complex calculation B) To modularise code C) To store data D) To read and write files
Correct answer: B) To modularise code Why the distractors fail: A) is too narrow, C) is a data structure, and D) is a file operation.
What is the difference between a list and a set in Python?
A) A list is ordered, while a set is unordered B) A list is unordered, while a set is ordered C) A list is used to store strings, while a set is used to store integers D) A list is used to store integers, while a set is used to store strings
Correct answer: A) A list is ordered, while a set is unordered Why the distractors fail: B) is the opposite of the correct answer, C) and D) are incorrect about the types of data stored.
What is the purpose of the open() function in Python?
A) To read data from a file B) To write data to a file C) To open a file for reading and writing D) To close a file
Correct answer: C) To open a file for reading and writing Why the distractors fail: A) and B) are too narrow, D) is the opposite of the correct answer.
What is the difference between a dictionary and a list in Python?
A) A dictionary is ordered, while a list is unordered B) A dictionary is unordered, while a list is ordered C) A dictionary is used to store strings, while a list is used to store integers D) A dictionary is used to store integers, while a list is used to store strings
Correct answer: B) A dictionary is unordered, while a list is ordered Why the distractors fail: A) is the opposite of the correct answer, C) and D) are incorrect about the types of data stored.
What is the purpose of the with statement in Python?
with
A) To open a file for reading and writing B) To close a file C) To read data from a file D) To write data to a file
Correct answer: A) To open a file for reading and writing Why the distractors fail: B) is the opposite of the correct answer, C) and D) are too narrow.
Explain the purpose of functions in Python programming. Provide an example of a function that calculates the area of a rectangle.
Describe the difference between a list and a set in Python. Provide an example of how to use a list and a set to store and manipulate data.
Explain the purpose of the open() function in Python. Provide an example of how to use the open() function to read and write data to a file.
Describe the difference between a dictionary and a list in Python. Provide an example of how to use a dictionary and a list to store and manipulate data.
Explain the purpose of the with statement in Python. Provide an example of how to use the with statement to open and close a file.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.