KEMBAR78
Exp 6 Code | PDF
0% found this document useful (0 votes)
9 views5 pages

Exp 6 Code

The document outlines a Python program that defines a 'Student' class with class variables for branch and semester, and instance variables for name, age, and marks. It demonstrates how to create an instance of the class, display the variables, and introduces mutator and accessor methods for setting and retrieving instance data. The program includes user input for creating a student instance and displaying the information using instance methods.

Uploaded by

unicornpeppy
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)
9 views5 pages

Exp 6 Code

The document outlines a Python program that defines a 'Student' class with class variables for branch and semester, and instance variables for name, age, and marks. It demonstrates how to create an instance of the class, display the variables, and introduces mutator and accessor methods for setting and retrieving instance data. The program includes user input for creating a student instance and displaying the information using instance methods.

Uploaded by

unicornpeppy
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/ 5

'''1 WAP to to define a class-student and assign following variables to it-

Class/Sta c variables- Branch, Sem Instance variables- Name, Age,

Marks Also show how to access & display all the variables.'''

class Student:

branch="Computer engineering"

semester="Sem 2"

def __init__(self,name,age,marks):

self.name=name

self.age=age

self.marks=marks

name=input("Enter name:")

age=int(input("Enter age:"))

marks=int(input("Enter marks:"))

s1=Student(name,age,marks)

print(f"Branch:{s1.branch}")

print(f"Semester:{s1.semester}")

print(f"Name:{s1.name}")

print(f"Age:{s1.age}")

print(f"Marks:{s1.marks}")

#2 Introduce instance methods in the above code to display all the variables.

class Student:

branch="Computer engineering"

semester="Sem 2"

def __init__(self,name,age,marks):
self.name=name

self.age=age

self.marks=marks

def display(self):

print(f"Branch:{s1.branch}")

print(f"Semester:{s1.semester}")

print(f"Name:{s1.name}")

print(f"Age:{s1.age}")

print(f"Marks:{s1.marks}")

name=input("Enter name:")

age=int(input("Enter age:"))

marks=int(input("Enter marks:"))

s1=Student(name,age,marks)

s1.display()

#3 WAP to store data into instances using mutator methods and to retrieve data from the instances
using accessor methods

class Student:

branch = "Computer engineering"

semester = "Sem 2"

def __init__(self, name="", age=0, marks=0):

self.name = name

self.age = age

self.marks = marks

# Mutator methods (se ers)

def set_name(self, name):


self.name = name

def set_age(self, age):

self.age = age

def set_marks(self, marks):

self.marks = marks

# Accessor methods (ge ers)

def get_name(self):

return self.name

def get_age(self):

return self.age

def get_marks(self):

return self.marks

display(self):

print(f"Branch: {Student.branch}")

print(f"Semester: {Student.semester}")

print(f"Name: {self.name}")

print(f"Age: {self.age}")

print(f"Marks: {self.marks}")

name = input("Enter name: ")

age = int(input("Enter age: "))

marks = int(input("Enter marks: "))

s1 = Student()

s1.set_name(name)

s1.set_age(age)

s1.set_marks(marks)

s1.display()
OUTPUT

You might also like