Monday, August 25, 2025

🐍VARIABLES AND DATA TYPES

Python Variables & Data Types

Learn how to store information in variables and understand Python’s basic data types.

Introduction

In Python, a variable is like a container that holds data. You give the container a name, and then you can use that name to access the data later. The kind of data a variable stores is called its data type.

Creating a Variable

You don’t need to declare variable types in Python — just assign a value using =.

# Assigning values to variables
name = "Alice"
age = 25
height = 5.6

print(name)
print(age)
print(height)

Output:


Alice
25
5.6

Common Data Types

Python has many data types, but here are the most common ones:

  • String (str): text data
  • Integer (int): whole numbers
  • Float (float): decimal numbers
  • Boolean (bool): True or False
# Examples of data types
greeting = "Hello"       # str
year = 2025              # int
price = 19.99            # float
is_student = True        # bool

print(type(greeting))
print(type(year))
print(type(price))
print(type(is_student))

Output:


<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>

Common Mistakes

  • Using invalid variable names:

    2name = "Bob"   # ❌ Error: variable names cannot start with numbers
    SyntaxError: invalid syntax
  • Forgetting quotes for strings:

    city = London   # ❌ Wrong
    NameError: name 'London' is not defined

    ✅ Correct:

    city = "London"
  • Case sensitivity: Variables are case-sensitive.

    Name = "Alice"
    print(name)  # ❌ Error
    NameError: name 'name' is not defined

    ✅ Correct:

    print(Name)  # Works fine

Deeper Insights

  • You can assign the same value to multiple variables at once:
  • x = y = z = 100
    print(x, y, z)
  • You can assign multiple values in one line:
  • a, b, c = 1, 2, 3
    print(a, b, c)
  • You can even change the type of a variable later:
  • age = 25
    age = "twenty five"
    print(age)

💡 Try It Yourself

  1. Create a variable country with your country’s name and print it.
  2. Create two variables, length and width, assign them values, and print their product.
  3. Try assigning one variable’s value to another and print both.
  4. What happens if you do number = "10" and then try print(number + 5)? (Hint: data types matter!)

What’s Next?

Now that you understand variables and data types, you’re ready to explore Operators in Python — the tools that let you do math and logic with variables.

➡️ Next: User Input

⬅️ Back to main page

You can run the  codes here and practice:

No comments:

Post a Comment

🐍What is scikitlearn??