LoginSignup
8
10

More than 5 years have passed since last update.

初心者がPythonの練習

Last updated at Posted at 2014-08-18

初心者のボクがPythonに挑戦します。
追記:Python3形式にしました。

環境

Colaboratory で実行しています。

環境構築についてはUbuntuの中で試します。
http://qiita.com/pugiemonn/items/7257aa01897011469131

HelloWorld

printでHelloWorldという文字列を出力します。

HelloWorld!
print("HelloWorld!")

変数

変数に10を代入します。

Python
val_int = 10
val_float = 1.23
val_boolean = True

TrueやFalseは頭文字が大文字です。

複数代入

,を並べると変数に複数代入できます。

変数を複数代入
a, b, c = 10, 30, 40
print(a)
print(b)
print(c)

#出力
10
30
40

リスト

リストは値が順番に並べられているものです。

Python
number = [2, 40, 3]
string_text = ['a', 'b', '8']



# リストのアクセス方法
>>> number[1]
40

ディクショナリ

キーとバリューがペアのハッシュのことです。ディクショナリは順番を保持しません。

Python
args = {80: 30, 2: 'piyo', 'hoge': 90}

# ディクショナリのアクセス方法
>>> args[80]
30
>>> args["hoge"]
90

コメント

1行コメント

コメントは#を使用します。

Python
# 1行のコメント

複数行コメント

複数行のコメントです。

Python
"""
コメントだよおおおおおおおお
"""

四則演算

計算しちゃいます。

Python
hoge = 100 + 2200
hoge = hoge / 100
hoge = hoge * 2

print hoge # 46

階乗

Python
hoge = 10 ** 2

print hoge # 100

for ループ

ループ
i = 0
for _ in range(10):
  i += 1
  print i

# 出力
1
2
3
4
5
6
7
8
9
10

条件文 if 文

if 文の使い方です。

vv = 50

if vv < 100:
  vv = '00'

print(vv)

# 出力
00

elsif 文

if がもしもしならば、あるいはもしの場合に elsif を使用します。

vv = 200

if vv < 100:
  vv = '00'
elif vv < 1000:
  vv = '0' + str(int(vv / 100))

print(vv)

# 出力
02

標準入力

Python
h1 = input()

print(h1)

# 入力
10

# 出力
10

標準入力の型

Python
h1 = input()
print(type(h1))

# 入力
10

# 出力
<class 'str'>

入力すると str 型になります。

関数

インデントで管理しています。

Python
def spam():
  hoge = 12
  return hoge

print(spam()) # 12

文字列の結合

+で文字列を結合できました。

文字列の結合
hoge = 'hoge'
print('piyo' + hoge)# piyohoge

感想

お疲れ様でした(☝ ՞ਊ ՞)

8
10
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
8
10