0% found this document useful (0 votes)
4 views11 pages

Tuiton Loopsfhandling

The document provides an overview of loops and file handling in Python, detailing the use of for and while loops, control statements, and file operations. It explains how to open, read, write, and append to files, emphasizing the use of the 'with' statement for resource management. Key examples illustrate the functionality of loops and file handling methods.

Uploaded by

p114ty2
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)
4 views11 pages

Tuiton Loopsfhandling

The document provides an overview of loops and file handling in Python, detailing the use of for and while loops, control statements, and file operations. It explains how to open, read, write, and append to files, emphasizing the use of the 'with' statement for resource management. Key examples illustrate the functionality of loops and file handling methods.

Uploaded by

p114ty2
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/ 11

Loops and File

Handling
REVISION IN DETAIL
1. for Loop:
The for loop in Python is used to iterate over
a sequence (such as a list, tuple, string,
etc.) or other iterable objects.

fruits = ["apple", "banana", "orange"]

for fruit in fruits:


print(fruit)

Loops
Iterations through range
for i in range(1, 5):
print(i)
The While loop
The while loop in Python repeatedly executes
a block of code as long as a specified
condition is True
count = 1
while count <= 5:
print(count)
count += 1
Control Statements in Loops:

Python provides control statements like


break, continue, and else that can be used
within loops for various functionalities.
numbers = [1, 2, 3, 4, 5, 6]

for num in numbers:


if num == 4:
break # Exit the loop when the number is
4
print(num)
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
continue # Skip even numbers
print(num)

Example using else with loops:


numbers = [1, 2, 3, 4, 5]

for num in numbers:


if num == 6:
break
else:
print("All numbers were iterated successfully.")
File Handling In Python
File handling in Python refers to the
manipulation, reading, writing, and
processing of files. It involves operations
on files stored in the computer's storage
system. Python provides various built-in
functions and methods that enable users
to perform file handling operations
effectively.
Opening a File
The open() function is used to open a file. It
takes two arguments: the file name and
the mode ('r' for reading, 'w' for writing, 'a'
for appending, 'r+' for reading and writing,
etc.).
file = open('example.txt', 'r')
Reading a file.
Once a file is opened in read mode, you can
read its contents using various methods
like read(), readline(), or readlines().
file = open('example.txt', 'r')
content = file.read()
print(content)
file.close()

Example using readline():


file = open('example.txt', 'r')
line = file.readline()
print(line)
file.close()
Example using readlines():

file = open('example.txt', 'r')


lines = file.readlines()
for line in lines:
print(line.strip()) # Removing newline
characters
file.close()
Writing to a File
 We can open a file in write mode and write
content to it using the write() method.
file = open('example.txt', 'w')
file.write('This is a sample text.')
file.close()
Appending to a file
Appending content to an existing file can
be done by opening it in append mode
('a') and using the write() method.
file = open('example.txt', 'a')
file.write('\nThis is appended text.')
file.close()
Using with Statement
The with statement ensures proper handling
of resources and automatically closes the
file when done.
with open('example.txt', 'r') as file:
content = file.read()
print(content)
# File is automatically closed outside the
`with` block

You might also like