Pythonの基礎

Pythonの基礎まとめ

1. 変数とデータ型

x = 10           # 整数(int)
y = 3.14 # 小数(float)
name = "Alice" # 文字列(str)
is_ok = True # 論理値(bool)

2. リスト・辞書

# リスト(配列のようなもの)
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # → "apple"

# 辞書(キーと値のセット)
person = {"name": "Alice", "age": 20}
print(person["name"]) # → "Alice"

3. if 文(条件分岐)

age = 18
if age >= 20:
print("大人")
else:
print("未成年")

4. for文・while文(繰り返し)

# for文
for fruit in fruits:
print(fruit)

# while文
i = 0
while i < 3:
print(i)
i += 1

5. 関数

def greet(name):
print("Hello, " + name)

greet("Alice") # → Hello, Alice

6. クラス(オブジェクト指向)

class Dog:
def __init__(self, name):
self.name = name

def bark(self):
print(self.name + " says ワン!")

dog = Dog("Pochi")
dog.bark() # → Pochi says ワン!

7. モジュールの使い方

import math
print(math.sqrt(16)) # → 4.0

投稿者: chosuke

趣味はゲームやアニメや漫画などです

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です