4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paizaコラボ企画ということで。順番はキャンペーン対象問題の順で。

Pythonを始めた人やDランク水準の人はこちらの記事を読むことをお勧めします。

問題を解く前に

1.まず、標準入力はできますか?

分からないなら上の問題を解きましょう。こちらのzennの記事に標準入力の例が載ってあるので分からないなら素直に読みましょう。
あとはQiitaでPythonの標準入力をまとめている記事があるのでそれも読んでおくことをおすすめします。

2.問題を解きましょう。

こんな駄文を読むより問題を解きましょう。paizaとQittaコラボ記念に記事を書くのもいいですね。

文字の一致

# coding: utf-8
a = input()
b = input()
if a == b:
    print("OK")
else:
    print("NG")

こんな入力も可能である。

# coding: utf-8
print("OK" if input() == input() else "NG")

==ではなくinを使うとPythonらしく見える。ただし「見える」だけであって、部分一致なので解法としては合ってないが。

# coding: utf-8
print("OK" if input() in input() else "NG")

一番小さい値

Pythonには関数に最小値を取得するmin()がある。

# coding: utf-8
input_line = [int(input()) for _ in range(5)]
print(min(input_line))

min()関数を使わない場合はこうする。

# coding: utf-8
input_line = [int(input()) for _ in range(5)]
s = 100
for i in input_line:
    if i < s:
        s = i
print(s)

足し算

# coding: utf-8
a,b = map(int,input().split())
print(a + b)

こんな解き方でも正解。

# coding: utf-8
print(sum(list(map(int,input().split()))))

list型にsum()を使うと合計値が出力される。

Eメールアドレス

# coding: utf-8
s = input()
t = input()
print(s + "@" + t)

これだけじゃないぞ!Pythonの標準出力達!

print(s,end="@");print(t)
print(str(s)+"@"+str(t))
print(f"{s}@{t}")
print("{}@{}".format(s,t))
print("%s@%s" % (s,t))

こちらの解説はzennの記事で。

【殿堂入りキャンペーン】N倍の文字列

# coding: utf-8
N = int(input())
print("*" * N)

ここまで解けたなら簡単な解法ではなくfor文を使って解いてみよう。

N = int(input())
for _ in range(N):
    print("*",end="")

ここまでできる人なら解説はいらないと思われるが、一応解説しておくとこんな感じで動いている。

N = int(input())

Nにint型の標準入力を代入する。

for _ in range(N):

for文を使ってNの回数分繰りかえす。アンダーラインは変数を使わないという明示ができる。

    print("*",end="")

アスタリスクを標準出力する。このとき、print()だけだと改行を行うので、それを阻止するためにprint( ,end="")とし改行を防ぐ。

さいごに

ここでしっかりと解説する気力はないので上のzennの記事を読んでください。ぜんぶ載ってます。
しかしこうやって解いてみるとPythonは本当に簡単な言語だな・・・。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?