Expressions and statements are the building blocks of a Python script. A line of code may an expression, a statement, or both.
- [Instructor] The distinction between statements and expressions is a bit different in every computer language. Generally speaking, a statement is a unit of execution, and an expression is a unit of evaluation. In a scripting language like Python, these definitions require further explanation. In Python, an expression is any combination of literals, identifiers, and operators. Generally, this means anything that returns a value is an expression. This is an assignment. It assigns a value to a variable.
This is an operation. You notice it has an operator, the multiplication symbol, and so it returns the result of that operation, the result of that multiplication. This is an aggregate value. In this case, it's a tuple. This is a simple value. This is a built-in constant value. And this is a function call. All function calls return a value in Python, even if the value is none, which is the absence of value. A statement is a line of code.
It may be an expression, or it may be something like the import statement or a break or a continue. Here in Komodo, I've opened a working copy of statements.py from chapter two of the exercise files. We'll go ahead and run this, so you can see what it does. It simply prints out, This is Python version and the version number. In Python, a statement is a line of code. It does not require a semicolon at the end of the line. And so this import statement is a statement. This assignment, even though it's also an expression, is a statement because it's a line of code by itself.
And then this print function, of course, is also a statement, and it's also an expression because, even though we're not using the returned value from the function call, it still does return a value. Now, in most cases, you'll see one statement per line. So if I add another line of code here, and save that and run it, you can see in the output, we now have two print results. And so one statement per line is normally how you will see Python code. Python does not require a semicolon at the end of the line, although you may put more than one statement on a line.
If I put a semicolon here and put that other print statement on the same line, I'll run it, you see we get the same result. This is possible, and it's extremely rare. You almost never see this in Python. You'll far more often see one statement per line, like this. So statements and expressions are simple concepts in Python, and understanding their distinctions will help you write code that is clear and easy to understand.
- 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
Learning Python Web Penetration Testingwith Christian Martorella2h 49m 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.