DEV Community

Guru prasanna
Guru prasanna

Posted on

Python Day-32 Object oriented programming(oops), CSV, Matplotlib

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?

  1. Code Reusability
  2. Scalability and Maintainability
from PIL import Image
photo = Image.open("abcd.jpeg")
photo.show()
Enter fullscreen mode Exit fullscreen mode

Output:

Image description

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()
Enter fullscreen mode Exit fullscreen mode

Output:

[2020, 2021, 2022, 2023, 2024]
[10000, 8000, 9500, 10500, 12000]
Enter fullscreen mode Exit fullscreen mode

Image description

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)
Enter fullscreen mode Exit fullscreen mode

Output:

<class '_io.TextIOWrapper'>
abcd.txt
w
False
True
False
Enter fullscreen mode Exit fullscreen mode

2.

f = open("abcd.txt","w")
f.write("Friday")
f.write("Saturday")
f.close()
Enter fullscreen mode Exit fullscreen mode

Output:

FridaySaturday
Enter fullscreen mode Exit fullscreen mode

3.

f = open("abcd.txt","w")
f.write("Friday")
f.write("Saturday")
f.close()
Enter fullscreen mode Exit fullscreen mode

Output:

sundaymonday
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

Output:
sundaymondaytuesdaywednesdayjanfebmar

6.

f = open("abcd.txt","r")
data = f.read(5)
print(data)
f.close()
Enter fullscreen mode Exit fullscreen mode

Output:
sunda

7.

f = open("abcd.txt","r")
data = f.readline()
print(data)
f.close()
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

Output:

Thursday
Friday
Saturday
Enter fullscreen mode Exit fullscreen mode

--> 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
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode

Output:

Number of Lines: 5
Number of Words: 5
Number of Letters: 62
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Output:

Enter file Name: user.py
File is present
Enter fullscreen mode Exit fullscreen mode

Top comments (0)