22616 - Programming with Python (Diploma 6th Sem) - Important Notes
Unit 1: Introduction and Syntax of Python
1. Features of Python:
Definition: Python is a high-level, interpreted, general-purpose programming language known for its
simplicity and readability.
- Easy to learn and use
- Interpreted language
- Dynamically typed
- Portable and platform-independent
- Extensive libraries
- Supports OOP
2. Applications of Python:
- Web development
- Data science
- Automation and scripting
- Game development
- AI/ML applications
3. Python Syntax and Indentation:
Definition: Syntax refers to the structure of statements in a programming language.
Python uses indentation to define code blocks.
Example:
if True:
print("Correct Indentation")
4. Data Types & Variables:
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:
name = input("Enter name: ")
print("Hello", name)
6. Program: Swap without third variable:
a=5
b = 10
a, b = b, a
print(a, b)
Unit 2: Operators and Control Flow
1. Operators:
Definition: Operators are symbols that perform operations on variables and values.
- Arithmetic: +, -, *, /, %, //, **
- Relational: >, <, ==, !=, >=, <=
- Logical: and, or, not
- Membership: in, not in
2. Conditional Statements:
Definition: Conditional statements control the flow of execution based on conditions.
Example:
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
3. Loops:
Definition: Loops are used to execute a block of code repeatedly.
Example:
for i in range(5):
print(i)
x=0
while x < 5:
print(x)
x += 1
4. Loop Control:
- break: exits loop
- continue: skips iteration
- pass: placeholder for future code
5. Program: Prime check:
num = 7
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
print("Prime" if is_prime else "Not Prime")
Unit 3: Data Structures
1. List:
Definition: A list is an ordered, mutable collection of items.
lst = [1, 2, 3]
lst.append(4)
print(lst)
2. Tuple:
Definition: A tuple is an ordered, immutable collection of items.
tup = (1, 2, 3)
3. Set:
Definition: A set is an unordered collection of unique elements.
s = {1, 2, 3}
4. Dictionary:
Definition: A dictionary is a collection of key-value pairs.
d = {"name": "John", "age": 25}
print(d["name"])
5. Comprehensions:
Definition: Comprehensions provide a concise way to create collections.
squares = [x*x for x in range(5)]
Unit 4: Functions and Modules
1. Function Definition:
Definition: A function is a block of code that performs a specific task.
def greet(name):
return "Hello " + name
2. Lambda Function:
Definition: A lambda function is an anonymous function expressed as a single statement.
add = lambda a, b: a + b
print(add(3, 4))
3. Module:
Definition: A module is a file containing Python definitions and statements.
Save as mymodule.py:
def hello():
print("Hello")
Import and use:
import mymodule
mymodule.hello()
Unit 5: OOP in Python
1. Class and Object:
Definition: A class is a blueprint for creating objects. An object is an instance of a class.
class Person:
def __init__(self, name):
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:
def display(self): print("A")
class B(A):
def show(self): print("B")
b = B()
b.display()
b.show()
3. Encapsulation & Data Hiding:
Definition: Encapsulation is the wrapping of data and functions into a single unit. Data hiding
restricts access to internal details.
class Sample:
def __init__(self):
self.__hidden = 10
Unit 6: File & Exception Handling
1. File Handling:
Definition: File handling allows reading from and writing to files.
f = open("file.txt", "r")
print(f.read())
f.close()
2. Writing Files:
with open("file.txt", "w") as f:
f.write("Hello")
3. Exception Handling:
Definition: Exception handling deals with runtime errors to prevent program crashes.
try:
x=1/0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Done")
4. Raise Exception:
Definition: The raise keyword is used to throw exceptions manually.
raise ValueError("Invalid input")
End of Important Notes for 22616 - Programming with Python