LoginSignup
5
4

More than 5 years have passed since last update.

pythonコードまとめ(初級)

Last updated at Posted at 2017-11-22

目的・概要

pythonの勉強をし始めた。
本を読んでコードを打つだけでは不安なのでポイントとなるコードをここにまとめる

基本

#コメント

#複数行に渡る文字列
a="""
こんにちは
こんばんは
"""

#変数(型宣言不要)
a=10

#文字列中の変数の挿入
a="""
こんにちは
こんばんは
{0}
{1}
""".format(a, b)

#入力(文字列型)
a=input("入力してください")

#if分岐
if a==10:
    a+=1
else:
    a-=1

#繰り返し(0-9までが印字)
for i in range(10):
    print(i)

リスト型etc.


#リスト
b = [1,2,3,4,5]

#リストを使った繰り返し(1-5までが印字)
for i in b:
    print(i)

#リストの結合
a = b + c

#スライス
a = [10, 20, 30, 40]
a[1:3]
>>>[20, 30]

#タプル
c = (1,2,3,4,5)

#集合型
d = {1,2,3,4,5}

#辞書型
e = {"一月":1, "二月":, "三月":3}

#キー一覧の取得(listに変換の必要あり)
e.keys()

#値の一覧の取得(listに変換の必要あり)
e.values()

関数について

#基本形
def add(a, b)
    '''docstring'''
    c = a+b
    return c

#docstringを確認
help(add)

終わりに

まだまだ勉強しないといかんですね

5
4
2

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
5
4