Book Image

Learn Python in 7 Days

Book Image

Learn Python in 7 Days

Overview of this book

Python is a great language to get started in the world of programming and application development. This book will help you to take your skills to the next level having a good knowledge of the fundamentals of Python. We begin with the absolute foundation, covering the basic syntax, type variables and operators. We'll then move on to concepts like statements, arrays, operators, string processing and I/O handling. You’ll be able to learn how to operate tuples and understand the functions and methods of lists. We’ll help you develop a deep understanding of list and tuples and learn python dictionary. As you progress through the book, you’ll learn about function parameters and how to use control statements with the loop. You’ll further learn how to create modules and packages, storing of data as well as handling errors. We later dive into advanced level concepts such as Python collections and how to use class, methods, objects in python. By the end of this book, you will be able to take your skills to the next level having a good knowledge of the fundamentals of Python.
Table of Contents (18 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
2
Type Variables and Operators

The __init__ method


The __init__() method must begin and end with two consecutive underscores. Here __init__ works as the class's constructor. When a user instantiates the class, it runs automatically. Let's see and understand this concept with the help of code. Here we will write the full code for classinit.py and then we will understand it line by line:

class Leapx_org():
  def __init__(self,first,last,pay):
    self.f_name = first
    self.l_name = last
    self.pay_amt = pay 
    self.full_name = first+" "+last
L_obj1 = Leapx_org('mohit', 'RAJ', 60000)
L_obj2 = Leapx_org('Ravender', 'Dahiya',70000) 
print L_obj1.full_name
print L_obj2.full_name

So, from preceding code, it seems difficult; let's understand it by line by line. The first line defines a class as we already know. When we create the __init__(self,first,last, pay) method inside the class then first argument, self, of __init__() method receives the instance of the class automatically. By convention we call it self, you can use...