0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

python入門 #1

Last updated at Posted at 2025-01-28

コメント化(メモ)

#このように#の後の文字は無視されるからめもがわりにつかうことができる

データーの表示(出力)

print(123)
print("hello") #文字の場合は「"」か「'」で囲う必要がある

問題演習1-1(print)

演算(基本)

print(1+1) #足し算
print(1-1) #引き算
print(2*2) #掛け算
print(2/2) #割り算

print("cat" + "dog") #文字をくっつける

問題演習2-1(演算)

変数の使い方

※ここではhensuとしているが、基本何でもいい(数字から始まるものは不可)

hensu = 10
print(hensu)

問題演習3-1と3-2(変数)

input(入力)

hensu = input()
print(hensu)

問題演習4-1(入力)

if文(もし~ならば)

if 1==1:
    print("yes")

while文(~の間繰り返す)

hensu = 1
while hensu != 10:
    print(hensu)
    hensu = hensu + 1

条件式(基本)

==等しいか
!=等しくないなか
< 左の値より右の値のほうが大きいか
> 右の値より左の値のほうが大きいか

"1" + 1のような文字+数字を行う方法

"1" + 1だと文字+数字と認識され、エラーが出る。これを解決するためには"1"を数字として認識させられるようにしないといけない。

print(int("1") + 1)

具体的な処理の流れは
int("1") + 1
1 + 1
2

"abc" + 1のような文字+数字を行う方法2

この場合abcは数字にできないので1を文字として認識させるようにしないといけない。

print("abc" + str(1))

具体的な処理の流れは
"abc" + str(1)
"abc" + "1"
abc1

関数(Scratchでいう定義)の使い方1

def kansu():#kansuという名前はあくまで関数名だから何でもいい
    print("1") #ここに関数の動作を書く]

kansu()#これで関数を実行する
kansu()#繰り返し使うこともできる

関数(Scratchでいう定義)の使い方2

def kansu(abc):
    print(abc)
kansu(123)

分かりにくいかもしれないのでScratchで簡単に示すとこういう感じです。
Screenshot 2025-01-28 21.27.13.png

for文

iというのは変数名なので、今まで同様iではなくて良いが、基本的にはiを使われることが多い。そしてrange(10)の10は繰り返す数。1回の繰り返しごとにiという変数は1ずつ増加する

for i in range(10):
    print(i)

実行結果

0
1
2
3
4
5
6
7
8
9

こちらもScratch版を載せとく
Screenshot 2025-01-28 21.30.35.png

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?