0% found this document useful (0 votes)
6 views9 pages

Week 7 - Revision - 2 1

Uploaded by

Siya Mt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Week 7 - Revision - 2 1

Uploaded by

Siya Mt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Programing

ITAPA2-12

Eduvos (Pty) Ltd (formerly Pearson Institute of Higher Education) is registered with the Department of Higher Education and Training as a private higher education institution under the
Higher Education Act, 101, of 1997. Registration Certificate number: 2001/HE07/008
Week 7: Lesson 14 Revision
What will be covered in today’s lesson?

1. List Comprehension
2. File handling
Week 7: Lesson

List Comprehension
Why Use List Comprehensions?
Concise Syntax:
• List comprehensions express complex list creation logic in a single
line of code, improving readability compared to for loops.

Enhanced Readability:
• The code clearly shows the transformation applied to each
element, making the logic easier to understand.

Efficiency:
• In many cases, list comprehensions can be as efficient (or even
more efficient) than for loops.
Basic List Comprehension Syntax
Syntax: [output_expression for item in input_sequence [if condition]]
• Output Expression: Defines what value each element in the resulting list will hold.
• Input Sequence: The iterable from which elements are taken for processing (e.g., a list,
range, string).
• Optional Condition: A condition that filters elements from the input sequence before
applying the output expression.
Using List Comprehensions with
Filtering
The if clause allows you to filter elements based on a condition.
• Syntax: [output_expression for item in input_sequence if condition]
• Example:
File handling
Core File Operations in Python
Opening Files:
• The open() function establishes a connection between your Python program and a file on
the storage device. It takes the file path and a mode (read, write, append) as arguments.

Reading Files:
• Once a file is opened in read mode, you can use methods like read() (reads entire file),
readline() (reads a single line), or readlines() (reads all lines as a list) to access the file's
content.

Writing Files:
• Opening a file in write mode allows you to write new data to the file using the write()
method. Existing data will be overwritten unless you use append mode.

Closing Files:
• It's crucial to close files using the close() method to release system resources and ensure
data integrity. Always close files after you're finished working with them.
Using context manager
Using Context Manager (with statement): A recommended approach
for file handling. The with statement automatically handles opening and
closing the file, ensuring proper resource management even if
exceptions occur.

You might also like