04. Expressions and Variables

An expression is some line of code that is executed to provide some meaningful result. Expressions can come in with unary, binary or ternary operators with several operands.

Unary

Unary expressions use just a single operand. For example, -1 is a unary expression since it only uses the negative (-) operand.

>>> -1
-1
>>> +1
1

There is no need to use the + operand, as it's implicitly mentioned.

Binary

Binary expressions include one operator and two operands.

+
Addition
-
Subtraction
*
Multiplication
/
Produces a float
//
Floor division
**
Power operators
%
Modulo (remainder)

Try doing some math on your Python interpreter.

>>> 2 ** 5
32
>>> 123 % 23
8
>>> 19//2
9
>>> 23/2
11.5

Variable assignments

Use the = to assign a value to a variable. To output variable, simply type the variable name into the prompt.

>>> x = 3
>>> x
3

Learn to be a Pythonista!

Introducing Python

Learn to be a Pythonista! Try Python

Easy to understand and fun to read, Introducing Python is ideal for beginning programmers as well as those new to the language. Author Bill Lubanovic takes you from the basics to more involved and varied topics, mixing tutorials with cookbook-style code recipes to explain concepts in Python 3. End-of-chapter exercises help you practice what you learned.

$ Check price
39.9939.99Amazon 4.5 logo(37+ reviews)

More Python resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Ad