22616_Python_Notes_Clean
22616_Python_Notes_Clean
1. Features of Python:
Definition: Python is a high-level, interpreted, general-purpose programming language known for its
- Interpreted language
- Dynamically typed
- Extensive libraries
- Supports OOP
2. Applications of Python:
- Web development
- Data science
- Game development
- AI/ML applications
Example:
if True:
print("Correct Indentation")
Definition: Data types define the type of data a variable can hold. A variable is a name that refers to
a value.
Common Data Types: int, float, str, list, tuple, dict, set, bool
5. Input/Output Functions:
Example:
print("Hello", name)
a=5
b = 10
a, b = b, a
print(a, b)
1. Operators:
Definition: Operators are symbols that perform operations on variables and values.
- Arithmetic: +, -, *, /, %, //, **
Example:
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
3. Loops:
Example:
for i in range(5):
print(i)
x=0
while x < 5:
print(x)
x += 1
4. Loop Control:
is_prime = True
if num % i == 0:
is_prime = False
break
1. List:
lst = [1, 2, 3]
lst.append(4)
print(lst)
2. Tuple:
tup = (1, 2, 3)
3. Set:
s = {1, 2, 3}
4. Dictionary:
5. Comprehensions:
1. Function Definition:
def greet(name):
2. Lambda Function:
add = lambda a, b: a + b
print(add(3, 4))
3. Module:
Save as mymodule.py:
def hello():
print("Hello")
import mymodule
mymodule.hello()
Unit 5: OOP in Python
class Person:
self.name = name
p = Person("John")
print(p.name)
2. Inheritance:
Definition: Inheritance allows a class to use properties and methods of another class.
class A:
class B(A):
b = B()
b.display()
b.show()
Definition: Encapsulation is the wrapping of data and functions into a single unit. Data hiding
class Sample:
def __init__(self):
self.__hidden = 10
Unit 6: File & Exception Handling
1. File Handling:
f = open("file.txt", "r")
print(f.read())
f.close()
2. Writing Files:
f.write("Hello")
3. Exception Handling:
Definition: Exception handling deals with runtime errors to prevent program crashes.
try:
x=1/0
except ZeroDivisionError:
finally:
print("Done")
4. Raise Exception: