Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Whats the difference between a loop, function and a class?

Functions are often called 'sub programs' in Python, why is that? Because functions often take some input, process that information and optionally outputs the processed information?

However doesn't a loop do the same thing as a function? E.g Input, process and output some data... with the difference being that loops can not be 'reused'? Whats the difference between functions and loops?

And what is a class and how is that different to functions?

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Wow this is a rather big question, but I like it! It's one of those concept type questions. So I'll try and make it brief and a general overview, although I can guarantee you that if you continue down the Python path (or really most any of the other programming courses here) you will learn a lot about all three.

Ok so you can think of a class as a sort of "cookie cutter". Imagine for example that you have a house. Every house has a roof, a color, an address, a number of doors, number of windows, dimensions etc. Every time you make a new instance of that class (or an object as it's called) you create a new house with a roof, a color(maybe this one is pink but the previous house was blue), a number of doors, number of windows, and new dimensions. Now besides having properties (the variables stored inside the class) they might also have methods. Methods are functions specifically associated with a class. You might have a method named clean and every time you run that method on any instance of a house, someone comes in and cleans the house. Boy, I'd love that method on mine! :smiley:

Now for functions: a function is a piece of code you can run over and over again simply by calling it. All methods are functions, but not all functions are methods. You can have a function that isn't tied to a class and is sort of free-floating in your code and will execute every time you call it. But all functions defined within a class are called methods.

Loops: A loop is a piece of code that runs over and over again until the condition is false. Or in the case of a for in loop until the number of items to iterate over has reached an end. A loop can be just about anywhere in your code. It can even be inside a function or method.

Hope this helps, but let me know if anything is unclear! :sparkles:

Hi Jennifer, am i adding comments correctly? When i add a new 'post' (e.g a new answer?) how do other users see that i might have added another question in that 'post'? I guess that what i am getting at is if it is recommended to create a new 'discussion' every time i have a question?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Kevin Ohlsson you should absolutely make a new question every time you have a new, unique question. This has a few benefits. First, it keeps the boards organized and secondly you might have a really brilliant question that someone gives an amazing answer to, but if it's located under a different question other students will likely never read down far enough to even read it. And it's a shame to waste a learning opportunity :smiley:

Great, thanks Jennifer, i'll create a new question then! Thanks for the super fast response! :)

This helps! Thank you!

THANK YOU Jennifer!!

Very clear examples! :sparkles:

Hi again,

As a follow-up to my previous question.

I've heard that everything is an object in Python, does that mean that everything in python is a part of a class, and that 'python' itself is made up of a different classes? Is a group of classes called a library?