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?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Learn to be a Pythonista!

Programming for Beginners

Learn to be a Pythonista! Try Python

This book doesn't make any assumptions about your background or knowledge of Python or computer programming. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(124+ reviews)

More Python resources

Ad