Object oriented programming system(oops):
-->Python is a multi-paradigm language.
-->In Python object-oriented Programming (OOPs) is a programming paradigm that uses objects and classes in programming.
Class
-->Template or blueprint of an idea(Logical entity).
-->Class is collection of objects.
Example: Bike
object
-->Object is a physical or real-time or real world entity.
--> It contains states(attributes) and behaviour(methods).
--> Object is representation/instance of class.
Example: Activa,pulsar
Note:we cannot create object without class.But class can be present without objects.
Example for state & behaviour-Bike
- State--> brand, color, and speed
- Behaviour--> start, accelerate, and stop
Why we use oops in python?
- Code Reusability
- Scalability and Maintainability
from PIL import Image
photo = Image.open("abcd.jpeg")
photo.show()
Output:
CSV-Matplotlib:
CSV File:
-->Comma seperated value files.
-->It is a plain text format with series of values seperated by commas.
-->It stores all lines and fields in rows and columns
-->It can be opened with any text editor in windows.
Matplotlib:
Matplotlib is a popular data visualization library in Python that allows users to create static, animated, and interactive plots.
Example:
import matplotlib.pyplot as plt
import csv
years = []
sales = []
with open("sales.csv","r") as f:
reader = csv.reader(f)
next(reader)
for each_row in reader:
years.append(int(each_row[0]))
sales.append(int(each_row[1]))
print(years)
print(sales)
plt.figure(figsize =(7,5))
plt.plot(years, sales, color="r", label=("Yearly Sales"))
plt.xlabel('Years')
plt.ylabel("Sales")
plt.title("Last 5 Years Sales ")
plt.show()
Output:
[2020, 2021, 2022, 2023, 2024]
[10000, 8000, 9500, 10500, 12000]
Modes to open csv files:
--> ‘r’ – to read an existing file,
--> ‘w’ – to create a new file if the given file doesn’t exist and write to it,
--> ‘a’ – to append to existing file content,
--> ‘+’ – to create a new file for reading and writing
Exercises:
1.
f = open("abcd.txt","w")
print(type(f))
print(f.name)
print(f.mode)
print(f.readable())
print(f.writable())
print(f.closed)
Output:
<class '_io.TextIOWrapper'>
abcd.txt
w
False
True
False
2.
f = open("abcd.txt","w")
f.write("Friday")
f.write("Saturday")
f.close()
Output:
FridaySaturday
3.
f = open("abcd.txt","w")
f.write("Friday")
f.write("Saturday")
f.close()
Output:
sundaymonday
In write mode(w) if file already exist then text will be replaced with newly entered text.
4.
f = open("abcd.txt","a")
f.write("tuesday")
f.write("wednesday")
f.close()
Output:
sundaymondaytuesdaywednesday
In append mode(a) if file already exist then text will be added to the last of existing text.
The close() method is used to close an open file in Python. Closing a file is important because it:
- Frees up system resources
- Ensures all data is written (for writing files)
- Prevents errors from accessing a closed file
5.
f = open("abcd.txt","r")
data = f.read()
print(data)
f.close()
Output:
sundaymondaytuesdaywednesdayjanfebmar
6.
f = open("abcd.txt","r")
data = f.read(5)
print(data)
f.close()
Output:
sunda
7.
f = open("abcd.txt","r")
data = f.readline()
print(data)
f.close()
Output:
sundaymondaytuesdaywednesdayjanfebmar
readline() method is used to read a single line from a file.
8.
f = open("abcd.txt","r")
data = f.readlines()
for every_line in data:
print(every_line, end='')
f.close()
Output:
Thursday
Friday
Saturday
--> The readlines() method is used to read all lines from a file and return them as a list of strings.
--> Each line in the file becomes an item in the list.
9) Find no.of.lines, no.of words and no.of letters in text file:
SundayMondayTuesdayWednesdayjanfebmar
guru
prasanna
lakshmi
pritha
f = open("abcd.txt","r")
data = f.readlines()
no_of_words = 0
no_of_letters = 0
for every_line in data:
no_of_lines = len(data)
words = every_line.split()
no_of_words += len(words)
for letter in words:
no_of_letters+= len(letter)
f.close()
print("Number of Lines:", no_of_lines)
print("Number of Words:", no_of_words)
print("Number of Letters:", no_of_letters)
Output:
Number of Lines: 5
Number of Words: 5
Number of Letters: 62
10) To find whether file is available in the given location or not:
import os
file_name = input("Enter file Name: ")
if os.path.isfile(file_name):
print("File is present")
f = open(file_name, "r")
else:
print("File is not present")
Output:
Enter file Name: user.py
File is present
Top comments (0)