Introduction to Python @ RomaTRE 2020

We’re going to introduce you to Python @ Roma TRE 2020 first lesson.Well this course is not a basic course so let us introduce Object Oriented Programming (remember it’s only a really short and rude sketch but also if we’re not going to create python classes in our lessons the bricks on which Python is based is the OOP).

What has to do a software program?

Of course in any field we’ve to work with data that are the main part of our work. Like you may see in this beatiful paint we use data for input then we make program to analyze them and at the end we “hope” to produce  “data” results.

The advantage to use OOP is to use a really general approach to our data so that we don’t need to go into specific description of them and allows us to think a complex software in terms of real world objects and their relationships to one another.

Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behaviour.

OOP focuses on the objects that developers want to manipulate rather than the logic required to manipulate them. This approach to programming is well-suited for programs that are large, complex and actively updated or maintained.The organization of an object-oriented program also makes the method beneficial to collaborative development, where projects are divided into groups.

Additional benefits of OOP include code reusability, scalability and efficiency. Even when using microservices, developers should continue to apply the principles of OOP.

The first step in OOP is to collect all of the objects a programmer wants to manipulate and identify how they relate to each other — an exercise often known as data modeling.

Examples of an object can range from physical entities, such as a human being who is described by properties like name and address, down to small computer programs, such as widgets.

Once an object is known, it is labeled with a class of objects that defines the kind of data it contains and any logic sequences that can manipulate it. Each distinct logic sequence is known as a method. Objects can communicate with well-defined interfaces called messages.

Principles of OOP

Object-oriented programming is based on the following principles:

  • Encapsulation. The implementation and state of each object are privately held inside a defined boundary, or class. Other objects do not have access to this class or the authority to make changes but are only able to call a list of public functions, or methods. This characteristic of data hiding provides greater program security and avoids unintended data corruption.
  • Abstraction. Objects only reveal internal mechanisms that are relevant for the use of other objects, hiding any unnecessary implementation code. This concept helps developers more easily make changes and additions over time.
  • Inheritance. Relationships and subclasses between objects can be assigned, allowing developers to reuse a common logic while still maintaining a unique hierarchy. This property of OOP forces a more thorough data analysis, reduces development time and ensures a higher level of accuracy.
  • Polymorphism. Objects can take on more than one form depending on the context. The program will determine which meaning or usage is necessary for each execution of that object, cutting down the need to duplicate code.


Classes in Python

Focusing first on the data, each thing or object is an instance of some class.

The primitive data structures available in Python, like numbers, strings, and lists are designed to represent simple things like the cost of something, the name of a poem, and your favorite colors, respectively.

What if you wanted to represent something much more complicated?

For example, let’s say you wanted to track a number of different animals. If you used a list, the first element could be the animal’s name while the second element could represent its age.

How would you know which element is supposed to be which? What if you had 100 different animals? Are you certain each animal has both a name and an age, and so forth? What if you wanted to add other properties to these animals? This lacks organization, and it’s the exact need for classes.

Classes are used to create new user-defined data structures that contain arbitrary information about something. In the case of an animal, we could create an Animal() class to track properties about the Animal like the name and age.

It’s important to note that a class just provides structure—it’s a blueprint for how something should be defined, but it doesn’t actually provide any real content itself. The Animal() class may specify that the name and age are necessary for defining an animal, but it will not actually state what a specific animal’s name or age is.

It may help to think of a class as an idea for how something should be defined.


Python Objects (Instances)

While the class is the blueprint, an instance is a copy of the class with actual values, literally an object belonging to a specific class. It’s not an idea anymore; it’s an actual animal, like a dog named Roger who’s eight years old.

Put another way, a class is like a form or questionnaire. It defines the needed information. After you fill out the form, your specific copy is an instance of the class; it contains actual information relevant to you.

You can fill out multiple copies to create many different instances, but without the form as a guide, you would be lost, not knowing what information is required. Thus, before you can create individual instances of an object, we must first specify what is needed by defining a class.


How To Define a Class in Python

Defining a class is simple in Python:

class Dog:

You start with the class keyword to indicate that you are creating a class, then you add the name of the class (using CamelCase notation, starting with a capital letter.)

Also, we used the Python keyword pass here. This is very often used as a place holder where code will eventually go. It allows us to run this code without throwing an error.


Instance Attributes

All classes create objects, and all objects contain characteristics called attributes (referred to as properties in the opening paragraph). Use the __init__() method to initialize (e.g., specify) an object’s initial attributes by giving them their default value (or state). This method must have at least one argument as well as the self variable, which refers to the object itself (e.g., Dog).

class Dog:

    # Initializer / Instance Attributes
    def __init__(self, name, age): = name
        self.age = age

In the case of our Dog() class, each dog has a specific name and age, which is obviously important to know for when you start actually creating different dogs. Remember: the class is just for defining the Dog, not actually creating instances of individual dogs with specific names and ages; we’ll get to that shortly.

Similarly, the self variable is also an instance of the class. Since instances of a class have varying values we could state = name rather than = name. But since not all dogs share the same name, we need to be able to assign different values to different instances. Hence the need for the special self variable, which will help to keep track of individual instances of each class.

NOTE: You will never have to call the __init__() method; it gets called automatically when you create a new ‘Dog’ instance.

So while each dog has a unique name and age, every dog will be a mammal.


Instance Methods

Instance methods are defined inside a class and are used to get the
contents of an instance. They can also be used to perform operations
with the attributes of our objects. Like the __init__ method, the first argument is always self:

class Dog:

    # Class Attribute
    species = 'mammal'

    # Initializer / Instance Attributes
    def __init__(self, name, age): = name
        self.age = age

    # instance method
    def description(self):
        return "{} is {} years old".format(, self.age)

    # instance method
    def speak(self, sound):
        return "{} says {}".format(, sound)

# Instantiate the Dog object
mikey = Dog("Mikey", 6)

# call our instance methods
print(mikey.speak("Gruff Gruff"))

In the latter method, speak(), we are defining behavior. What other behaviors could you assign to a dog?

This is not our goal here these few words have only the goal to make a reminder to OOP.


Now to prepare you to what is the core of Machine Learning and Data Science let us to show a nice video 4 mins long

Now 5 mins of break and we’ll go on more technical part of the course.