DEV Community

Cover image for Day 4/100: Variables and Data Types Explained Simply
 Rahul Gupta
Rahul Gupta

Posted on

Day 4/100: Variables and Data Types Explained Simply

Welcome to Day 4 of the 100 Days of Python series!
Now that we’ve covered how to print and get input from the user, it’s time to dive deeper into variables and data types — two of the most important concepts in any programming language. Today, we’ll explain how to store information in Python and use different types of data.


📦 What You'll Learn Today

  • What variables are and how to use them
  • Understanding data types: Strings, Integers, Floats, and Booleans
  • How to perform basic operations with different data types
  • Type conversion

🧪 Step 1: What Are Variables?

In Python, a variable is like a container where you can store data. You can name the container anything you like (as long as it follows Python’s naming rules), and the variable will hold your data.

For example:

name = "Alice"
age = 25
height = 5.8
is_student = True
Enter fullscreen mode Exit fullscreen mode

Here:

  • name is a variable holding a string value "Alice"
  • age is a variable holding an integer value 25
  • height holds a floating-point number 5.8
  • is_student holds a boolean value True

Rules for Naming Variables:

  • Variable names must start with a letter or an underscore (_), not a number.
  • They can contain letters, numbers, and underscores (_), but no spaces.
  • Variable names are case-sensitive (age and Age are different).

🧠 Step 2: Understanding Data Types

In Python, we work with several data types. Let’s break down the most common ones:

🔹 String (str)

A string is used to represent text. Strings are surrounded by quotes — either single (') or double (").

greeting = "Hello, World!"
name = 'John Doe'
Enter fullscreen mode Exit fullscreen mode

🔹 Integer (int)

An integer is a whole number without a decimal point.

age = 30
year = 2025
Enter fullscreen mode Exit fullscreen mode

🔹 Float (float)

A float is a number that has a decimal point.

height = 5.9
temperature = 98.6
Enter fullscreen mode Exit fullscreen mode

🔹 Boolean (bool)

A boolean represents either True or False. This is especially useful for making decisions.

is_raining = False
is_python_fun = True
Enter fullscreen mode Exit fullscreen mode

🔹 Special Data Types (Lists, Tuples, Dictionaries, etc.)

We’ll dive into these more in later articles, but for now, here’s a quick example:

  • List: Ordered collection of values, e.g., ["apple", "banana", "cherry"]
  • Tuple: Immutable ordered collection, e.g., (1, 2, 3)
  • Dictionary: Unordered collection of key-value pairs, e.g., {"name": "Alice", "age": 25}

🧪 Step 3: Basic Operations with Data Types

Now let’s perform some basic operations with these data types.

String Operations

You can combine strings using the + operator (concatenation):

greeting = "Hello"
name = "Alice"
message = greeting + ", " + name + "!"
print(message)  # Output: Hello, Alice!
Enter fullscreen mode Exit fullscreen mode

Integer Operations

Integers can be added, subtracted, multiplied, and divided:

x = 5
y = 10
print(x + y)  # Output: 15
print(x * y)  # Output: 50
Enter fullscreen mode Exit fullscreen mode

Float Operations

Floats behave the same way as integers when performing arithmetic:

a = 5.5
b = 3.2
print(a + b)  # Output: 8.7
print(a / b)  # Output: 1.71875
Enter fullscreen mode Exit fullscreen mode

Boolean Operations

Booleans are often used in comparisons:

is_raining = True
is_sunny = False
print(is_raining and is_sunny)  # Output: False
print(is_raining or is_sunny)   # Output: True
Enter fullscreen mode Exit fullscreen mode

🔄 Step 4: Type Conversion

Sometimes, you may need to convert between different data types. Python provides several ways to do this:

# Converting to int
age = "30"  # age is a string
age_int = int(age)  # Convert string to int

# Converting to float
height_str = "5.8"
height_float = float(height_str)  # Convert string to float

# Converting to string
age_str = str(age_int)  # Convert int to string
Enter fullscreen mode Exit fullscreen mode

🚀 Recap

Today, you learned:

  • How to use variables to store data
  • The most common data types in Python: Strings, Integers, Floats, Booleans
  • How to perform basic operations with different data types
  • How to convert between data types using type conversion

Top comments (0)