Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

14th-june-23

November 27, 2023

0.0.1 Is python a compiled language or interpreted language even though it uses


both?
• its an interpreted language
• a programming language is said to interpreted or compiled depending on where the memory
allocation and type checking happens.
• providing memory to data is called as memory allocation
• type checking means checking the type of data.
• in python the type checking and memory allocation happens at interpretor stage. hence,
Python is said to be interpreted language

0.0.2 Errors in Python


1. Compile time error
• occurs during the compilation stage
• Two types
• Syntax error
• Indentation error
• if any compile time error occurs, Byte code will not be generated
2. Runtime error
• it happens when the code getting executed (running) after successful compilation of the
code
• also called Exceptions
• spelling mistakes, dividing a number by 0, add a number with an alphabet, …..
[9]: a = 100
print(a)
b = 200
print(b)
c = a + b
print(c)

100
200
300

[ ]: # to call a function we have to start with a bracket and end in a bracket

1
[15]: a = 100
print(a)
b = 200
print(b
c = a + b
print(c)

Input In [15]
c = a + b
^
SyntaxError: invalid syntax

[23]: # since the error has happened at compiled time therefore print(a)
# hasnt worked
# also the error shows at c = a + b because it was expecting maybe the bracket␣
↪closed at next line

[25]: a = 100
print(a)
b = 200
print(b
)
c = a + b
print(c)

100
200
300

[13]: a = 100
print(a)
b = 200
print(b)
c = A + b
print(c)

100
200

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [13], in <cell line: 5>()
3 b = 200
4 print(b)
----> 5 c = A + b
6 print(c)

2
NameError: name 'A' is not defined

[16]: # in this case the byte code will be generated as there is no compilation error
# so line by line will be executed

0.0.3 Python is a case sensitive language means ‘a’ and ‘A’ are diff.

[17]: a = 100
print(a)
b = 200
print(b)
c = A + b
print(c)
d = 10000
print(d)

100
200

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [17], in <cell line: 5>()
3 b = 200
4 print(b)
----> 5 c = A + b
6 print(c)
7 d = 10000

NameError: name 'A' is not defined

[18]: # after the error, the execution of program will stop

[20]: x = 100
print(x)
y = 0
print(y)
z = x / y
print(z)

100
0

---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Input In [20], in <cell line: 5>()

3
3 y = 0
4 print(y)
----> 5 z = x / y
6 print(z)

ZeroDivisionError: division by zero

[21]: # runtime error, as x and y has been printed

[22]: x = 100
print(x)
y = 0
print(y)
z = x $ y
print(z)

Input In [22]
z = x $ y
^
SyntaxError: invalid syntax

[27]: a = 100
print(a)
b = 200
print(b)
c = a + b
prin(c)

100
200

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [27], in <cell line: 6>()
4 print(b)
5 c = a + b
----> 6 prin(c)

NameError: name 'prin' is not defined

[32]: 10 + 'a'

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

4
Input In [32], in <cell line: 1>()
----> 1 10 + 'a'

TypeError: unsupported operand type(s) for +: 'int' and 'str'

1 Memory Management
• managing the memory => allocating and deallocating the space/memory to and from the
values
• inefficient memory management leads to slowness of the program execution
• In Python, memory management is taken care by Python itself (automatic)
• Python Memory Manager (PMM) is responsible to manage the memory

Memory allocation
1. Static memory allocation
• the memory is allocation is done during compile time
• stack memory is used
• memory cannot be re-used
• int a = 100;
2. Dynamic memory allocation
• the memory allocation is done during runtime
• heap memory is used
• memory can be re-used
• a = 100
• In Python, dynamic memory allocation is used
• The PMM allocates the memory to a value and when the value is not needed, PMM auto-
matically deletes the value from the memory.
• Stack memory: memory allocation happens on continuous memory location
• Heap memory: its a pile of memory (non-continuous). It happens randomly.
• PMM has:
– Raw memory allocator: used to interact with the OS to ensure there is enough memory
– Object Specific allocator: used to allocate the memory to the values/data/objects
• Areas created during memory allocation
[ ]:

You might also like