2.6. Expressions

An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions (assuming that the variable x has been assigned a value):

17
x
x + 17

If you type an expression in interactive mode, the interpreter evaluates it and displays the result:

But in a script, an expression all by itself doesn’t do anything! This is a common source of confusion for beginners.

Type the following statements in the Python interpreter to see what they do:

5
x = 5
x + 1

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

You have attempted 1 of 4 activities on this page