Understanding Variables and Data Types in Python

Understanding Variables and Data Types in Python

In Python, variables are used to store data values. Unlike some other programming languages, Python does not require you to declare the type of a variable. The type is inferred from the value it is assigned. Let's explore this with some examples.

Step 1: Variable Assignment

Here's how you can assign different types of data to variables:


# Assigning an integer
x = 10

# Assigning a float
y = 3.14

# Assigning a string
name = "Darchums"

# Assigning a boolean
is_active = True

Step 2: Checking Data Types

You can use the type() function to check the data type of a variable:


print(type(x))        # Output: <class 'int'>
print(type(y))        # Output: <class 'float'>
print(type(name))     # Output: <class 'str'>
print(type(is_active))# Output: <class 'bool'>

Step 3: Try It Yourself!

Use the interactive editor below to experiment with Python variables and data types:

Feel free to modify the code and observe how the output changes based on your inputs.

Understanding Variables and Data Types in Python

Variables are like containers in Python — they store data values that your program can use and modify. Unlike many other programming languages, Python doesn’t require you to declare the data type of a variable explicitly. Python figures it out based on what you assign to it!


1. Declaring Variables

You create a variable by simply assigning a value to a name:


# Examples of variable assignments
name = "Darchumstech"      # string
age = 25                   # integer
height = 5.9               # float
is_programmer = True       # boolean

These are now stored in memory and can be used anywhere in your code.


2. Data Types in Python

Python has several built-in data types. Here are the most commonly used:

  • int – whole numbers (e.g., 5)
  • float – decimal numbers (e.g., 3.14)
  • str – strings/text (e.g., "Hello")
  • bool – boolean values (True or False)

You can check a variable’s type using the type() function:


print(type(age))        # <class 'int'>
print(type(height))     # <class 'float'>
print(type(name))       # <class 'str'>
print(type(is_programmer))  # <class 'bool'>

3. Dynamic Typing

In Python, you can change the type of a variable by assigning a new value:


x = 10          # int
x = "Ten"       # now a string
print(type(x))  # <class 'str'>

This feature is known as dynamic typing.


4. Naming Variables

Some rules for naming variables in Python:

  • Names must start with a letter or underscore (_)
  • Can contain letters, digits, and underscores
  • Cannot start with a number
  • Case-sensitive: nameName

valid_name = "Python"
_valid = 123
thisIsCamelCase = True

5. Try It Yourself!

Use the interactive editor below to experiment with variables and data types:

Modify the values above and run the program to see how Python handles each type!


6. Quick Practice Task

Try creating variables for your name, age, favorite language, and whether you love coding. Print them all!


# Your mini exercise
my_name = "Your Name"
my_age = 20
language = "Python"
love_coding = True

print("Name:", my_name)
print("Age:", my_age)
print("Favorite Language:", language)
print("Love Coding:", love_coding)

Paste it in the editor above and hit ▶ Run!


What's Next?

In the next tutorial, we'll cover how to get input from users and create interactive programs!

Comments

Popular Posts