3.1 Expressions and Statements
Constants
The values of constants do not change. They are constant values. Constants can take the form of integers, letters, or strings. We can have both numeric and string constants. String constants are normally surrounded by single or double quotations. When we supply a constant to the print function, the value of that constant is printed to the console. When we say 'print(123),' Python writes '123' to the terminal.
Variables
A variable is a specific location in memory where a programmer may store data. This saved data may be accessed later by using the variable name. It is up to the programmer to choose a variable name. You may also modify the value of a variable later on. A variable differs from a constant in this case. Consider the following example, in which we have the variables x and y with values of 25 and 50.
Statements
Different sorts of statements can be identified. The first is an assignment statement, as seen below. We set the value '5' to the variable 'x' there. Second, we have an assignment statement that includes an expression. You can see the numeric operator there, which adds the value 5 to the variable x. Third, you'll see a print statement, which instructs Python to output the value of variable 'x' to the terminal.
Assignment Statement
In an assignment statement, on the left-hand side, we have the variable to store the result. On the right-hand side, we have the expression to be evaluated.
Numeric Expressions
Numeric operators are included in numerical expressions. You'll see several common operations, such as addition, subtraction, and multiplication, as well as some unfamiliar operator symbols. In Python, for example, the operator sign for multiplication is an asterisk. Divide with a slash, power with a double asterisk, and the remainder with a percentage sign, for example.
Operator Precedence
What should you do if there are many operators working at the same time? Even with standard mathematical processes, this is a prevalent problem. This is why we require an evaluation order. This is known as operator precedence. It assists us in determining which operation to perform first, which to perform next, and so on.
The rules can be identified as follows. Parenthesis should be evaluated first and then look at the exponentiation. Multiplication, division, and remainder operations come next. Then comes addition and subtraction. Left to right is the last in the list.
- Parenthesis
- Power
- Multiplication
- Addition
- Left to Right
Example :
There is no parenthesis here. Of course, if we had parenthesis, things would have been a lot simpler. We have exponentiation, which is two to the power three, which is symbolized by a double asterisk. It should be assessed first because it is the most important operation in this case. The outcome of step one is eight. Then, in step two, we have multiplication and division, both of which have a similar priority level. So, using the left-to-right rule, we see that division comes before multiplication. In step two, we divide eight by four, yielding two. Step 3 is simple since we will execute multiplication before adding, resulting in ten. Finally, we only have addition and subtraction to do.










0 Comments