LoginSignup
0
2

More than 5 years have passed since last update.

【Python】基本編

Last updated at Posted at 2019-03-24

はじめのコマンド

  • 以下のコマンド実行で、Pythonプログラマが心すべき言葉が出力されます。
import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

翻訳した結果

<Pythonの禅 ~By Tim Peters>
- 美しさは醜いよりも優れています。
- 明示的は暗黙的より優れています。
- 単純は複雑よりも優れています。
- 複雑は複雑よりも優れています。
- フラットは入れ子よりも優れています。
- 疎は密よりも優れています。
- 読みやすさが重要です。
- 特別な場合は、ルールを破るほど特別なわけではありません。
- 実用性は純度を打ち負かすが。
- エラーは黙って渡されるべきではありません。
- 明示的に沈黙していない限り。
- あいまいさに直面して、推測する誘惑を拒否します。
- それをするための一つの、そして好ましくは一つだけの明白な方法があるべきです。
- あなたはオランダ人でない限り、その方法は最初は明白ではないかもしれませんが。
- 今ではないよりはましです。
- 今では正しいよりも多くの場合決して良いことはありませんが。
- 実装を説明するのが難しい場合、それは悪い考えです。
- 実装が説明しやすいのであれば、それは良い考えかもしれません。
- 名前空間は素晴らしいアイデアの1つです - それらをもっとやろう!

時々、このコマンドを実行して内省したいと思います。

Hello World の出力

print('Hello World')

ブロックの表現

  • インデントで表現します
i=3

if i % 2:
  print('偶数')
else:
  print('奇数')
  • 上記のif文を例とすると、if と elseのインデントを揃えて、条件分岐内の処理は、そこからインデントして、表現します。インデントの文字数は半角スペース4文字、タブでなくても構いません。上記の例では2文字の半角スペースで表現しています。

繰り返し

for i in range(1, 100):
  print(i)

  • 明示的に型の宣言は不要です。

- 変数名 = (値)で自動的に型が定義されます。

型は値(オブジェクト)が持ってます。
変数は値(オブジェクト)を参照しているだけで、type関数は値(オブジェクト)の型を表示してるだけです。
変数は変数辞書に格納され、変数名は辞書キーとして使う単なる文字列です。

# 整数
i = 10
print(i,type(i))

# 浮動小数点
f = 3.141592
print(f,type(f))

# 文字列
s = "hoge"
print(s,type(s))

=>
10 <class 'int'>
3.141592 <class 'float'>
hoge <class 'str'>
  • 上記の例で分かるように、自動的に適した型が定義されます。

コレクション

  • 複数のデータ型をまとめてコレクションできます。
my_collect = ['hoge', 'foo', 'bar', 2, 3.141592]
print(my_collect)

for val in my_collect[0:5]:
    print(val, type(val))

=>
['hoge', 'foo', 'bar', 2, 3.141592]
hoge <class 'str'>
foo <class 'str'>
bar <class 'str'>
2 <class 'int'>
3.141592 <class 'float'>

辞書

# 辞書の作成
my_dict = {
    'key1': 'val1',
    'key2': 'val2',
    'key3': 'val3',
}

# リストの出力
print(my_dict)

# キーのみ出力
for key in my_dict.keys():
  print(key)

# 値のみ出力
for val in my_dict.values():
  print(val)

=>
{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
key1
key2
key3
val1
val2
val3
0
2
3

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
2