Python初級編 第2回【print()、変数、type()】

Python初級編 第2回【print()、変数、type()】

はじめに

print("Hello world!")      #出力結果:Hello world!

変数とは

# 変数に値を代入する
message = "Hello world!"

# 変数の値を出力する
print(message)  # 出力結果:Hello world!

変数の命名規則

variable1 = 10
_variable2 = 20
invalid-variable = 30  # ハイフンは使用できないためエラーになる
number_one = 5
number_two = -4
number_three = 0
number_one = 3.14
number_two = -0.001
number_three = 2.715
sentence_one = "Hello" # sentence_one = 'Hello'で変数を宣言することもできます。
sentence_two = "Hi" # sentence_two = "Hi"で変数を宣言することもできます。
sentence_three = "こんにちは" # sentence_three = "こんにちは"で変数を宣言することもできます。

ブール型(bool)とは

is_student = True
has_permission = False

type()とは

# 整数型の変数
number = 10
print(type(number))  # 出力結果:<class 'int'>

# 浮動小数点型の変数
pi = 3.14
print(type(pi))  # 出力結果:<class 'float'>

# 文字列型の変数
greeting = "Hello"
print(type(greeting))  # 出力結果:<class 'str'>

# ブール型の変数
is_happy = True
print(type(is_happy))  # 出力結果:<class 'bool'>
Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

コメントを残す

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

CAPTCHA