Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Statements and Expressions
#1
Understanding Statements and Expressions
After a ton of research, reading and asking around here is my understanding of statements and expressions. I request all the feedback that you can muster.

When I write my source code, I am writing a bunch of instructions for Python to do. If I want Python to save some data to memory there is an instruction for that. If I want Python to run the same block of coding on some data there are instructions for that as well.

You can think of your source code, instructions to Python, as being three different kinds:
  • Statements
  • Expressions
  • Expression Statements


Defining Statements
Every line of code is called a statement. When I write my source code, I write them one line at a time (that will be numbered when using an IDE such as PyCharm). For example:

num_list = [10, 20, 30]  #statement

for num in num_list:  #statement
   print(num)  #statement

5 + 5 #statement

results = 10 > 4   #statement

print("Hello World!") #statement
Each line of code is called a statement no matter what the instruction is. Whether it is an expression statement, a statement with no expression, or a statement with expressions.


Defining Expressions
An expression is any instruction I give to Python that makes Python evaluate some data and return the results of that evaluation. An expressions main job in life is to take some data, evaluate it, and return the results/value. For example:

>>> print("Hello World!") #expression
Hello World!  #results of expression

>>> 5 + 5  #expression
10   #results of expression

<<< 10 < 20   #expression
True  #results of expression
Defining Expression Statements
If an expression is the only instruction on the line of code, it is called an expression statement. For example:

5 + 5  #expression statement

10 > 5  #expression statement

print("Hello World!")  #expression statement[/color]
It is common to see a function call as the only thing on that line of coding. But, you will mainly see 5 + 5 in the console only for learning purposes.


There are three kinds of statements:
  1. simple statements
  2. multi-line simple statements
  3. compound statements

Further, there are two types of expressions:
  1. simple expression
  2. complex expression

Simple expressions are just literals. Complex expressions involve the use of operators.

Most expressions are commonly found inside of compound statements such as if statements and for loops.

Is this a correct understanding of statements and expressions? Is there anything that I am missing?
Thanks!
Julie
Larz60+ write Feb-24-2021, 03:04 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
This looks good: https://stackoverflow.com/questions/4728...-in-python

Aside from homework, I don't think the difference is not practically important.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regular Expressions pprod 4 3,087 Nov-13-2020, 07:45 AM
Last Post: pprod
  Regular Expressions amitalable 4 2,774 Mar-14-2019, 04:31 PM
Last Post: DeaD_EyE
  Using Generators and their expressions BeerLover 3 4,545 Mar-18-2017, 11:06 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020