1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python基礎(変数,型,演算子,if文,for文)

Last updated at Posted at 2020-05-06

基本的な変数と型の使い方
変数に型宣言はなく、値が型を持っている

#1.変数と型

a = 10             # 整数型(int)
b = 10.123         # 浮動小数点型(float)
c = "Hello"        # 文字列型(str)
d = True           # 論理型(bool)
e = [1, 2, 3]      # リスト型(list)

print(a)
print(b)
print(c)
print(d)
print(e)

[出力]
10
10.123
Hello
True
[1, 2, 3]

#2.リスト、タプル


リストは配列と同じ

a = [10, 9, 8, 7, 6]     # リストの作成

print(a[0])             # 1番目の要素を表示

a.append(5)             # 末尾に要素を追加する
print(a)

a[0] = 0                # 要素の入れ替え
print(a)

[出力]
10
[10, 9, 8, 7, 6, 5]
[0, 9, 8, 7, 6, 5]


タプルもリストと同様に配列だが、要素の追加や削除、入れ替え等はできない

a = (5, 4, 3, 2, 1)     # タプルの作成
print(a)
print(a[2])    # 3番目の要素を取得

b = (1,)       #要素が1つだけのタプルは末尾に","が必要
print(b)

[出力]

(5, 4, 3, 2, 1)
3
(1,)

#3.演算子

算術演算子	+	足し算
-	引き算
*	かける
/	割る(小数)
//	割る(整数)
%	余り
**	べき乗
比較演算子	<	小さい
>	大きい
<=	以上
>=	以下
==	等しい
!=	等しくない
論理演算子	and	両者を満たす
or	どちらか片方を満たす
not	満たさない

#4.if文

a = 5
if a < 10:
    print(" a < 10")
elif a > 100:
    print(" a > 100 ")

[出力]
a < 10

#5.for文
ループの範囲をrangeやin演算子とともに用います

for a in [1, 100, 200]:    # リストを使ったループ
    print(a)
    
for a in range(3):      # rangeを使ったループ
    print(a)

[出力]
1
100
200
0
1
2

1
2
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?