Weekly Programming Topics
Introduction to Arrays
Week 6 -Topic 1 Introduction to Arrays · Whatisin thislesson?
Introduction to Arrays
Week 6 -Topic 1 Introduction to Arrays
Whatisin thislesson?
- List
- Introduction to List (Recap)
- Processing List
- Using List for File Handling
- List with record file
- List with number file
- Multi-Dimensional List
- Plotting List Data with the matplotlib Package 11th January 2026 Tram Tran
8-3
- Lists Figure 8-6 A list of integers
- A list is an object that contains multiple data items.
- Each item that is stored in the list is called an element.
For example:
even_numbers = [2, 4, 6, 8, 10]
- Elements are enclosed in brackets and separated by commas.
8-4
Lists Iterating Over a List with the for Loop
numbers = [99, 100, 101, 102]
for n in numbers: print n
8-5
Lists Indexing
my_list = [10, 20, 30, 40]
print my_list[index]
10 20 30 40 0 1 2 3 - 4 -3 -2 -1
my_list
8-6
Lists Slicing
days = [‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’,
‘Thursday’, ‘Friday’, ‘Saturday’]
mid_days = days[2:5] # from indexes 2 up to but not including 5
# [‘Tuesday’, ‘Wednesday’,‘Thursday’] Sunday Monday Tuesday WednesdayThursday Friday Saturday
0 1 2 3 4 5 6 - 7 -6 -5 -4 -3 -2 -1
days
8-7
Lists Finding Items in a List with in and not in Program 8-9 (in_list.py)
8-8
Lists Passing a List as an Argument to a Function Program 8-18 (total_function.py)
8-9
Lists Returning a List from a Function Program 8-19 (return_list.py)
8-10
- Using List for File Handling Working with Lists and Files … Saving a list as a File
Program 8-21 (writelines.py)
8-11
- Using List for File Handling Working with Lists and Files … Reading a list from a File
Program 8-23 (read_list.py)
7-12
- Using List for File Handling Concept:
The data that is stored in a file is frequently organized in records. A record is a complete set of data about an item, and a field is an individual piece of data within a record.
7-13
- Using List for File Handling: Processing
Records
- A file’s data is organized into records and fields
- Record – a complete set of data that describes one item
- Field – a single piece of data within a record Figure 7-19 Records in a file
7-14
- Using List for File Handling: Processing
Records
7-15
- Using List for File Handling: Processing
Records class Department:
IT, HR, SALES, FINANCE = range(1, 5) Department_names = ['IT', 'HR', 'Sales', 'Finance']
class Employee: def __init__(self, name, emp_id, department):
self.name = name self.emp_id = emp_id self.department = department
def __str__(self): return f"Name: {self.name}\nID: {self.emp_id}\nDepartment: {self.department}"
7-16
Save List as file from employee import Employee # Write employees to file def write_employees(file, emp_list): file.write(str(len(emp_list)) + "\n") # number of employees while index < len(emp_list): file.write(emp_list[index].name + "\n") file.write(emp_list[index].emp_id + "\n") file.write(str(emp_list[index].department) + "\n") def main(): # Create employees manually (sample data)
employees = []
count = int(input("How many employees? "))while index < count: print("\nEmployee", index + 1)
name = input("Name: ")
emp_id = input("ID: ")
department = int(input("Department
(1-4): "))
emp = Employee(name, emp_id,department) employees.append(emp) # Open file for writing
a_file = open("input.txt", "w")write_employees(a_file, employees) a_file.close() print("\nEmployees saved successfully!")
if __name__ == "__main__":
main()
7-17
- Using List for File Handling: Processing
Records from employee import Employee, Department_names # Reads and returns a single employee from the file def get_employee(file):
name = file.readline().strip() emp_id = file.readline().strip() department = int(file.readline().strip())
return Employee(name, emp_id, department) # Reads and returns a list of employees from the file def get_employees(file):
emp_list = [] count = int(file.readline()) # first line =
number of employees while index < count:
emp = get_employee(file)
emp_list.append(emp) return emp_list # Prints employees def print_employees(emp_list):
total = len(emp_list)
while index < total: print("Name:", emp_list[index].name) print("ID:", emp_list[index].emp_id) print("Department:", Department_names[emp_list[index].department – 1]) print() def main():
a_file = open("input.txt", "r") # open forreading
employees = get_employees(a_file)
print_employees(employees) a_file.close()
if __name__ == "__main__":
main()
Multi-Dimensional List
- Two-Dimensional Lists
- CONCEPT: A two- dimensional list is a list that has other lists as its elements.
- Why Use Two- Dimensional Lists?
- Two-dimensional lists are useful for working with multiple sets of data. 1-19
Two-Dimensional Lists
1-20
- Plotting List Data with the matplotlib Package
- matplotlib package is a specialized library used for generating two-dimensional charts and graphs.
- Installation: install it separately after Python is already on your system. 1-21
Plotting List Data with the matplotlib Package
- matplotlib package is a specialized library used for generating two-dimensional charts and graphs.
- Installation: install it separately after Python is already on your system. 1-22 VS Code: 1.Ctrl + Shift + P 2.Python: Select Interpreter 3.Python 3.11
Plotting List Data with the matplotlib Package
1-23
Plotting List Data with the matplotlib Package
1-24
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
QUESTIONS
?
Recap
- List
- Introduction to List (Recap)
- Processing List
- Using List for File Handling
- List with record file
- List with number file
- Multi-Dimensional List
- Plotting List Data with the matplotlib Package 1-26
1-27