1class Person:
2 def __init__(self, _name, _age):
3 self.name = _name
4 self.age = _age
5
6 def sayHi(self):
7 print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
8
9p1 = Person('Bob', 25)
10p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
1class Person:
2 def __init__(self, name, age):
3 self.name = name
4 self.age = age
5 def myfunc(self):
6 print("Hello my name is " + self.name +".")
7
8p1 = Person("Victor", 24)
9p1.myfunc()
1
2class Dog(object):
3 def __init__(self, name, age):
4 self.name = name
5 self.age = age
6
7 def speak(self):
8 print("Hi I'm ", self.name, 'and I am', self.age, 'Years Old')
9
10JUB0T = Dog('JUB0T', 55)
11Friend = Dog('Doge', 10)
12JUB0T.speak()
13Friend.speak()
1# Python classes
2
3class Person():
4 # Class object attributes (attributes that not needed to be mentioned when creating new class of person)
5 alive = True
6
7 def __init__(self, name, age):
8 # In the __init__ method you can make attributes that will be mentioned when creating new class of person
9 self.name = name
10 self.age = age
11
12 def speak(self):
13 # In every method in class there will be self, and then other things (name, age, etc.)
14 print(f'Hello, my name is {self.name} and my age is {self.age}') # f'' is type of strings that let you use variable within the string
15
16person_one = Person('Sam', 23) # Sam is the name attribute, and 23 is the age attribute
17person_one.speak() # Prints Hello, my name is Sam and my age is 23
18
19==================================================================
20# Output:
21
22>>> 'Hello, my name is Sam and my age is 23'
1class awwab(object):
2 def __init__(self, name, age):
3 self.name = name
4 self.age = age
5
6 def speak(self):
7 print("Hello, my name is",self.name,"and I am",self.age,"years old!")
8
9awwabasad = awwab("Awwab Asad", 11)
10print(awwabasad.speak())