05-Python

Python(Part.3)| python [arithmetic operator]

python| まとめ | 現役エンジニア&プログラミングスクール講師「python」のまとめページです。pythonに関して抑えておきたい知識や文法やにについて記事をまとめています。まとめページの下部には「おすすめの学習書籍」「おすすめのITスクール情報」「おすすめ求人サイト」について情報を掲載中...

Objectives

Understand ‘operator’ and ‘operand’.
Understand ‘operator and expression precedence’.

Overview of operators

When you think of ‘learning programming’, ‘variables’, ‘control structures’ and ‘classes’ come to mind, but before you learn about them, you need to know about the ‘symbols’ used in programming. Knowing about these ‘symbols’ will make learning programming easier.

In this article (Python (Part 3)), you will learn about ‘operators’ and ‘operand’ while dealing with ‘arithmetic operators’.

‘Operator’ and ‘Operand’

‘Using operators’

Before learning the ‘operators’, enter the following into the ‘interactive shell (python console)’. You will see the calculation results returned.

‘Operator’

‘Operator’ is ‘symbols’ used in the four arithmetic operations (addition, subtraction, multiplication, division, etc.), value comparisons and logic to find rules of value. There are the following types of operators.

OperatorUsages
Arithmetic operatorsTo perform four arithmetic operations
Comparison operatorsTo compare values and return a true or false value
Logical operatorsTo derive rules of value
Concatenation operatorsTo concatenate strings with strings
※Cannot be used in python to concatenate numbers and strings.

‘Arithmetic operators’

Arithmetic operators include ‘addition’, ‘subtraction’, ‘multiplication’, ‘division’, ‘remainder’ and ‘power’.

Arithmetic operatorsMeaning
+Adds the left and right values. (Addition)
Subtracts the left and right values. (Subtract)
*Multiply the left and right values. (Multiply)
/Divide the left value by the right value. (division)
//Divide the left value by the right value to obtain the ‘quotient’. (truncated division)
%Divide the left value by the right value to obtain the ‘remainder’. (remainder)
**Power the left value by the right times (power).

There is a difference between ‘power’ and ‘power of magnitude’. This article uses ‘power’ in the sense of multiplying the same number more than once.

【English translation of captcha】
演算子 → operators

‘Operand’

An ‘operand’ is a ‘value’ that is the object of an operation. The ‘operand’ is the following part

【English translation of captcha】
演算子 → operand

‘Operator and expression precedence’

‘Operators’ have priorities, so that when operators or expressions with different priorities are lined up, the operation with the highest priority is performed first. Commonly used for simple calculations are the ‘1’ expression join, the ‘4’ to ‘7’ arithmetic operators and the ’18’ assignment statement (which is not an operator in the grammar).

In the table below, priority ‘1’ is the highest and ’18’ the lowest.

The parentheses () in priority “1” are called parenthesis expressions or expression bonds. Although it is a tuple notation, a tuple is only generated when a “,” is used within round brackets. If it does not generate a tuple, it acts as an expression used in an arithmetic operation.

The “=” with priority “18” is an assignment statement. It is not an operator, although in python ‘+=’ and ‘-=’ are called cumulative assignment operators.

PriorityOperatorsMeaning
1() 、[]、
{key: value}、{}
Expression binding (tuple notation), list notation,
Dictionary notation, set notation
2x[n]、x[n:m]、
x(args *kwargs) x.attr
Indexes, slices,
Function calls, Attribute references
3await Expressionawait Asynchronous (pause in processing)
4**Power
5+x、-x、~xPositive numbers (unary +), Negative numbers (unary -), Bit inversion
6*、/、//、%、@Multiplication, division, Truncated division, Remainder, Matrix multiplication
7+、-Addition, Subtraction
8<<、>>Shift operation (shift left, shift right)
9&Bitwise logical conjunction (AND)
10^Bitwise exclusive or (XOR)
11|Bitwise OR (OR)
12in、not in、is、is not、
>、>=、<、<=、!=、==、<>
Comparisons (Attributional, Identity and Value comparisons)
13not xLogical negation (NOT)
14andLogical conjunction (AND)
15orOR (logical OR)
16if … elif … elseCondition
17lambdalambda
18=:=Assignment statement
※Not grammatically an operator

For example, if the following calculation is performed, the order of operations changes as follows.

First, the basic premise is that programmes are read ‘top to bottom’ and ‘left to right’. In the case of the expression below, the programme is a single line, so it is read from left to right.

In order, “answer” is read and “=” is read. At this point, the “=” in the assignment statement is read. Then the next operator to be read, “6”, and the next operator “+” are read, and the priorities of “=” and “+” are compared.

answer = 6 + 7 – 5 * (-4) * (4 + 6 / 3) + 5

As ‘+’ is the operation that takes precedence over ‘=’ from the precedence of the two, it is read further to the next operator.

answer = 6 + 7 – 5 * (-4) * (4 + 6 / 3) + 5

‘+’ and ‘-‘ have the same priority, so 6+7 is processed.

Operators where the left operation is performed first, such as ‘+’ and ‘-‘, are called left-joins.
The powers ‘**’ and ‘=’ are right-joins.

The expression changes to following and read further to the right. Now the priority is compared between ‘-‘ and ‘‘. The ‘‘ has a higher priority, so the calculation moves backwards.

answer = 13 – 5 * (-4) * (4 + 6 / 3) + 5

The bracketed expression is then read. The bracketed expression contains an expression for a negative number. It is then read up to the operator.

answer = 13 – 5 * (-4) * (4 + 6 / 3) + 5

Two asterisks(*) in a row. The priority is of course the same, so only the left-hand asterisk is processed.

answer = 13 – (-20) * (4 + 6 / 3) + 5

Further reading the formula on the right.

answer = 13 – (-20) * (4 + 6 / 3) + 5

()There is a bracketed formula, so the calculations in this take precedence.

answer = 13 – (-20) * (4 + 6 / 3) + 5

The “/” has a higher priority than the “+” and “/”, so the process is transferred to this one.

answer = 13 – (-20) * (4 + 6 / 3) + 5

The calculations in brackets are performed.

answer = 13 – (-20) * (4 + 2.0) + 5

It is read until the next ‘+’. The “*” has a higher priority and is therefore processed first.

answer = 13 – (-20) * (6.0) + 5

answer = 13 – (-120.0) + 5

‘-‘ and ‘+’ have the same priority, so ‘-‘ is processed.

answer = 13 – (-120.0) + 5

answer = 133.0 + 5

Further reading the formula on the right.

answer = 138.0

Finally, the assignment statement with the lowest priority is processed and the “variable” and “value” are linked.

In programming, “variables” are often compared to “boxes” and expressed as storing “values”, but “variables” do not contain “values”. What it contains is the “address to the value”. The value is reused as variable name → address to value → value.

An image that is closer to a programme is “variable (name)” and “value” are “tied” together.

That is all for this issue.

「python」おすすめ書籍 ベスト3 | 現役エンジニア&プログラミングスクール講師「python」の学習でお勧めしたい書籍をご紹介しています。お勧めする理由としては、考え方、イメージなどを適切に捉えていること、「生のpython」に焦点をあてて解説をしている書籍であることなどが理由です。勿論、この他にも良い書籍はありますが、特に質の高かったものを選んで記事にしています。ページの下部には「おすすめのITスクール情報」「おすすめ求人サイト」について情報を掲載中。...

ブックマークのすすめ

「ほわほわぶろぐ」を常に検索するのが面倒だという方はブックマークをお勧めします。ブックマークの設定は別記事にて掲載しています。

「お気に入り」の登録・削除方法【Google Chrome / Microsoft Edge】「お気に入り」の登録・削除方法【Google Chrome / Microsoft Edge】について解説している記事です。削除方法も掲載しています。...
【パソコン選び】失敗しないための重要ポイント | 現役エンジニア&プログラミングスクール講師【パソコン選び】失敗しないための重要ポイントについての記事です。パソコンのタイプと購入時に検討すべき点・家電量販店で見かけるCPUの見方・購入者が必要とするメモリ容量・HDDとSSDについて・ディスプレイの種類・バッテリーの持ち時間や保証・Officeソフト・ウィルス対策ソフトについて書いています。...
RELATED POST
05-Python

python| まとめ | 現役エンジニア&プログラミングスクール講師

2024年8月11日
プログラミング学習 おすすめ書籍情報発信 パソコン初心者 エンジニア希望者 新人エンジニア IT業界への就職・転職希望者 サポートサイト Programming learning Recommended schools Recommended books Information dissemination Computer beginners Prospective engineers New engineers Prospective job seekers in the IT industry Support site