Everything About Python - Beginner To Intermediate
Everything About Python - Beginner To Intermediate
Abhijit Paul
What is Python
1. It is a high-level, general-purpose programming language
2. It is used for: web development (server-side), software
development, mathematics, data science, system scripting.
3. It’s design philosophy is to enhance code-readability with the
use of indentation
4. It is dynamically typed & auto garbage-collected language
5. It works on diferent operating system
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 1 of 87
:
6. It runs on an interpreter system, which means each line of
code can be executed as soon as its written, i.e. supports
quick prototyping
7. It is a multi-paradigm programming language, which means
python supports Object Oriented Programming, Procedural
Programming and Structured Programming
Python Packages
1. Package in Python is a folder & Module is a python file.
2. Python Package that can contains single or multiple sub-
folders called as sub-modules, single or multiple python files.
3. Python module can contains several classes, functions,
variables, etc.
4. On installation of a package it resides in the system and only
when we import it, it gets loaded in RAM to avoid wastage of
RAM.
5. Advantages of using packages are it reduces coding and
increases code re-usability.
from math import sqrt # Here we are importing the specific method
print(sqrt(81))
import math # Here we are importing the math module itself. And t
print(math.sqrt(81))9.0
9.0
Keywords in Python
1. Keywords are special reserved words in Python that have
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 2 of 87
:
specific meanings and purposes
2. They can’t be used for anything but its own defined purpose
only.
3. They are in-built and need not to be imported.
4. There are 36 keywords in Python3.9
Case sensitive
Python is a case sensitive language that means captial & small
letters are treated differently
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 3 of 87
:
#None = 10 # cannot assign to None is the syntax error
none = 10
print(none)10marks1 = 80
Marks1 = 90
print(sum1)170
Identifiers
1. Its the name we give to identify a variable, function, class,
module or other object.
2. That means, whenever we want to give an entity a name, that’s
called ‘identifier’
3. It can start with Alphanumeric character & underscore
4. It cannot start with a Digit or Any special characters
5. It cannot be a reserved keyword
# Identifiers
Variables
1. Definition: In Python, a variable is a named location used to
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 4 of 87
:
store data in memory.
2. Declaration and Assignment: Variables are declared by
writing the variable name and assigning it a value using the
equals sign (=). For example:
Comments
1. Definition: A comment in Python is a piece of text un your
vode that is not executed. Its typically used to explain what the
code is doing or leave notes fort developers who will be
reading ot maintaing the code
2. Single Line Comments: Python uses a has symbol (#) to
denote a comment. Anything written after Hash will be ignored
by Python interpreter
3. Multi-Line Comments: Multi-Line strings using triple
quotes(‘’’ or “””) can be used as multi-line comments, as any
strings written within will be ignored by Python interpreter
# Add to numbers
a=4 # value of a is 4
b=5 # value of b is 5
c = a+b # sum is in c
print(c)9def add_number(a,b):
"""
Parameters:
a (int): First variable
b (int): Second variable
Return:
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 5 of 87
:
int: sum of variables a & b
"""
return a+bprint(add_number.__doc__) # to print the doc string
a (int): First variable
b (int): Second variable
Return:
int: sum of variables a & b# Multi Line Comment: We use Triple
Indentation
1. Importance: In Python, identation is not just for readability. Its
part of the syntax and is used to indicate a block of code
2. Space Usage: Python uses identation to define the scope of
loops, functions, classes, etc. Its Four Spaces or a Tab
3. Colon: Usually, a colon(:) at the end of a line is followed by an
intended block of code.
4. Without properly indenting the Python code, you will end up
seeing ‘IndentationError’ and the code will not get compiled.
5. Python Indentation is a way of telling a Python interpreter that
the group of statements belongs to a particular block of code
if True:
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 6 of 87
:
#print("This is True") # IndentationError: expected an indented b
print("This is True")This is True
Statements
1. Definition: A statement in Python is a logical instruction that
the Python interpreter can read and execute.
2. In general, Any instruction written in the source code and
executed by the Python interpreter is called a statement.
3. Types: Python includes several types of statements viz,
assignment statements, conditional statement, looping
statement, etc
# Conditional statement
x = 2
if x > 0:
print("{} is a positive number".format(x))
# Looping Statement
for i in range(3):
print("I am {}".format(i+1))2 is a positive number
I am 1
I am 2
I am 3
Multi-Line Statement
In Python, end of statement is identified by a newline character i.e
‘\n’. But we can make a statement extend over multiple line.
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 7 of 87
:
1. continuation character ‘\’
2. paranthetis ()
3. brackets []
4. braces {}
5. semi-colon “;”
# continuation character.
g = "Geeks \
for \
Geeks"
print(g)Geeks for Geeks# Paranthesis
X = (1+2+4+5+6+7+1+2+4+5+6+7+
1+2+4+5+6+7+1+2+4+5+6+7+1+
2+4+5+6+7+1+2+4+5+6+7+1+2+
4+5+6+7+1+2+4+5+6)
print(X)
Y = {1+2+4+5+6+7+1+2+4+5+6+7+
1+2+4+5+6+7+1+2+4+5+6+7+1+
2+4+5+6+7+1+2+4+5+6+7+1+2+
4+5+6+7+1+2+4+5+6}
print(Y)
Z = [1+2+4+5+6+7+1+2+4+5+6+7+
1+2+4+5+6+7+1+2+4+5+6+7+1+
2+4+5+6+7+1+2+4+5+6+7+1+2+
4+5+6+7+1+2+4+5+6]
print(Z)193
{193}
[193]
Dynamic Typing
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 8 of 87
:
Python is dynamically typed, which means that you dont have to
declare the type of a variable when you create one. You can chage
the type of data held by a variable at any time throughout the life of
program
#Dynamic Typing
Data Types
Primitive Data Types:
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 9 of 87
:
1. List: Ordered collection, Mutuable, Hetergoenous data,
supports Indexing & Slicing
2. Tuple: Ordered collection, Immutuable, Hetergoenous data,
supports Indexing & Slicing
3. Dictionary: Ordered collection, Mutable, Key-Value pair,
Values can be Hetergoenous data, Keys are immutable
4. Set: Unordered collection, Mutable, Contains Unique value
only, Hetergoenous data
a = 10 #'int' datatype
print(f"Data Type is {type(a)}")
Lists
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 10 of 87
:
brackets [], separated by commas.
Tuples
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 11 of 87
:
print("Indexing: Data in list at position 1 is ", e[1])
print("Slicing: Data in list at between position 1 to 4 is ", e[1:
Tuple: (1, 2.2, 'data', [3, 'data1'])
Indexing: Data in list at position 1 is 2.2
Slicing: Data in list at between position 1 to 4 is (2.2, 'data',
Dictionaries
Updating Values: You can update the value for a particular key by
using the assignment operator =.
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 12 of 87
:
Adding and Deleting Key-Value Pairs: You can add a new key-
value pair simply by assigning a value to a new key. You can delete
a key-value pair using the del keyword.
t['fruit3'] = 'papaya'
print("Updated dictionary: ", t)
t['fruit4'] = "sugarcane"
print("Added new key-value to dictionary: ", t)
t['fruit5'] = "kiwi"
print("Added new key-value to dictionary: ", t)
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 13 of 87
:
is unique.
# difference
art = {"Bob", "Rolf", "Anne", "Charlie"}
science = {"Bob", "Jane", "Adam", "Anne"}
# union
all_science_art = science.union(art) # print(science | art)
print(all_science_art)
# intersection
both = science.intersection(art) # print(science & art)
print(both){'Adam', 'Jane'}
{'Bob', 'Rolf', 'Jane', 'Anne', 'Adam', 'Charlie'}
{'Anne', 'Bob'}
List Comprehension
l = [1, 2, 3, 4, 5, 6]
lc = [i**2 for i in l]
print(lc)[1, 4, 9, 16, 25, 36]
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 14 of 87
:
Dictionary Comprehension
Standard Output
print() is a standard output function which displays anything passed
as an argument to the function
Standard Input
input() is a standard input function which accepts anything passed
as an argument to the function in string format
Operators
Arthmetic / Mathametical operators
+, -, /, *, //, %, **
a = 10
b = 3
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 15 of 87
:
print("a * b: ", a*b)
print("a / b: ", a/b)
print("a // b: ", a//b)
print("a % b: ", a%b)
print("a ** b: ", a**b)a + b: 13
a - b: 7
a * b: 30
a / b: 3.3333333333333335
a // b: 3
a % b: 1
a ** b: 1000
Logical Operators
and, or, not
print("AND operator")
print(True and True)
print(True and False)
print(False and True)
print(False and False)
print("OR operator")
print(True or True)
print(True or False)
print(False or True)
print(False or False)
print("NOT operator")
print(not True)
print(not False)AND operator
True
False
False
False
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 16 of 87
:
OR operator
True
True
True
False
NOT operator
False
True
Bitwise Operator
&, |, ^, >>, <<
a = 10 # 1010
b = 4 # 0100
# Bitwise AND
print("a & b ", a&b) # 1010 and 0100 --> 0000 --> 0 (apply 'and'
# 10 = 1 0 1 0
# 4 = 0 1 0 0
# =============
# 0 0 0 0 = 0
# Bitwise OR
print("a | b ", a|b) # 1010 or 0100 --> 1110 --> 14(apply 'or' o
# 10 = 1 0 1 0
# 4 = 0 1 0 0
# =============
# 1 1 1 0 = 14
# Bitwise XOR --> compares each bit of 1st operand with the 2nd
# If one of the bit is 1 (but not both) , the corresponding result
print("a ^ b ", a^b)
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 17 of 87
:
# 10 = 1 0 1 0
# 4 = 0 1 0 0
# =============
# 1 1 1 0 = 14
# 10 = 1 0 1 0
# 4 = 1 1 0 0
# =============
# 0 1 1 0 = 6
a = 10
print("a << 3 ", a << 3)
# 10 = 010
# 80 = 1010000a & b 0
a | b 14
a ^ b 14
a >> 2 2
a << 2 40
a << 3 80
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 18 of 87
:
bin(10)'0b1010'
x = 2 # x is assigned a value of 2
y = 4 # y is assigned a value of 4
if x < y:
print(f"x: {x} is less than y: {y}")
else:
print(f"x: {x} is greater than y: {y}")x: 2 is less than y: 4
while loop
A while loop is used to execute a block of statements repeatedly
until a given condition is satisfied.
And when the condition becomes false, the line immediately after
the loop in the program is executed.
counter = 0
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 19 of 87
:
2: I enjoy coding!
3: I enjoy coding!
4: I enjoy coding!
While loop ended
for loop
For loops are used for sequential traversal
Both for loop and while loop is used to execute the statements
repeatedly while the program runs.
The major difference between for loop and the while loop is that
# break
for i in range(5):
print(f"Iteration count: {i}")
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 20 of 87
:
if i >= 3:
print("Breaking for loop")
breakIteration count: 0
Iteration count: 1
Iteration count: 2
Iteration count: 3
Breaking for loop# continue
for i in range(5):
if i == 3:
print("Continuing with next iteration")
continue # skips the current iteration
print(f"{i}: Some more operation...")0: Some more operation...
1: Some more operation...
2: Some more operation...
Continuing with next iteration
4: Some more operation...
Functions
Advantags of having functions:
1. Makes programming fast
2. Reusability
Built-In Functions
# len
l = len('abc') # 3
# print
print(l) # prints the given input3
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 21 of 87
:
User Defined Functions
Function with no arguments
def introduction():
print("Hello, this is my first function")
During the function call, user can pass ’n’ number of arguments and
while perform the operations all of them will be considered
Returns:
int: sum value
"""
print(f"Number of arguments: {len(args)}")
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 22 of 87
:
return f"Sum: {sum(args)}"
print(calculate_sum(2,3))
print(calculate_sum(2,3,5))Number of arguments: 2
Sum: 5
Number of arguments: 3
Sum: 10
def makeSentence(**kwargs):
sentence=''
for word in kwargs.values():
sentence+=word
return sentence
Recursion Function
A function that calls itself is said to be recursive, and the technique
of employing a recursive function is called recursion.
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 23 of 87
:
The same holds true if multiple instances of the same function are
running concurrently.
def function():
x = 10
function()
These two instances of the name x are distinct from each another
and can coexist without clashing because they are in separate
namespaces.
# But the main problem with recursion is that we need to set some
# else it will throw `RecursionError: maximum recursion depth exce
function()--------------------------------------------------------
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 24 of 87
:
2 x = 10
----> 3 function()
countdown(5)5
4
3
2
1
0# Factorial using recursion
def factorial(n: int):
if n < 2:
return 1
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 25 of 87
:
else:
return n * factorial(n-1)
print(fibonacci(7))33
Functional Programming
Functional programming is a programming paradigm in which
we try to bind everything in pure mathematical functions style.
It is a declarative type of programming style.
Its main focus is on “what to solve” in contrast to an imperative
style where the main focus is “how to solve“.
It uses expressions instead of statements. An expression is
evaluated to produce a value whereas a statement is executed
to assign variables.
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 26 of 87
:
To take another function as an argument
To return another function to its caller
Lambda Function
is a small anonymous function which can take any number of
arguments, but can only have one expression. e.g. lambda
argument(s) : expression
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 27 of 87
:
max_num = lambda x,y: x if x>y else y
print(max_num(4,9))9
map()
It is a built-in function that allows us to process and transform
all the items in an iterable without using an explicit for loop.
This technique is known as mapping.
It is useful when we need to apply a transformation function to
each item in an iterable and transform them into a new iterable.
Two arguments: map(function, iterable)
def square(num):
return num ** 2
numbers = [2,4,6,8]
print(list(map(square, numbers)))[4, 16, 36, 64]
l1 = [1,2,3]
l2 = [9,6,5]
print(list(map(lambda x,y: x-y, l2, l1)))[8, 4, 2]
filter()
Its a built in function that yields the items of the input iterable
for which function returns True.
It means that filter() will check the truth value of each item in
iterable and filter out all of the items that are falsy
Two arguments: filter(function, iterable)
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 28 of 87
:
# Suppose we need to filter out (-ve) value from the list of mixed
import math
# As a next step we can now calculate the squared root using map()
print(list(map(math.sqrt, filter(lambda x: x>0, data))))[49, 36, 8
[7.0, 6.0, 9.0]numbers = [1,2,3,4,5,6,7,8]
def even(a):
return a%2==0
#alternative way
result = filter(lambda a: a%2==0, range(20))
print(list(result))[2, 4, 6, 8]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
reduce()
It performs functional computation by taking a function and an
iterable (e.g., list, tuple, dictionary, etc.) as arguments
It doesn’t return multiple values or any iterator, it just returns a
single value as output which is the result of the whole iterable
getting reduced to only a single integer or string or boolean.
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 29 of 87
:
# initializing list
l = [1, 3, 5, 6, 2]
sorted()
It returns a sorted list of the specified iterable object (e.g list,
tuple)
iter() → Iterators
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 30 of 87
:
This method returns an iterator for the given argument which is
a iterable
One argument: iter(iterable)
numbers = [1,2,3,4,5,6,7,8]
iteration = iter(numbers)
Generator
Its an elegant and simple way to create custom iterators in
Python
A generator is a function that returns an iterator that produces
a sequence of values when iterated over.
It is useful when we want to produce a large sequence of
values, but we don’t want to store all of them in memory at
once. (Memory Efficient approach)
Its defined as a normal function with def, but instead of return
statement it has yield
The yield keyword is used to produce a value from the
generator and pause the generator function’s execution until
the next value is requested.
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 31 of 87
:
#Generating data stream upto maximum value without storing in memo
def even_generator(max=2):
n = 2
while n <= max:
yield n
n += 2
result = even_generator(4)
print(next(result)) # --> max=2 -> yields n=2 --> prints 2 and n=2
print(next(result)) # --> max=2 -> yields n=4 --> prints 4 and n=4
print(next(result)) # --> max=2 -> yields n=6 --> n <= max conditi
4
------------------------------------------------------------------
def generate_fibonacci():
n1 = 0
n2 = 1
while True:
yield n1
n1 , n2 = n2, n1 + n2
seq = generate_fibonacci()
print(next(seq))
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 32 of 87
:
print(next(seq))
print(next(seq))
print(next(seq))
print(next(seq))0
1
1
2
3
Exception Handling
Some of the most common types of exceptions are:
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 33 of 87
:
OverflowError: Raised when the result of an arithmetic
operation is too large to be expressed by the normal number
format.
AttributeError: Raised when an attribute reference or
assignment fails.
SyntaxError: Raised when the parser encounters a syntax
error.
IndentationError: Raised when there is incorrect indentation.
NameError: Raised when a local or global name is not found.
try block: The code within the try block contains the
statements that may potentially raise an exception. It allows
you to specify the section of code that you want to monitor for
exceptions.
except block: If an exception occurs within the try block, the
corresponding except block(s) are executed. The except block
allows you to define the actions or code that should be
executed when a specific exception is raised. You can have
multiple except blocks to handle different types of exceptions.
The else block allows you run code without errors.
The finally block executes code regardless of the try-and-
except blocks.
Use the raise keyword to throw (or raise) an exception.
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 34 of 87
:
try:
print("Good morning today is 17th June")
except:
print("Some issue")
else:
print("No issues")Good morning today is 17th June
No issues"""If the "finally" block is supplied,
it will be executed whether or not the try block raises an erro
try:
x = 2
print(x)
except:
print("There is no X")
finally:
print("The 'try except' executed")2
The 'try except' executed# Use the "raise" keyword to throw an exc
x = 2
if x < 10:
raise Exception("There is a problem: X is below zero")----------
3 x = 2
5 if x < 10:
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 35 of 87
:
----> 6 raise Exception("There is a problem: X is below zero")
ZeroDivisionError
result = n / d
print("Result:", result)------------------------------------------
----> 4 result = n / d
5 print("Result:", result)
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 36 of 87
:
ZeroDivisionError: division by zerotry:
n = int(input("Please enter the numerator: "))
d = int(input("Please enter the denominator: "))
result = n / d
print("Result:", result)
except ZeroDivisionError:
print("There is an Error: Division by zero is not allowed.")Th
ValueError
result = n / d
print("Result:", result)Result: 3.0try:
n = int(input("Please enter the numerator: "))
d = int(input("Please enter the denominator: "))
result = n / d
print("Result:", result)
except ValueError:
print("Please enter valid integers for the numerator and denom
try:
n = int(input("Please enter the numerator: "))
d = int(input("Please enter the denominator: "))
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 37 of 87
:
result = n / d
print("Result:", result)
except ValueError:
print("Please enter valid integers for the numerator and denom
except ZeroDivisionError:
print("Division by zero is not allowed.")Please enter the nume
Please enter the denominator: 2
Result: 1.0
TypeError
try:
x = "10" # Assigning a string value to the variable x
y = 2
print("Result:", z)
except TypeError:
print("Error: TypeError occurred.")Error: TypeError occurred.
IndexError
try:
list1 = [1, 2, 3]
except IndexError:
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 38 of 87
:
print("IndexError error occurred.")IndexError error occurred.
KeyError
try:
my_dictionary = {"name": "John", "age": 30}
except KeyError:
print("KeyError error occurred.")KeyError error occurred.
FileNotFoundError
try:
file_location = "my_file.txt"
except FileNotFoundError:
print(f"File '{file_location}' not found.")File 'my_file.txt'
IOError
try:
file_location = "file123.txt"
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 39 of 87
:
except IOError:
print(f"Unable to write to file '{file_location}'.")Unable to
ImportError
try:
import library1234
except ImportError:
print("Unsuccessful to import module 'library1234'.")Unsuccess
MemoryError
except MemoryError:
print("Insufficient memory for the list.")Cannot execute code,
The Kernel crashed while executing code in the the current cell or
Cannot execute code, session has been disposed. Please try restart
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 40 of 87
:
The Kernel crashed while executing code in the the current cell or
OverflowError
except OverflowError:
print("Error: Calculation resulted in an overflow.")
except ValueError:
print("Exceeds the limit (4300) for integer string conversion;
else:
print(result)7888609052210118054117285652827862296732064351090
AttributeError
try:
age = 20
except AttributeError:
print("'age' object has no attribute.")
else:
print(result)'age' object has no attribute.
SyntaxError
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 41 of 87
:
try:
print("John age is:"
age = 20
except SyntaxError:
print("Error: Invalid syntax.")Cell In[14], line 3
age = 20
IndentationError
try:
name = "John"
age = 50
except IndentationError:
print("There is an IndentationError")Cell In[15], line 3
age = 50
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 42 of 87
:
NameError
try:
side = "4"
print(area) # Attempting to access an undefined variable
except NameError:
print("NameError is there")NameError is there
User-defined Exceptions
By deriving a new class from the default Exception class in
Python, we can define our own exception types.
class MyCustomError(Exception):
pass
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 43 of 87
:
MyCustomError: This is a custom error
Custom Exception
try:
input_num = 16
if input_num < n:
raise WrongAge # calling your custom exception
else:
print("You can work")
except WrongAge:
print("Invalid Age: You are not allowed to work")Invalid Age:
Logging
Logging is a technique for monitoring events that take place
when some software is in use.
For the creation, operation, and debugging of software,
logging is crucial.
There are very little odds that you would find the source of the
issue if your programme fails and you don’t have any logging
records.
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 44 of 87
:
Additionally, it will take a lot of time to identify the cause.
""" In the code above, we first import the logging module, then we
basicConfig method to configure the logging.
We set the level to DEBUG, which means that all logs of level
DEBUG and above will be tracked."""
logging.basicConfig(level=logging.DEBUG)
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 45 of 87
:
issues that have come up.
Here are the different log levels in increasing order of severity:
DEBUG: Detailed information, typically of interest only when
diagnosing problems.
INFO: Confirmation that things are working as expected.
WARNING: An indication that something unexpected
happened, or may happen in the future (e.g. ‘disk space low’).
The software is still working as expected.
ERROR: More serious problem that prevented the software
from performing a function.
CRITICAL: A very serious error, indicating that the program
itself may be unable to continue running.
Debug
import logging
logging.basicConfig(level=logging.DEBUG)
Info
import logging
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 46 of 87
:
logging.basicConfig(level=logging.INFO)
def login(user):
logging.info('User %s logged in', user)
Warning
import logging
logging.basicConfig(level=logging.WARNING)
def MyBalance(amount):
if amount < 40000:
logging.warning('Sorry you have Low balance: %s', amount)
Error
import logging
logging.basicConfig(level=logging.ERROR)
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 47 of 87
:
LetUsDivide(4, 0)ERROR:root:You are trying to divide by zero, whic
Critical Errors
import logging
logging.basicConfig(level=logging.CRITICAL)
def LetUsCheckSystem(sys):
if sys != 'OK':
logging.critical('System failure: %s', sys)
import os
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 48 of 87
:
# Specify the directory and file
dir_path = "/Users/abhijitpaul/Documents/Personal/Learning/iNeuron
log_file = 'system.txt'
full_path = join(dir_path, log_file)
# Set up logging
# Get a logger instance (this will fetch the root logger)
logger = logging.getLogger()
def LetUsCheckSystem(sys):
if sys != 'OK':
logging.critical('System failure: %s', sys)
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 49 of 87
:
def addition(a, b):
pdb.set_trace() # Set a breakpoint here
result = a + b
return result
CLASS
It is a blueprint of an object or you can say its the design of an
object
Attributes
Acct No.
Acct Holder Name
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 50 of 87
:
Balance
Account Type
IFSC
MObile No
Methods
Deposit()
Withdrawal()
Transfer()
OBJECT
Creating object of a class which will allocate memory in the
system to load the class members and utilize it
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 51 of 87
:
Dunder methods are methods that allow instances of a class to
interact with the built-in functions and operators of the class
itself provided by Python Language
In other words, all the operations we do on a Python object is
implemented using these dunder methods
The two underscores are there just to prevent name collision
with other methods implemented by unsuspecting
programmers.
__init__
self
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 52 of 87
:
"__init__ is the dunder method that INITialises the instan
"""
self.first_name = first_name
self.last_name = last_name
__repr__
class Person:
def __repr__(self) -> None:
return f"This is a Person class without customized represe
p = Person()
print(f"Print Person instance: {p}")Print Person instance: This is
__str__
class Person:
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 53 of 87
:
def __str__(self) -> None:
return f"This is a Person class without customized represe
p = Person()
print(f"Print Person instance: {p}")Print Person instance: This is
__getattr__
class Person:
def __init__(self, first_name, last_name) -> None:
self.first_name = first_name
self.last_name = last_name
__setattr__
class Person:
def __init__(self, first_name, last_name) -> None:
self.first_name = first_name
self.last_name = last_name
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 54 of 87
:
We will create a Class called ‘ToDo’ which will keep a track of to-do
list
class ToDo:
def __init__(self, owner: str) -> None:
self.owner = owner
self.tasks = []
todo.display_tasks()
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 55 of 87
:
4-Pillars of Object-Oriented Programming
(OOPs)
1. Inheritance → Provides reusability
2. Encapsulation → Provides security | Binding data with methods
| No exposing data directly by allowing data accessibility by
using those methods
3. Abstraction → Hides the backend implementation
4. Polymorphism → Provides many forms
Inheritance
Inheritance is one of the characteristics of Object Oriented
Programming paradigm.
It facilitates the passing of attributes and methods of a class
(base) to another class (derived)
A child class acquires the properties and can access all the
data members and functions defined in the parent class.
Moreover, a child class can also provide its specific
implementation to the functions of the parent class.
Usages:
Types:
Single Inheritance
Multilevel Inheritance
Multiple Inheritance
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 56 of 87
:
Single Inheritance
In Single Inheritance, one child inherits from one parent i.e. there
will be a single base class and a single derived class
def info(self):
print(f"Name: {self.name}")
def info(self):
super().info() # Reusing the info() method of base class
print(f"Gender: {self.gender}")
Multilevel Inheritance
In Multilevel Inheritance, one child class inherits attributes and
methods from a parent class and subsequently becomes the parent
class of another child class.
class Parent:
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 57 of 87
:
def __init__(self,name):
self.name = name
def getName(self):
return self.name
class Child(Parent):
def __init__(self,name,age):
super().__init__(name)
self.age = age
def getAge(self):
return self.age
class Grandchild(Child):
def __init__(self,name,age,location):
super().__init__(name,age)
self.location=location
def getLocation(self):
return self.location
gc = Grandchild("Srinivas",24,"Assam")
print(gc.getName(), gc.getAge(), gc.getLocation())Srinivas 24 Assa
Multiple Inheritance
In Multiple Inheritance, one child class can inherit attributes and
methods from multiple parent classes i.e there will be multiple base
class and a single derived class
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 58 of 87
:
self.name = name
self.age = age
def person_info(self):
print(f"Name: {self.name} & Age: {self.age}")
def company_info(self):
print(f"Company Name: {self.name} & Domain: {self.domain}"
def employee_info(self):
super().person_info()
super().company_info()
print(f"Salary: {self.salary}")
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 59 of 87
:
It is the order in which Python looks for a method in a hierarchy
of classes.
It plays vital role in the context of multiple inheritance as a
single method may be found in multiple super classes
Python constructs the order in which it will look for a method in
the hierarchy of classes.
It uses this order, known as MRO, to determine which method it
actually calls.
It is possible to see MRO of a class using mro() method of the
class.
#CASE1
class A:
def process(self):
print('A process()')
class B:
pass
obj = C()
obj.process()
print(C.mro()) # C -> A -> B -> ObjectCLassA process()
[<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>,
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 60 of 87
:
So, first it goes to super class given first in the list then second
super class, from left to right order. Then finally Object class, which
is a super class for all classes.
#CASE2
class A:
def process(self):
print('A process()')
class B:
def process(self):
print('B process()')
class D(C,B):
pass
obj = D()
obj.process()
class B(A):
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 61 of 87
:
def process(self):
print('B process()')
obj = C()
obj.process() # C -> A -> B -> objectClass (A cannot be before B
The problem comes from the fact that class A is a super class for
both C and B. If you construct MRO then it should be like this: C ->
A -> B -> A
# Scenario 1
class A:
def __init__(self) -> None:
print("A")
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 62 of 87
:
class B:
def __init__(self) -> None:
print("B")
obj = C() # Here as well MRO came into picture. super() method c
class A:
def __init__(self) -> None:
print(f"Inside A. Type of self: {type(self)}") # But this
print("A")
class B:
def __init__(self) -> None:
print(f"Inside B. Type of self: {type(self)}") # But this
print("B")
Encapsulation:
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 63 of 87
:
It provides security by binding data with methods ensuring no
data is not exposed directly and allowing data accessibility by
using the methods only.
We bind the data with methods
Its bundling of data along with the methods that operates on
that data, into a single unit. e.g. Class, Containers
Its a way to restrict the direct access to both attributes and
methods of an object, so that unauthorised users cannot
access it.
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 64 of 87
:
# public function to show the details
def show(self):
print("The code of the company")
Abstraction:
Its a process of handling complexity by hiding unnecessary
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 65 of 87
:
information from the user.
It enables the user to implement more complex logic on top of
the provided abstraction without understanding or even
thinking about all the hidden complexities
# Note:
# 1. A class is defined as an Abstract Class by inheriting from `A
# 2. A method is defined as an Abstract Method by preceding its de
class Shape(ABC):
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 66 of 87
:
def __init__(self, shapeType) -> None:
self.shapeType = shapeType
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Rectangle(Shape):
def __init__(self, length, breadth) -> None:
self.length = length
self.breadth = breadth
super().__init__("Rectangle")
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 67 of 87
:
Polymorphism
Polymorphism means having different forms
In Python, polymorphism refers to a function having same
name but being used in different ways and different scenarios.
Types
There are two different ways we can achieve Polymorphism.
Method Overloading
In this, more than one method in same class shares the same
method name but different signatures.
@dispatch(int, int)
def product(first: int, second: int):
return first * second
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 68 of 87
:
def product(first: int, second: int, third: int):
return first * second * third
print(product(2,3))
print(product(3,4,5))
print(product(1.1,2.2, 3.3))6
60
7.986000000000001
Method Overriding
Its an OOPs concept and related to Inheritance.
When child class method overrides (inducing its own
implementation) its parent class method having same name,
parameters and return type, its called Method Overriding
# Single Inheritance
class Rectangle:
def area(self, length, breadth):
return length * breadth
class Triangle(Rectangle):
def area(self, base, height):
return 0.5 * base * height
rec = Rectangle()
tri = Triangle()
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 69 of 87
:
print(rec.area(4, 8))
print(tri.area(4, 8))32
16.0import logging
Operator Overloading
class Vegetables:
def __init__(self, carrot: int, beans: int) -> None:
self.carrot = carrot
self.beans = beans
v1 = Vegetables(4, 8)
v2 = Vegetables(9, 3)
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 70 of 87
:
any class methods.|Instance variables are defined within class
methods, typically the constructor| Scope|Changes made to the
class variable affect all instances of that class.|Changes made to
the instance variable does not affect all instances.|
Initialization|Class variables can be initialized either inside the class
definition or outside the class definition.|Instance variables are
typically initialized in the constructor of the class.| Access|Class
variables are accessed using the class name, followed by the
variable name.|Instance variables are accessed using the instance
name, followed by the variable name.| Usage|Class variables are
useful for storing data that is shared among all instances of a class,
such as constants or default values.|Instance variables are used to
store data that is unique to each instance of a class, such as object
properties.|
class Employee:
def display(self):
print(f"First Name: {self.first_name} | Last Name: {self.l
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 71 of 87
:
emp2.display()
print(emp2.company_name) # We can access class variable using the
print(Employee.company_name) # We can access class variable using
ABC Technologies
ABC Technologies
First Name: Mousumi | Last Name: Paul
ABC Technologies
ABC Technologies
NOTE
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 72 of 87
:
global variables.
# Local Variable
a = 20
def func1():
a = 10 # This variable `a` is local variable and is scope ins
print(f"Local Variable value inside the function is {a}")
func1()
print(f"Variable value outside the function is {a}")
Decorators
Decorators are functions (or classes) that provide enhanced
functionality to the original function (or class) without the
programmer having to modify their structure.
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 73 of 87
:
2. Class level decorator
3. Property (Setter | Getter | Deleter)
# Sequence of execution
def hello_decorator(func): #3 func = sum_two_numbers
def inner(a,b): #6
print("Before execution") #7
value = func(a,b) #8 Here sum_two_number function will be
print("After execution") #12
return f"Computed value: {value}" #13 End of execution
return inner #4 It returns the inner() function definition wh
grades = {
5: 'A+',
4: 'A',
3: 'B',
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 74 of 87
:
2: 'C',
1: 'D'}
return percent, grades[percent // 20]
return wrapper
class Student:
def __init__(self, name, score, total):
self.name = name
self.__score = score
self.total = total
@staticmethod
@grade_decorator
def get_percent(score, total):
return score/total * 100
def get_record(self):
percent, grade = Student.get_percent(self.__score, self.to
return f"Name: {self.name} | Percentage scored: {percent}
def __str__(self):
return f"Name: {self.name} | Score: {self.__score} | Total
def make_secure(func):
@functools.wraps(func)
def secure_function():
if user["access_level"] == "admin":
return func()
else:
return f"No admin permission for {user['username']}"
return secure_function
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 75 of 87
:
@make_secure
def get_admin_password():
return "1234"
print(get_admin_password())
print(get_admin_password.__name__) # It prints `secure_function`
get_admin_password# Decorators with parameters
import functools
def make_secure(func):
@functools.wraps(func)
def secure_function(*args, **kwargs):
if user["access_level"] == "admin":
return func(*args, **kwargs)
else:
return f"No admin permission for {user['username']}"
return secure_function
@make_secure
def get_password(panel):
if panel == "admin":
return "1234"
else:
return "super secret password"
print(get_password("admin"))
print(get_password("billing"))1234
super secret password# Decorators with parameters
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 76 of 87
:
import functools
@make_secure("admin")
def get_admin_password():
return "1234"
@make_secure("user")
def get_dashboard_password():
return "super secret password"
print(get_admin_password())
print(get_dashboard_password())
print(get_admin_password())
print(get_dashboard_password())No admin permission for jose
No user permission for jose
1234
super secret password
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 77 of 87
:
2. Class level decorator
Instance Method, Class Method, Static Method
Instance Method:
Class Method:
Static Method:
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 78 of 87
:
Static methods are restricted in what data they can access —
and they’re primarily a way to namespace your methods.
class MyClass:
data = 10 # class variable
def method(self):
# Instance methods can also access the class itself throug
return 'instance method called', self
@classmethod
def classmethod(cls):
return 'class method called', cls
@staticmethod
def staticmethod():
return 'static method called'
obj = MyClass()
# This confirms that the instance method has access to the object
print(obj.method())
# When the method is called, Python replaces the self argument wit
# We could ignore the syntactic sugar of the dot-call syntax (obj.
# print(MyClass.method(obj))
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 79 of 87
:
We can call the classmethod & staticmethod methods on the class
itself - without creating an object instance beforehand
print(MyClass.classmethod())
print(MyClass.staticmethod())('class method called', <class '__mai
static method called# Class Methods are mostly used as Factory Met
class Book:
TYPES = ("Hardcover", "Paperback") # Class variable which sho
@classmethod
def hardcover(cls, name: str, page_weight: int) -> "Book":
return cls(name, cls.TYPES[0], page_weight+100)
@classmethod
def paperback(cls, name: str, page_weight: int) -> "Book":
return cls(name, cls.TYPES[1], page_weight)
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 80 of 87
:
The property decorator is very useful when defining methods for
data validation, like when deciding if a value to be assigned is valid
and won’t lead to issues later in the code.
class Person:
def __init__(self, name):
self.__name = name
@property
def name(self):
print('Getting name')
return self.__name
@name.setter
def name(self, value):
print('Setting name to ' + value)
self.__name = value
@name.deleter
def name(self):
print('Deleting name')
del self.__name
p = Person('Adam')
print('The name is:', p.name)
p.name = 'John'
del p.nameGetting name
The name is: Adam
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 81 of 87
:
Setting name to John
Deleting name
class Employee:
# constructor
def __init__(self, name):
print('Inside Constructor')
self.name = name
print('Object initialized')
def show(self):
print('Hello, my name is', self.name)
# destructor
def __del__(self):
print('Inside destructor')
print('Object destroyed')
# create object
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 82 of 87
:
s1 = Employee('Emma')Inside Constructor
Object initializeds1.show()Hello, my name is Emma# delete object
del s1Inside destructor
Object destroyedfrom typing import List, Optional
Optional[List[int]]typing.Optional[typing.List[int]]
Duck Typing
Duck Typing is a way of programming in which an object
passed into a function or method supports all method
signatures and attributes expected of that object at run time.
The object’s type itself is not important. Rather, the object
should support all methods/attributes called on it.
class Duck:
def sound(self):
print("quack quack")
class Dog:
def sound(self):
print("woof woof")
class Cat:
def __init__(self):
self.sound = "meow meow"
def all_sounds(obj):
if hasattr(obj, 'sound') and callable(obj.sound):
obj.sound()
du = Duck()
do = Dog()
ca = Cat()
for obj in [du, do, ca]:
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 83 of 87
:
all_sounds(obj)quack quack
woof woof
Multi-Threading
Process in Python
In computing, a process is an instance of a computer program that
is being executed. Any process has 3 basic components:
An executable program.
The associated data needed by the program (variables,
workspace, buffers, etc.)
The execution context of the program (State of the process)
Thread:
Entity within a process that can be scheduled for execution
Smallest unit of execution
Multiple threads can exist within one process where:
Each thread contains its own register set and local variables
(stored in the stack).
All threads of a process share global variables (stored in heap)
and the program code.
A thread is simply a subset of a process
A thread contains all this information in a Thread Control Block
(TCB)
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 84 of 87
:
Stack pointer: Points to the thread’s stack in the process. The
stack contains the local variables under the thread’s scope.
Program counter: a register that stores the address of the
instruction currently being executed by a thread.
Thread state: can be running, ready, waiting, starting, or done.
Thread’s register set: registers assigned to thread for
computations.
Parent process Pointer: A pointer to the Process control
block (PCB) of the process that the thread lives on.
Multithreading in Python
Defined as the ability of a processor to execute multiple
threads concurrently.
In a simple, single-core CPU, it is achieved using frequent
switching between threads. This is termed context switching.
In context switching, the state of a thread is saved and the
state of another thread is loaded whenever any interrupt (due
to I/O or manually set) takes place.
Context switching takes place so frequently that all the threads
appear to be running parallelly (this is termed multitasking).
import threading
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 85 of 87
:
import os
def task1():
print("Task 1 assigned to thread: {}".format(threading.current
print("ID of process running task 1: {}".format(os.getpid()))
def task2():
print("Task 2 assigned to thread: {}".format(threading.current
print("ID of process running task 2: {}".format(os.getpid()))#
print("ID of process running main program: {}".format(os.getpid())
# creating threads
t1 = threading.Thread(target=task1, name='t1')
t2 = threading.Thread(target=task2, name='t2')
# starting threads
t1.start()
t2.start()
Resources:
Self made contents while practicing Python. Hence might be very
comprehensive and not with detailed explanations of related topics.
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 86 of 87
:
I will try to come up with further topics in future. Keep watching the
blog.
Summary
In this article, we talked about Python right from scratch to
intermediate level. There are more to learn in Python, however this
would be enough to jump into “Coding world of Python”
Happy coding!
In Plain English
Thank you for being a part of our community! Before you go:
https://python.plainenglish.io/everything-about-python-beginner-to-intermediate-e5922edc5987 25/11/23, 12 27 PM
Page 87 of 87
: