Programming,  Python

Python: Order of operations, string operations, and comments

In programming, mathematics is important to know. If I want to know to make a program that needs a lot of operations or computation. Don’t worry this is not hard. I am at the beginning of our learning programming journey. I will learn the order of operations, string operations, and comments. If you dominate math this is easy for you.

Order of operations

I studied here in Spain so I did not know what is PEMDAS is. So sometimes we cannot remember how to solve the expression with the proper order of evaluation.  Let see what is PEMDAS.

P is for Parenthesis.

E is for Exponentiation

M is for Multiplication

D is for Division

A is for Addition

S is for Subtraction.

Keep in my this when you want to solve an expression in your program.

I will do some example to prove that Python programming language follows the order of evaluation depends on the rules of precedence.

First, let see expressions with parenthesis.

As you can see the rules of precedence has been followed by the interpreter of Python.

Exponentiation is the next highest precedence. Recall that the exponentiation operator symbol is this  **. Let see examples.

Multiplication and Division have the same precedence.

As you see there is an error or bug in the program. The expression I wrote has space before the number 6. This is illegal in Python when a white space left in before of the syntax. The error is a syntax called unexpected indent.

Operators with the same precedence are evaluated from left to right.

Strings  operations

Remember that string is the characters that you want to display. “This is I am writing now is 1 of the string inside of quotes”. The question is if we can do operations with strings. The answer is we cannot do any mathematical operations on strings. This is illegal in all programming languages.

Open again the IDLE of Python. Search (Near of the Start menu) → type IDLE → Enter

Note we can say that string can be in a single quote.

We can do trick like writing spam times x.

Comments

The first thing to know is that the program never displays the comments on the screen. Therefore, there a lot of comment uses like defining a variable, describing the function, and write a instruction inside the program and more. Practice using well with natural comment because in programming commenting your code can bring good understanding.

Each programming language may have an equal symbol like this # and others have a different symbol like this // or /** */.

Very easy right!

Practice, practice, and practice make your programming learning better.

Leave a Reply

Your email address will not be published. Required fields are marked *