Unlike many other languages, spaces and indents have meaning in Python. Statements are terminated by a line ending, and comments are introduced with a pound sign.
- [Instructor] Unlike most other modern scripting languages, whitespace is significant in Python. Here in Komodo I've opened a working copy of hello-main.py from chapter two of the exercise files. In Python a block is delimited by indentation. There's no brackets or parentheses to match up just indents. And so these two functions, main and message, they have blocks which are associated with them and that's the code that comprises the body of the function. And you notice that they're indented under the definition of the function, the function declaration, the function definition.
And so the main function has a function call to the message function, and the message function has a function call to the print function. And you can tell that these are associated because of their indentation. Now if I take this message function, well let's go ahead and run this and you can see that it prints this Python version and the version number and so if I take this and I just add another print, let's say print, line two, and I save that and run it you see now we have a line two in our output.
And I can do this again, and make that one line three and save that and run it and now we have three lines of output. And we can tell that these are all part of this same function because they're indented. If I were to un-indent one of these lines and save that and run it now, that line three will come first because it's outside of all of the functions and it's getting executed before main is ever called down there on line 14 and if I re-indent it and save it, now it's part of that function again because it's indented with the rest of the block.
So a block is indicated by the common indention. So these are all indented exactly the same so they're all part of exactly the same block. If I wanted to I could say, if true, and make this part of a separate block, and we'll give it an else, print, not in the block, not in the if, not true, we'll just say not true. If I run this we get the line three, and if I say false and save that and run it now we get the not true.
And so these are part of separate blocks that whole if is part of that other block but these are indented in another level and so they're part of a different block. Comments in Python are introduced by a pound sign. So if I come here and say, this is a comment, of course that's not code and it doesn't run so when I run this we still get the same result. A comment ends at the end of the line, and so there's nothing like a multi-line comment in Python.
If you want to make a multi-line comment you have to do it something like this. So in most languages whitespace is mostly ignored in Python it's meaningful and you must be conscious of formatting as you write your code and in Python, comments are introduced by the pound sign or the hash sign and they end at the end of the line and again that end of the line is significant in Python.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.