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

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Learn to be a Pythonista!

Python Programming

Learn to be a Pythonista! Try Python

This book is designed to be used as the primary textbook in a college-level first course in computing. It takes a fairly traditional approach, emphasizing problem solving, design, and programming as the core skills of computer science. However, these ideas are illustrated using a non-traditional language, namely Python.

$ Check price
45.9945.99Amazon 4.5 logo(211+ reviews)

More Python resources

Ad