• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

init in Python

October 25, 2021 Python

In this post, you will learn how to work with init so that you can use that same knowledge and thus evolve.

  • init is basically a constructor and its function is to initialize everything that is necessary when creating a certain object.
class Person:
  def __init__(self, firstName, lastName, age):
    self.firstName = firstName
    self.lastName = lastName
    self.age = age

  def information(self):
    print('Name:', self.firstName, self.lastName)
    print('Age:', self.age)

  def checkEntrance(self):
    if self.age >= 18:
      print('This person can enter the place because he is of legal age.')
    else:
      print('This person cannot enter the site because he is a minor.')

pessoa1 = Person('Nelson', 'Silva', 25)
pessoa2 = Person('Larissa', 'Fernandes', 17)

pessoa1.information()
pessoa1.checkEntrance()

'''
  Name: Nelson Silva
  Age: 25
  This person can enter the place because he is of legal age.
'''

pessoa2.information()
pessoa2.checkEntrance()

'''
  Name: Larissa Fernandes
  Age: 17
  This person cannot enter the site because he is a minor.
'''

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved