0
0

More than 3 years have passed since last update.

はじめてのPython3 ~はじめての計算編~

Last updated at Posted at 2020-07-06

はじめに

この記事は、私がPython3で学んだことを備忘録としてメモしている程度のものです。

Python3 バージョンについて

この記事は以下のバージョンで進めています。

% python3 --version
Python 3.7.3

Python起動

% python3
Python 3.7.3 (default, Apr  7 2020, 14:06:47) 
[Clang 11.0.3 (clang-1103.0.32.59)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

この >>> になっている状態なら、入力を待機してくれています。

計算

演算式
x + y xとyの和
x - y xとyの差
x * y xとyの積
x / y xとyの商
x // y xとyの商の小数点以下を切り上げる
x % y xとyの剰余
x ** y xのy乗(累乗)
>>> 2 + 3 
6
>>> 4 - 2 
2
>>> 30 * 3
90
>>> 90 / 9
10
  • 「x ** y」 とすることで、累乗(xのy乗)となります。
# (例) 3の3乗
>>> 3 ** 3
27
  • 「x // y」 とすることで、商の小数点以下を切り下げます。
>>> 100 // 3
33
>>> 100 / 3
33.333333333333336
  • 「x % y」とすることで、x/yの剰余を算出します。
>>> 12 % 3 # 4あまり0
0
>>> 13 % 3 # 4あまり1
1
>>> 13 % 7 # 1あまり6
6

計算の優先順位

  • 最優先
    ・ ( ) … 括弧の中の計算式
  • 優先順位が高め
    ・ * … 乗
    ・ / … 商
    ・ % … 剰余
  • 優先順位は低い
    ・ + … 和
    ・ - … 差
number = 100

print(number + number) # 100 + 100
# 200

print(number * 2) # 100 * 2
# 200

print(number - number / 2) # 100 - 100 / 2
# 50.0

print((number + number) * 3) # (100 + 100) * 3
# 600

print((number * 2 - number) / 2) # (100 * 2 - 100) / 2
# 50.0

print(number % 30) # 100 % 3 ( ※ 100 を 3 で割った時の余り)
# 10

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0