Unit 5 - Lesson 4

53.1.1. Methods - An overview

(a) Methods are a set of statements that are called to perform a specific task.
(b) Methods are used in OOPS.

53.1.2. Writing a program with methods

class Person:

	#fill in the missing code
	def setName(self,name):
		self.name = name
	def getName(self):
		return(self.name)

p1 = Person()
p2 = Person()
name1 = input('p1 name: ')
name2 = input('p2 name: ')

p1.setName(name1)
p2.setName(name2)

print("p1 name:",p1.getName())
print("p2 name:",p2.getName())

53.1.3. Python's way of method overloading

class Greeting:
	def sayHello(self, name=None, wish=None):
		if wish == None:
			print("Hello"+name)
		else:
			print("Hello"+name+wish)

#write your code here....

a = input()
b = input()
greet = Greeting()
greet.sayHello(a)
greet.sayHello(a,b)

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.