6  Introduction to Python

In this chapter we’ll cover some of the basics of Python. This will include using Python as a calculator, variables in Python, loops, conditionals, and functions. This will help form a basic understanding of the fundamentals of programming with Python’s syntax

6.1 Variables and Assignments

6.2 Loops in Python

6.3 Conditionals

6.4 Functions

6.5 Classes in Python

A Class in Python acts as a “blueprint” for creating objects. They are extremely useful for the reusability and organization of code.

An instance is an object that belongs to a class.

Let’s consider the following example

Code
# Create a class Square
class Square():
    
    def __init__(self, length):
        self.length = length

    def area(self):
        return self.length*self.length

    def perimiter(self):
        return self.length*4

In this case we have created a class

Code
square1 = Square(23)
square2 = Square(23.123)
Code
square1.length
23
Code
square1.perimiter()
92
Code
square1.area()
529
2+2