Python3 では、round()による四捨五入は使えない!!
How: decimal モジュールを使う。
サンプルコード
# -*- coding: utf-8 -*-
#
from decimal import Decimal, ROUND_HALF_UP
i = float(input())
print("入力値")
print(i)
#
print("roundの結果")
ex_round = round(i,1)
print(ex_round)
#
print("decimalの結果")
o = float(Decimal(str(i)).quantize(Decimal('0.1'), rounding=ROUND_HALF_UP))
print(o)
結果
入力値
12.25
roundの結果
12.2
decimalの結果
12.3
Oracle Application Express Notes | Apps development Notes | Google Cloud Platform | Python | apps test | Cool Beans | English | Books
2019/01/06
2019/01/05
Colaboratory すごい - Colaboratory + Python3 + Pandas
Google の Colaboratory を使ってデータサイエンス、機械学習にチャレンジをしてみる。
Goal:Google Colaboratory を立ち上げて、機械学習用のデータ・セットを読み込みデータをハンドリングする。主に、Pandas を使ってみるという内容。
How
1. Colaboratoryに新しいノートブックを作成する。以下にアクセスして、Python3のノートブックを作成する。
https://colab.research.google.com/
2. Notebook
Goal:Google Colaboratory を立ち上げて、機械学習用のデータ・セットを読み込みデータをハンドリングする。主に、Pandas を使ってみるという内容。
How
1. Colaboratoryに新しいノートブックを作成する。以下にアクセスして、Python3のノートブックを作成する。
https://colab.research.google.com/
2. Notebook
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2019/01/04
2019/01/01
標準入力の取扱 input と print - Python3
Goal:Python3での標準入力をハンドリングする。
#1 複数の値が1行に同時に入力された場合、数値として取り込む
入力値 --- 以下の3つの数値を入力したら、int型として取り込む
100 3 123456
(py3) toshi$ python python3-input.py
100 3 123456
output---
a=100
b=3
c=123456
#2 入力行数を最初に入力して、その後、複数行に渡る入力値を、配列として取り込む
入力値 --- 以下のように入力したら、3*3の行列として取り込む
3
1 2 3
4 5 6
7 8 9
(py3) toshi$ python python3-input2.py
3
1 2 3
4 5 6
7 8 9
output---
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#1 複数の値が1行に同時に入力された場合、数値として取り込む
入力値 --- 以下の3つの数値を入力したら、int型として取り込む
100 3 123456
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
#1 複数の値が1行に同時に入力された場合、数値として取り込む | |
# 入力値 --- 以下のように3つの数値が入力されて、int型として取り込む | |
# 100 3 123456 | |
# input | |
a, b, c = map(int,input().split()) | |
# output | |
print('output---') | |
print('a=%d' % a) | |
print('b=%d' % b) | |
print('c=%d' % c) |
(py3) toshi$ python python3-input.py
100 3 123456
output---
a=100
b=3
c=123456
#2 入力行数を最初に入力して、その後、複数行に渡る入力値を、配列として取り込む
入力値 --- 以下のように入力したら、3*3の行列として取り込む
3
1 2 3
4 5 6
7 8 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
#2 入力行数を最初に入力して、その後、複数行に渡る入力値を、配列として取り込む | |
# 入力値 --- 以下のように入力したら、3*3の行列として取り込む | |
# 3 | |
# 1 2 3 | |
# 4 5 6 | |
# 7 8 9 | |
# input | |
n = int(input()) | |
a = [] | |
for i in range(n): | |
a.append(list(map(int,input().split()))) | |
# output | |
print('output---') | |
print(a) |
(py3) toshi$ python python3-input2.py
3
1 2 3
4 5 6
7 8 9
output---
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
登録:
投稿 (Atom)