class Person:
def __init__(self, _name, _age):
self.name = _name
self.age = _age
def sayHi(self):
print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
p1 = Person('Bob', 25)
p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
class ClassName(object): #"(object)" isn't mandatory unless this class inherit from another
def __init__(self, var1=0, var2):
#the name of the construct must be "__init__" or it won't work
#the arguments "self" is mandatory but you can add more if you want
self.age = var1
self.name = var2
#the construct will be execute when you declare an instance of this class
def otherFunction(self):
#the other one work like any basic fonction but in every methods,
#the first argument (here "self") return to the class in which you are
# plz suscribe to my youtube channel -->
# https://www.youtube.com/channel/UC-sfqidn2fKZslHWnm5qe-A
class Fan:
def __init__(self, company, color, number_of_wings):
self.company = company
self.color = color
self.number_of_wings = number_of_wings
def PrintDetails(self):
print('This is the brand of',self.company,'its color is', self.color,' and it has',self.number_of_wings,'petals')
def switch_on(self):
print("fan started")
def switch_off(self):
print("fan stopped")
def speed_up(self):
print("speed increased by 1 unit")
def speed_down(self):
print("speed decreased by 1 unit")
usha_fan = Fan('usha','skin',5)
fan = Fan('bajaj','wite', 4)
print('these are the details of this fan')
usha_fan.PrintDetails()
print()
usha_fan.switch_on()
A class is a block of code that holds various functions. Because they
are located inside a class they are named methods but mean the samne
thing. In addition variables that are stored inside a class are named
attributes. The point of a class is to call the class later allowing you
to access as many functions or (methods) as you would like with the same
class name. These methods are grouped together under one class name due
to them working in association with eachother in some way.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_name(self):
print(f"Hello, I am {self.name}")
p1 = Person("Sara", 18)
p1.say_name()
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
# Node class
class Node:
# Function to initialize the node object
def __init__(self, data):
self.data = data # Assign data
self.next = None # Initialize
# next as null
# Linked List class
class LinkedList:
# Function to initialize the Linked
# List object
def __init__(self):
self.head = None
class Foo:
def __init__(self):
self.definition = Foo!
def hi():
# Some other code here :)
# Classes require an __init__ if you want to assign attributes. (self) defines what describes the attribs.
class IntellipaatClass:
a = 5
def function1(self):
print(‘Welcome to Intellipaat’)
#accessing attributes using the class object of same name
IntellipaatClass.function(1)
print(IntellipaatClass.a)
class awwab(object):
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
print("Hello, my name is",self.name,"and I am",self.age,"years old!")
awwabasad = awwab("Awwab Asad", 11)
print(awwabasad.speak())
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def greet(self, person_to_greet):
# person_to_greet will be another Person object
print(f"Hey {person_to_greet.name}, nice to meet you I'm {self.name}")
def ask_age(self, ask_from):
print(f"{self.name}: {ask_from.name}, How old are you?")
print(f"{ask_from.name}: i am {ask_from.age}")
# Creating a person object
tom = Person("Tom", 50, "Male")
# we can also create an object with keyword arguments
jack = Person(name="Jack", age=19, gender="Male")
# Here we call the greet method of tom, and we pass the Jack Person Object Created above
tom.greet(jack)
# if we call the greet method of jack and pass the Tom person object, then jack greets tom
jack.greet(tom)
# Here Jack will ask age of tom, and tom will reply with his age
jack.ask_age(tom)
>>> print(ObjectCreator) # you can print a class because it's an object
<class '__main__.ObjectCreator'>
>>> def echo(o):
... print(o)
...
>>> echo(ObjectCreator) # you can pass a class as a parameter
<class '__main__.ObjectCreator'>
>>> print(hasattr(ObjectCreator, 'new_attribute'))
False
>>> ObjectCreator.new_attribute = 'foo' # you can add attributes to a class
>>> print(hasattr(ObjectCreator, 'new_attribute'))
True
>>> print(ObjectCreator.new_attribute)
foo
>>> ObjectCreatorMirror = ObjectCreator # you can assign a class to a variable
>>> print(ObjectCreatorMirror.new_attribute)
foo
>>> print(ObjectCreatorMirror())
<__main__.ObjectCreator object at 0x8997b4c>
fromr random import radint
Asnwer= randint(1,200)
count= 0
while true:
guess = getingeger("whatis yourestimate")
if guess< 1 or guess > 200:
print(your estimate must be between 1 and 200)
continue
count+ = 1
if guess < answer:
print("higher")
elif guess > answer:
print("lower")
else:
print("you guessed it!")
if count== 1
print("wow first try!!")
else:
print("you estimated it in, count,'time'")