‘Python Programming Tutorial- Expressions and Variables’ post is follow up to ‘Types in Python Explained- Built-in Data types‘. All posts in this series are based on course “Python For Data Science” at cognitiveclass.ai.
Expressions are operations that Python performs. For example, basic arithmetic operations like adding multiple numbers. You can check in Jupyter Notebooks by typing different expressions.
We can perform operations such as subtraction using the subtraction sign.
type 30-70 and run in python, here python acts as a calculator.
We can use the double slash for integer division, where the result is rounded 50//10.5
The expressions in the parentheses are performed first- (45*5) + 8
Python follows mathematical conventions when performing mathematical expressions.
You can do a variety of mathematical operations in Python including:
- addition: 2 + 2
- subtraction: 5 – 2
- multiplication: 3 * 2
- division: 4 / 2
- exponentiation: 4 ** 2
What is Variable?
In Python a variable is a name that refers to a value. Usually Assignment statements create new variables and also give them values to refer to.
In simplest terms, a variable is just a box that you can put stuff in. You can usevariables to store all kinds of stuff, but for now, we are just going to look at storing numbers in variables.
lucky = 7
This code creates a variable called lucky , and assigns to it the integer number 7
Convention for Variable names
It can be arbitrarily long. They can contain both letters and numbers, but they have to begin with a letter. It is legal to use uppercase letters, but it is a good idea to begin variable names with a lowercase letter.
The underscore character (
_) can appear in a name. It’s common to use the underscore to represent the start of a new word, you can also use a capital letter.
keywords cannot be used as variable names. A error will be thrown if it is used.