Loops in Python

Developed with
QA

Contents

keyboard_tab

The course is part of this learning path

Practical Data Science with Python
course-steps
5
certification
4
lab-steps
3
description
4
play-arrow
Start course
Overview
DifficultyAdvanced
Duration49m
Students208
Ratings
4/5
starstarstarstarstar-border

Description

Continuing on from Practical Data Science with Python, this course explores a variety of Python features in a practical, hands-on way. It starts by looking at Python functions and you are given a guided walkthrough of a range of functions relating to data science. It then moves on to dictionaries in Python and how to create them. After that, you'll be guided through flow control in Python, loops, and finally, you'll be shown a technical demonstration in Python looking at classes, variables, and stringification.

Learning Objectives

The main objective of this course is to enhance your knowledge of Python, and learn about Python functions, loops, dictionaries, and flow control.

Intended Audience

This course is intended for IT professionals who already have a good knowledge of Python and who want to enhance that knowledge from a data science perspective.

 

Transcript

Hello and welcome back. So, now I would like us to do an exercise involving looping. So, this is called the FizzBuzz test. It's just a classic programming problem. What it would involve is you defining a loop containing if statements. And based upon the result of these if statements, you will selectively print out the Fizz Buzz or FizzBuzz. This is simply based on divisibility tests.

So you will need something like either an overall for-loop or a while loop to just get your numbers. So to get you started, we can have for number in range 101. If I just print out the numbers, this is a loop that has generated all the numbers for you.

Now, all you have to do is ask questions to the number. How do we ask questions? Well, we use if statements to do that. Remainder is how we can test for divisibilities. So we can use any number, any number, percentage, another number, double equal zero as a test for this number being divisible by this other number here. If this evaluates as true, if six percentage two evaluates true, that means it's divisible. Six percentage two double equal zero, that says that this is divisible by two, otherwise it's not divisible. If that's divisible by three, there you go, it's divisible by three. Is it divisible by four? No, it's not, because the remainder after division is nothing.

So using this, using a loop, using an if l statement, we should be able to have a go at this. If a number is divisible by 15, then that means we're going to print out FizzBuzz. Okay? If you're divisible by three and five, you're going to be divisible by 15. And then we're going to do an elif statement for number, percentage three equal zero. And then we're going to print out Fizz. Otherwise, the number is divisible by five. We're going to print out Buzz, and then finally we have our else, which is going to be printing out the number itself.

We also have a comprehension version of this. So this is the list comprehension version of the FizzBuzz test solution. You don't have to know how this works in its entirety, but we are just again chaining together multiple if l statements. So we're saying we're going to print out FizzBuzz if it's divisible by 15, otherwise we'll print out Fizz if it's divisible by five, otherwise we'll print up Buzz if it's divisible by three. Otherwise we'll print out the number itself, and the only difference is that this is just going to output our values as a list.

Comprehensions are optimized for constructing collections. So if you wanted a list as your output, this would be the best way to do it. Although best is misleading because in Python, the best way to do anything is the way that it's easiest to understand for someone else, right? So, I would argue that it's probably a lot easier to understand this.

You're only saving on collection construction time. You're still asking if else, if else, if else, okay? You're not saving a massive amount of time, but it's just to show you that it can be done. We're saying for each of these guys perform this block of if else.

Now, as we have seen everything in Python that we've been working with is an object. And we are able to define our own object, that is our own things that we want to create. To do this, we have to define something called a class, which is a template describing how an object is to behave, the attributes it has and the things that it can do. So, its attributes, its methods, and what to do whenever it's created.

Let's start simply, I'm going to create a simple class. So, in Data science you will often have classes defining what to do. You will have data processing classes. You will create an instance of a data processing object and your object will then perform all your transformations for you.

Machine learning algorithms in Python are all represented as objects. The internal States, the current value of their attributes is the value of the coefficients after training. So, objects lend themselves to that application. But I want to find a template for creating an object. I've used the class key word, I'm going to create a simple class. I'm going to define some interesting special methods. And then after that I'm going to talk about what these all actually mean.

So, if I have a simple class, I want to initialize self, and I want self to tell you, "I have been created." And I have a method that is just going to print out the value. So, this is a very, very simple class definition. So, if I run this nothing happens and nothing should happen. All that's going to happen is that this thing called a class, this template, is going to get shoved into memory somewhere.

But what I have now is an outline for what a simple object will look like. So, I'm going to create one and then we'll unpick what each of these methods mean. My simple object is just a variable name, and if I have a look at my simple object, what we get told is that I have a simple object at this location in memory. We don't need to care about this bit too much just for now. But what this says is I have a simple object at, and then this is the hexadecimal memory address, just a place where it lives essentially.

Now, this object contains two things. It has a method which prints out the value contained within it, and then I can directly access value. We wouldn't usually be able to directly access value with an object, but we can here.

If I call the print method, what happens is I print out this thing that I've defined and it's just a string that says, "I have been created." so, what is this thing? What have I done and what's going on here? Whenever you see a method or a function that has two underscores either side of it, what that means is that it's special in the sense that it has a specific meaning in Python. The double underscore in its method describes what an object should do when it's created.

Whenever I create a simple object, I'm saying that I want a variable to be defined call the value that contains the string I have been created. A similar thing happens when we just call the interject constructor, for example, we would by default get a value of zero. If I call the string constructor, what I get is an empty string containing nothing. Whereas for my simple object, I'm going to get a value within it that says, "I have been created." But this is just an internal variable.

So we can think of objects as wrappers around collections of things. So the strangest thing, the thing that people struggle with the most when they see classes in Python, is this self. This word self is popping up everywhere. The best way to think of self is simply as a pointer. It says that whatever an object is created initialize that specific object by creating a variable core value specific to the current object containing, for example, in this case, I have been created.

Whenever I've used the print method, I'm specifically printing out the value contained within this object, not any other simple objects in the world. Like when I call the UpperCase method, I don't want to turn every string in the world to uppercase. I want to turn this string right here, right now, to uppercase. That's what self means. It just says, "Do this for me." You can have, or you can actually name it to anything you'd like. Self is just again a very strong Python convention.

So the only way that my object actually knows what value is, is by calling itself .value. If I say value, I'm hoping I get told off by doing something like this. Yes, I got told off because I get told, "The simple has no object called value" because I'm not pointing to the specific value for this specific object that I have right here, which I do with self. It's going to go looking for a value somewhere else and it's not going to find it. So I can create as many of these as I like. I can create another which is going to be given by simple.

So, I now have two simple objects. If I call another .print method, then I can print out the current state of the internal value now. But what we should be able to see is that if I take another's value and then I reassign it, so now it's equal to two, I have been altered. We should see that when I actually print out, use the print method, which goes to value and prints out of the current state value, I've changed it, I've changed the internal variable.

So, these are two completely different objects. The method remains the same for both of them. I'm updating the internal state by changing the value of value. Remember that Python convention is for classes to have capital letters at the start.

Another thing to remember is dir, dir on something like the string class gives me a list of everything that strings can do. Strings have an init method. This tells a string what to do whenever it's created. I could call dir on simple, I would get distinctly less, but I've got all of these. These things have actually been defined without my knowing about it. In the background, all of this has been defined. So the specific class attributes, the str attribute of all these guys have been defined behind the scenes.

About the Author

Delivering training and developing courseware for multiple aspects across Data Science curriculum, constantly updating and adapting to new trends and methods.

Covered Topics