APP - Chapter 2
APP - Chapter 2
11. The process has its own Thread has Parents’ PCB,
Process Control Block, its own Thread Control
Stack, and Address Space. Block, and Stack and
common Address space.
12. Changes to the parent Since all threads of the
process do not affect child same process share
processes. address space and other
resources so any changes
to the main thread may
affect the behavior of the
other threads of the
process.
2. RegEx can be used to check if a string contains the specified search pattern.
4. RegEx Module:-
i) Python has a built-in package called re, which can be used to work with Regular
Expressions.
Regular expressions (called regex or regexp) specify search patterns. Typical examples
of regular expressions are the patterns for matching email addresses, phone numbers,
and credit card numbers.
importing
RegEx Functions
The “re” module provides a set of functions that enables us to
search a string for a match. Some of the functions are listed
below:
findall() function
The findall() function returns a list containing all matches.
Example:
findall
search() function
The search() function takes a regular expression pattern and a
string, and it searches for that pattern within the string. If
the search is successful, search() returns a match object.
Otherwise, it doesn’t return any.
Example:
Search
split() function
The split() function returns a list that shows where the string
has been split at each match.
Example:
i. DateTime in Python can be imported to work with the date as well as time.
ii. Python Datetime module comes built into Python, so there is no need to install it externally.
iii. Python Datetime module supplies classes to work with date and time.
iv. These classes provide several functions to deal with dates, times, and time intervals.
v.Date and DateTime are an object in Python, so when you manipulate them, you are
manipulating objects and not strings or timestamps.
vi.The DateTime module is categorized into 6 main classes:-
date – An idealized naive date, assuming the current Gregorian calendar always was, and
always will be, in effect. Its attributes are year, month, and day.
time – An idealized time, independent of any particular day, assuming that every day has
exactly 24*60*60 seconds. Its attributes are hour, minute, second, microsecond, and tzinfo.
date-time – It is a combination of date and time along with the attributes year, month, day, hour,
minute, second, microsecond, and tzinfo.
timedelta – A duration expressing the difference between two date, time, or datetime instances
to microsecond resolution.
timezone – A class that implements the tzinfo abstract base class as a fixed offset from the
UTC (New in version 3.2).
- Errors and exceptions can lead to unexpected behavior or even stop a program
from executing.
- Python provides various functions and mechanisms to handle these issues and
improve the robustness of the code.
Different types of exceptions in python:
In Python, there are several built-in Python exceptions that can be raised when an error
occurs during the execution of a program. Here are some of the most common types
of exceptions in Python:
SyntaxError: This exception is raised when the interpreter encounters a syntax error in
the code, such as a misspelled keyword, a missing colon, or an
unbalanced parenthesis.
NameError: This exception is raised when a variable or function name is not found in
the current scope.
IndexError: This exception is raised when an index is out of range for a list, tuple, or
other sequence types.
IOError: This exception is raised when an I/O operation, such as reading or writing a
file, fails due to an input/output error.
Syntax Error: As the name suggests this error is caused by the wrong syntax in the
code. It leads to the termination of the program.
Example:
There is a syntax error in the code . The ‘if' statement should be followed by a colon (:),
and the ‘print' statement should be indented to be inside the ‘if' block.
amount = 10000
if(amount > 2999)
print("You are eligible to purchase Dsa Self Paced")
Output
Exceptions: Exceptions are raised when the program is syntactically correct, but the code
results in an error. This error does not stop the execution of the program, however,
it changes the normal flow of the program.
Example:
Here in this code a s we are dividing the ‘marks’ by zero so a error will occur known as
‘ZeroDivisionError’
marks = 10000
a = marks / 0
print(a)
Output:
TypeError: This exception is raised when an operation or function is applied to an object of the
wrong type. Here’s an example:
Here a ‘TypeError’ is raised as both the datatypes are different which are being
Added.
x=5
y = "hello"
z=x+y
output:
Traceback (most recent call last):
File "7edfa469-9a3c-4e4d-98f3-5544e60bff4e.py", line 4, in <module>
z=x+y
TypeError: unsupported operand type(s) for +: 'int' and 'str'
The code attempts to add an integer (‘x') and a string (‘y') together, which is not a valid
operation, and it will raise a ‘TypeError'. The code used a ‘try' and ‘except' block to catch this
exception and print an error message
x=5
y = "hello"
try:
z=x+y
except TypeError:
print("Error: cannot add an int and a str")
Output:
Python provides a keyword finally, which is always executed after the try and except
blocks. The final block always executes after the normal termination of the try block or
after the try block terminates due to some exception.
Syntax:
try:
# Some Code....
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
finally:
# Some code .....(always executed)
Example:
try:
k = 5//0
print(k)
except ZeroDivisionError:
print("Can't divide by zero")
finally:
print('This is always executed')