0% found this document useful (0 votes)
17 views18 pages

Advanced Python Session 2

The document covers advanced Python topics including regular expressions (regex) for string matching, file handling methods for reading, writing, and appending files, lambda functions for creating anonymous functions, and exception handling using try/except statements. It explains the syntax and usage of these concepts with examples. Additionally, it mentions the use of the finally statement to ensure code execution after exception handling.

Uploaded by

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

Advanced Python Session 2

The document covers advanced Python topics including regular expressions (regex) for string matching, file handling methods for reading, writing, and appending files, lambda functions for creating anonymous functions, and exception handling using try/except statements. It explains the syntax and usage of these concepts with examples. Additionally, it mentions the use of the finally statement to ensure code execution after exception handling.

Uploaded by

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

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

You might also like