0% found this document useful (0 votes)
19 views10 pages

Python Notes With Answers

The document provides a comprehensive introduction to Python, covering its basic syntax, data types, functions, and object-oriented programming concepts. It also includes sections on file handling, error management, relational databases, GUI development with Tkinter, and machine learning tools such as Numpy, Pandas, and Scikit-learn. Each section is illustrated with code examples to facilitate understanding.

Uploaded by

deepakkumarsaw13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views10 pages

Python Notes With Answers

The document provides a comprehensive introduction to Python, covering its basic syntax, data types, functions, and object-oriented programming concepts. It also includes sections on file handling, error management, relational databases, GUI development with Tkinter, and machine learning tools such as Numpy, Pandas, and Scikit-learn. Each section is illustrated with code examples to facilitate understanding.

Uploaded by

deepakkumarsaw13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT I

**UNIT I: Introduction to Python**

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.

3. **Basic Coding Skills**:

Example:

```python

print("Hello, Python!")

```

4. **Data Types and Variables**:

```python

x = 10 # int

y = 3.14 # float

name = "KL" # str

```

5. **Working with Numeric Data**:

```python

a=5+3*2 # Output: 11
```

6. **Working with String Data**:

```python

s = "Python"

print(s.upper()) # Output: PYTHON

```

7. **Python Functions**:

```python

def greet(name):

return "Hello " + name

```

8. **Boolean Expressions**:

```python

x = 10 > 5 # True

```

9. **Selection Structure**:

```python

if x > 0:

print("Positive")

```

10. **Iteration Structure**:

```python
for i in range(5):

print(i)

```

11. **Working with Lists**:

```python

fruits = ["apple", "banana"]

fruits.append("orange")

```

12. **List of Lists**:

```python

matrix = [[1, 2], [3, 4]]

```

13. **Tuples**:

```python

t = (1, 2, 3)

```

14. **Dates and Times**:

```python

import datetime

print(datetime.datetime.now())

```

15. **Dictionaries**:
```python

student = {"name": "KL", "age": 20}

```

UNIT II
**UNIT II: Classes in Python**

1. **OOP Concepts**: Encapsulation, Inheritance, Polymorphism, Abstraction.

2. **Creating Classes and Objects**:

```python

class Student:

def __init__(self, name):

self.name = name

s = Student("KL")

```

3. **Constructors**: `__init__` is used for object initialization.

4. **Data Hiding**:

```python

self.__private = "hidden"

```

5. **Instance Methods**:

```python
def show(self):

print(self.name)

```

6. **Special Methods**: `__str__`, `__len__`, etc.

7. **Class Variables**: Shared among all instances.

8. **Inheritance**:

```python

class Teacher(Person):

pass

```

9. **Polymorphism**: Same method behaves differently in different classes.

10. **Custom Exceptions**:

```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**

1. **Data Streams**: Input/Output using streams (files, keyboard, etc.)

2. **File Handling**:

- Writing:

```python

with open("file.txt", "w") as f:

f.write("Hello")
```

- Reading:

```python

with open("file.txt", "r") as f:

print(f.read())

```

3. **Access Modes**: `r`, `w`, `a`, `r+`

4. **Error Handling**:

```python

try:

x=1/0

except ZeroDivisionError:

print("Can't divide by zero")

```

5. **Exception Hierarchy**: BaseException > Exception > ArithmeticError > ZeroDivisionError

6. **Multiple Exceptions**:

```python

try:

# code

except (IOError, ValueError):

pass

```
7. **Working with Directories**:

```python

import os

os.mkdir("new_folder")

```

UNIT IV
**UNIT IV: Relational Databases and GUI**

1. **SQL Statements**:

- SELECT, INSERT, UPDATE, DELETE

2. **SQLite with Python**:

```python

import sqlite3

conn = sqlite3.connect("data.db")

c = conn.cursor()

c.execute("CREATE TABLE users (id INT, name TEXT)")

conn.commit()

```

3. **GUI and Events** (Using Tkinter):

```python

from tkinter import *

def click():

print("Clicked")

btn = Button(text="Click", command=click)


btn.pack()

```

UNIT V
**UNIT V: Machine Learning Tools in Python**

1. **Numpy**:

```python

import numpy as np

arr = np.array([1, 2, 3])

```

2. **Pandas**:

```python

import pandas as pd

df = pd.DataFrame({"Name": ["A", "B"], "Age": [20, 21]})

```

3. **Matplotlib**:

```python

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])

plt.show()

```

4. **Seaborn**:

```python
import seaborn as sns

sns.histplot([1,2,3,4])

```

5. **Scikit-learn**:

```python

from sklearn.linear_model import LinearRegression

```

You might also like