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

Python blocks

Hello guys,

what does the teacher means when he says that the ,, : '' means that a new colon starts. I don't get it what a colon and blocks are. And i also don't understand what objects are and what a object oriented programming language does? Could someone take a little bit time and explain me all the stuff, cause internet does't help me like you guys.

I appreciate the help.

Thank you a lot.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

In Python, a block is a section of code indented to the same level. Indenting a line of code further than the one above it starts a new block while indenting the line of code less closes the block or blocks above it. All commands that expect an indented block of code to follow, such as, if, while, try, etc., all end in a colon to signify an indented block should follow.

A valid question is if the indentation signifies a block of code, why is the colon needed at all? Though not standard practice, it is syntactically allowed to have some commands with blocks written all on a single line. Such as:

# compressed syntax
if count > 0: count -= 1
item = []  # closes previous block

# preferred syntax
if count > 0:
    count -= 1
item = []  # closes previous block

About objects and object oriented programming (OOP), it a much larger question. It may suffice to say that in OOP, every item from numbers, to strings, to complex classes, are all objects. An object is defined as a class that includes attributes (data contained in the object) and methods (included functions that know how to properly treat the attributes. For example:

# a list is an object
>> mylist = []
>>> type(mylist)
<class 'list'>
# which includes pre-defined methods
>>> dir(mylist)
['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
# each of the above methods operate on the data contained in the list to provide special
# functionality so the user does have to recreate it

There are more built-in methods to classes that are typically used by Python itself and are rarely called directly by the programmer. These methods have a double-underscore before and after the name (also called "dunder" methods). For example, the __init__() method is automatically called to initialize a class when it is instantiated (used to create a new object instance). You will get more into dunder methods later on in the Treehouse course work.

The is much more to know about OOP, but give it time to sink in as you continue with the lessons. Of course, if you have specific questions about an OOP topic, please return to the forum and post a question! Good luck!!