Presentation ITP
Presentation ITP
Syntax:
for iterator_var in
sequence: statements(s)
SYNTAX OF NESTED
LOOP:-
Syntax
for iterator_var in sequence:
for iterator_var in sequence:
statements(s)
statements(s)
Loop Control Statements
Loop control statements change execution from their normal sequence. When execution
leaves a scope, all automatic objects that were created in that scope are destroyed.
Python supports the following control statements.
Continue Statement
the continue statement in Python returns the control to the beginning of the loop.
# An empty loop
for letter in 'geeksforgeeks':
pass
print('Last Letter :', letter)
OUTPUT
LAST LETTER : S
DAY:-6
ITERATIONN IN PYTHON
An iterator is an object that contains a countable number of values.
An iterator is an object that can be iterated upon, meaning that we can traverse
through all the values.
Technically, in Python, an iterator is an object which implements the iterator
protocol, which consist of the methods __iter__() and __next__().
ITERATOR VS ITERABLE:-
Lists, tuples, dictionaries, and sets are all iterable objects. They are
iterable containers which we can get an iterator from.
All these objects have a iter() method which is used to get an iterator.
Example:-Get :- OUTPUT
Return an iterator from a tuple, and print each value: Apple
mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple) Banana
Cherry
print(next(myit))
print(next(myit))
print(next(myit))
FOR EXAMPLE:-
STOPITERATION:-
To prevent the iteration from going on forever, we can use the StopIteration statement.
In the __next__() method, we can add a terminating condition to raise an error if the
iteration is done a specified number of times:
EXAMPLE:-
def __next__(self):
if self.a <= 20:
x = self.a
self.a += 1
return x
else:
raise StopIteration
myclass = MyNumbers()
myiter = iter(myclass)
for x in myiter:
print(x)
DAY:-7
CLASSES AND OBJECT IN
PYTHON:
CLASSES:-A class is a user-defined blueprint or prototype from which objects are created.
Classes provide a means of bundling data and functionality together. Creating a new class
creates a new type of object, allowing new instances of that type to be made. Each class
instance can have attributes attached to it for maintaining its state. Class instances can
also have methods (defined by their class) for modifying their state.
SAMPLE PROGRAM:-
ADVANTAGES
•Versatility: File handling in Python allows us to perform a wide range of operations, such as creating,
reading, writing, appending, renaming, and deleting files.
•Flexibility: File handling in Python is highly flexible, as it allows us to work with different file types
(e.g. text files, binary files, CSV files, etc.), and to perform different operations on files (e.g. read, write,
append, etc.).
•User–friendly: Python provides a user-friendly interface for file handling, making it easy to create,
read, and manipulate files.
•Cross-platform: Python file-handling functions work across different platforms (e.g. Windows, Mac,
Linux), allowing for seamless integration and compatibility.
DISADVANTAGES
•Error-prone: File handling operations in Python can be prone to
errors, especially if the code is not carefully written or if there are
issues with the file system (e.g. file permissions, file locks, etc.).
•Security risks: File handling in Python can also pose security risks,
especially if the program accepts user input that can be used to
access or modify sensitive files on the system.
•Complexity: File handling in Python can be complex, especially
when working with more advanced file formats or operations.
Careful attention must be paid to the code to ensure that files are
handled properly and securely.
•Performance: File handling operations in Python can be slower
than other programming languages, especially when dealing with
large files or performing complex operations.
DAY 14:-
ACCESS MODES IN FILE HANDLING:-
In Python, there are six methods or access modes, which are:
1.Read Only ('r’): This mode opens the text files for reading only. The start of the file is
where the handle is located. It raises the I/O error if the file does not exist. This is the
default mode for opening files as well.
2.Read and Write ('r+’): This method opens the file for both reading and writing. The
start of the file is where the handle is located. If the file does not exist, an I/O error gets
raised.
3.Write Only ('w’): This mode opens the file for writing only. The data in existing files
are modified and overwritten. The start of the file is where the handle is located. If the file
does not already exist in the folder, a new one gets created.
4.Write and Read ('w+’): This mode opens the file for both reading and writing. The
text is overwritten and deleted from an existing file. The start of the file is where the
handle is located.
5.Append Only ('a’): This mode allows the file to be opened for writing. If the file
doesn't yet exist, a new one gets created. The handle is set at the end of the file. The
newly written data will be added at the end, following the previously written data.
6.Append and Read (‘a+’): Using this method, we can read and write in the file. If the
file doesn't already exist, one gets created. The handle is set at the end of the file. The
DAY 15:-
OPERATIONS IN FILE HANDLING
1.CREATING A FILE:
"x" – Create: this command will create a new file if and only if there is no file already in existence with
that name or else it will return an error.
4. Closing a file:-
It is good practice to always close the file when you are
done with it.
f = open("myfiles.txt", "r") print(f.readline())
f.close()
THANK
YOU