ADVANCED
PYTHON
Session 2
REGEX
A regex is a sequence of characters that forms a
search pattern. It can be used to match a string or find
substrings in a string.
search() method
findall() method
split() method
sub() method
FILE HANDLING
We can handle files using Python and perform
operations like opening, reading, and writing on the files.
Modes:
“r” – read mode (for reading the contents of the file)
“w” – write mode (for writing content to the file)
“a” – append mode (for adding new content to the
end of the file)
Opening files:
Reading files:
Writing to files:
Appending to files:
LAMBDA FUNCTIONS
A lambda function is a small anonymous function (a
function without a name).
Syntax:
lambda arguments : expression
Example:
EXCEPTION HANDLING
To handle exceptions and to call some code when an
exception occurs, you can use a try/except statement.
The try block contains the code that we want to test.
If that exception occurs, the code in the try block stops
its execution, and the code in the except block runs.
Example:
finally
We can use the finally statement at the end of a
try/except block to ensure that some code will run
even if there are errors.
Example:
THANK YOU