Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions classes_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,59 @@
# the properies defined in the class are called attributes and the functions are called methods





class Human:
def __init__(self,name,age,gender): ## the init function is executed whenever a class is initiated // we can use this function to pass parameters to the object when it is created
## this func is also called a constructor
self.name = name
self.age = age
self.gender = gender

def greet(self): ## self refers to the name of the object that is being creatd using this class
print("Hello I am " + self.name)

def say_hi(self):

print("HI EVERYBODY !!")


class Robot:
def introduce_self(self):
print("my name is ",self.name) ##
print("my weight is " ,self.weight)
print("my address is ", self.addr)

class Animal:
def __init__(self):
self.name = input("enter the name of the animal :")
self.age = int(input("enter the age of the animal :"))
self.habitat = input("enter the habitat of this animal :")
self.owner = input("enter the name of owner :")
self.care_taker_robo = r1

def info(self):
print("I am a ",self.name)
print("my age is ",self.age)
print("my owner is :",self.owner)

H1 = Human("David",32,"male")
print(H1.name)
print(H1.age)
print(H1.gender)
H1.greet()



r1 = Robot()
r1.name = "ROBO1"
r1.weight = 30
r1.addr = "USA"
r1.introduce_self()



Human1 = Human("David",32,"male")

print(Human1.name)
print(Human1.age)
print(Human1.gender)
Human1.greet()
Human1.say_hi()
a1 = Animal()
a1.info()
a1.care_taker_robo.introduce_self()

56 changes: 55 additions & 1 deletion file_handling_4.py
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
##
## updating in a binary file
#
# tell() and seek()
#
import pickle
from sys import flags

infile = open("trial.txt",'r')

print("initially the file_pointer is at = ",infile.tell())
print("reading 4 bytes : ",infile.read(4))
print("now the file pointer is art :",infile.tell())

## the tell function gives the current position of the file pointer
print("now printing results of the seek function")

infile.seek(10) ## default mode , puts the file pointer to the
# 10th byte from the beginning

infile.seek(5,1) ## mode 1 , puts the file pointer to the 5th byte(forward direction) from
# the current file pointer position

infile.seek(-10,2) ## mode 2 , puts the file pointer to the 10th byte backward from the
# end of file

#######################################################################
#### UPDATING THE RECORDS OF A FILE AT A PARTICULAR POSITION ###
#######################################################################

## read the file student_new.dat and give 10 marks bonus to students who have scored more that 550
##

upfile = open("student_new.dat","rb+")
stu = {} ## crating a empty file to store the records in the file
found = False

try:
while True:
rpos = upfile.tell()
stu = pickle.load(upfile)

if stu['Marks'] > 550:
stu['Marks'] += 10
upfile.seek(rpos)
pickle.dump(stu,upfile)
found = True

except EOFError:
if found == False:
print("no such records found")
else:
print("record(s) have been updated")

upfile.close()

39 changes: 39 additions & 0 deletions file_handling_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## CSV file manipulation

import csv


infile = open("student.csv","ra+",newline='\n')
infile_1 = open("student.csv","ra+",newline='')

## writing to csv files
## delimeter refers to the separator used in a csv file to separate datas

## we have to create a writer object at first ..... csv.writer(input-file) creates the writer object
## the writer object is required for coverting user entered data into
## delimited strings
## the writer object has many methods like writerow, writerows
## etc....
data_writer = csv.writer(infile_1)
data_writer.writerow(['Rollno','Name','Marks']) ## the coloumn headings are written here
## the above line writes a row with Rollno,Name,Marks written ....that serves as a heading

no_of_students = int(input("Enter the no of students :"))

for i in range(no_of_students):
print("Student record ",i+1)
rollno = int(input("Enter the roll number of the student :"))
name = input("Enter the name of the student :")
marks = int(input("Enter the marks obtained by the student :"))
student_record = [rollno,name,marks]
data_writer.writerow(student_record)


##

infile.close()
infile_1.close()




3 changes: 3 additions & 0 deletions list_adv.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,6 @@ def menu():
menu()


### to use this as a module in other programs uncomment all the
### lines with menu()

3 changes: 3 additions & 0 deletions student.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rollno,Name,Marks
12,ookla,356
56,hjlls,987
4 changes: 3 additions & 1 deletion student.dat
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
3,sdsd,32
34,david,989
23,dino,455
45,gour,567