Python Notes With Answers
Python Notes With Answers
1. **Introduction to Python**: Python is a high-level, interpreted programming language known for its
simplicity and readability. It supports multiple programming paradigms and has a vast standard
library.
2. **Use IDE to Develop Programs**: IDEs like PyCharm, VS Code, Thonny provide an environment
to write, run, and debug Python code efficiently with features like syntax highlighting and
auto-completion.
Example:
```python
print("Hello, Python!")
```
```python
x = 10 # int
y = 3.14 # float
```
```python
a=5+3*2 # Output: 11
```
```python
s = "Python"
```
7. **Python Functions**:
```python
def greet(name):
```
8. **Boolean Expressions**:
```python
x = 10 > 5 # True
```
9. **Selection Structure**:
```python
if x > 0:
print("Positive")
```
```python
for i in range(5):
print(i)
```
```python
fruits.append("orange")
```
```python
```
13. **Tuples**:
```python
t = (1, 2, 3)
```
```python
import datetime
print(datetime.datetime.now())
```
15. **Dictionaries**:
```python
```
UNIT II
**UNIT II: Classes in Python**
```python
class Student:
self.name = name
s = Student("KL")
```
4. **Data Hiding**:
```python
self.__private = "hidden"
```
5. **Instance Methods**:
```python
def show(self):
print(self.name)
```
8. **Inheritance**:
```python
class Teacher(Person):
pass
```
```python
class MyError(Exception):
pass
```
11. **Iterators**:
```python
it = iter([1, 2, 3])
print(next(it))
```
12. **Generators**:
```python
def gen():
yield 1
```
13. **Decorators**:
```python
def decorator(func):
def wrapper():
print("Before")
func()
print("After")
return wrapper
```
UNIT III
**UNIT III: I/O and Error Handling**
2. **File Handling**:
- Writing:
```python
f.write("Hello")
```
- Reading:
```python
print(f.read())
```
4. **Error Handling**:
```python
try:
x=1/0
except ZeroDivisionError:
```
6. **Multiple Exceptions**:
```python
try:
# code
pass
```
7. **Working with Directories**:
```python
import os
os.mkdir("new_folder")
```
UNIT IV
**UNIT IV: Relational Databases and GUI**
1. **SQL Statements**:
```python
import sqlite3
conn = sqlite3.connect("data.db")
c = conn.cursor()
conn.commit()
```
```python
def click():
print("Clicked")
```
UNIT V
**UNIT V: Machine Learning Tools in Python**
1. **Numpy**:
```python
import numpy as np
```
2. **Pandas**:
```python
import pandas as pd
```
3. **Matplotlib**:
```python
plt.show()
```
4. **Seaborn**:
```python
import seaborn as sns
sns.histplot([1,2,3,4])
```
5. **Scikit-learn**:
```python
```