0% found this document useful (0 votes)
24 views

APP - Chapter 2

The document discusses the key differences between processes and threads. Processes take more time and resources to create, terminate, and switch between compared to threads. While processes are isolated from each other, threads within the same process can share resources like memory. Context switching and communication are more efficient for threads than processes.

Uploaded by

jumbarthijenil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

APP - Chapter 2

The document discusses the key differences between processes and threads. Processes take more time and resources to create, terminate, and switch between compared to threads. While processes are isolated from each other, threads within the same process can share resources like memory. Context switching and communication are more efficient for threads than processes.

Uploaded by

jumbarthijenil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

APP__Chapter 2 notes

Difference between Process and Thread:


S.NO Process Thread

1. Process means any Thread means a segment


program is in execution. of a process.

2. The process takes more The thread takes less time


time to terminate. to terminate.

3. It takes more time for It takes less time for


creation. creation.

4. It also takes more time for It takes less time for


context switching. context switching.

5. The process is less Thread is more efficient in


efficient in terms of terms of communication.
communication.

6. Multiprogramming holds We don’t need multi


the concepts of programs in action for
multi-process. multiple threads because a
single process consists of
multiple threads.

7. The process is isolated. Threads share memory.

8. The process is called the A Thread is lightweight as


heavyweight process. each thread in a process
shares code, data, and
resources.

9. Process switching uses an Thread switching does not


interface in an operating require calling an operating
system. system and causes an
interrupt to the kernel.

10. If one process is blocked If a user-level thread is


then it will not affect the blocked, then all other
execution of other user-level threads are
processes blocked.

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.

13. A system call is involved in No system call is involved,


it. it is created using APIs.

14. The process does not Threads share data with


share data with each other. each other.
Note: In some cases where the thread is processing a bigger workload compared to a
process’s workload then the thread may take more time to terminate. But this is an
extremely rare situation and has fewer chances to occur.

Introduction to the Python regular expressions

What is Python Regular Expression (RegEx)?

1. A RegEx, or Regular Expression, is a sequence of characters that forms a search


pattern.

2. RegEx can be used to check if a string contains the specified search pattern.

3. A regular expression can be a single character, or a more complicated


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.

Regular expressions are essentially a specialized programming language embedded in


Python. And you can interact with regular expressions via the built-in re module in
Python.
A Python regular expression is a sequence of metacharacters that
define a search pattern. We use these patterns in a
string-searching algorithm to "find" or "find and replace" on
strings.

The term "regular expressions" is frequently shortened to


"RegEx".

In this guide, we will learn the basics of regular expressions


in Python through a demonstration We will begin by importing our
“re” module.

importing

Fig: Importing regular expression module (re)

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

Fig: findall() function

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

Fig: search() function

split() function
The split() function returns a list that shows where the string
has been split at each match.

Example:

Fig: split() function


It splits the string when it encounters space in a string.

Python datetime module

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.

tzinfo – It provides time zone information objects.

timezone – A class that implements the tzinfo abstract base class as a fixed offset from the
UTC (New in version 3.2).

Exception & Error Handling in Python

- 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.

TypeError: This exception is raised when an operation or function is applied to an


object of the wrong type, such as adding a string to an integer.

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.

KeyError: This exception is raised when a key is not found in a dictionary.


ValueError: This exception is raised when a function or method is called
with an invalid argument or input, such as trying to convert a string to an
integer when the string does not represent a valid integer.

AttributeError: This exception is raised when an attribute or method is not found on an


object, such as trying to access a non-existent attribute of a class
Instance.

IOError: This exception is raised when an I/O operation, such as reading or writing a
file, fails due to an input/output error.

ZeroDivisionError: This exception is raised when an attempt is made to divide a


number by zero.
ImportError: This exception is raised when an import statement fails to find or load a
module.

Difference between Syntax Error and Exceptions

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'

try catch block to resolve it:

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:

Error: cannot add an int and a str


Finally Keyword in Python

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:

The code attempts to perform integer division by zero, resulting in a ZeroDivisionError. It


catches the exception and prints “Can’t divide by zero.” Regardless of the exception, the finally
block is executed and prints “This is always executed.”

try:
k = 5//0
print(k)

except ZeroDivisionError:
print("Can't divide by zero")

finally:
print('This is always executed')

You might also like