HomeBooks

En

Chapter 1
§1.0 Running Python
Chapter 2
§2.0 Basics
§2.1 Variables and Data Types
KM
bykarutt

2025-06-03

Variables and Data Types

In Python, you use "variables" to handle data. Let's learn about variables and the "types (data types)" of data they can hold.

1. What is a Variable?

  • Overview: A variable is like a "box" for storing data, each with its own name.
  • What You Can Do: Temporarily store values like numbers, text, or True / False and use them as many times as you want in your program. You can name variables freely (with some rules).
age = 20
name = "Taro"
is_student = True

print(age)        # Outputs 20
print(name)       # Outputs Taro
print(is_student) # Outputs True

2. What is a Data Type?

  • Overview: The "type" of data a variable can hold.
  • What You Can Do: The type determines what operations you can perform.

Here are the four main data types commonly used in Python:

TypeDescriptionMain Uses & FeaturesExample
intIntegerUsed for arithmetic operationsx = 10
floatDecimalUsed for calculations with decimalspi = 3.14
strString (text)Used for text like sentences or namess = "hello"
boolBoolean (True/False)Used for conditionals and logic operationsflag = False
x = 5           # int
y = 2.5         # float
s = "Python"    # str
flag = True     # bool

3. Checking the Type

  • Use the type() function to check a variable's data type.
print(type(x))  # <class 'int'>
print(type(s))  # <class 'str'>
Prev
§2.0 Basics