Variables and Data Types

1. Introduction to Variables

A variable is a named storage location in memory used to store data. In Python, variables are dynamically typed, meaning you don’t need to declare their type explicitly.

Declaring Variables

x = 10       # Integer
name = "Dynamic Duniya" # String
price = 99.99 # Float

Python automatically determines the data type based on the assigned value.


2. Rules for Naming Variables

  • Must start with a letter (a-z, A-Z) or underscore (_).
  • Can contain letters, digits (0-9), and underscores (_).
  • Cannot be a Python keyword (like if, else, while, etc.).
  • Case-sensitive (age and Age are different variables).

Valid examples:

my_var = 5
_name = "Dynamic Duniya"
value123 = 10.5

Invalid examples:

3var = 10   # Invalid: cannot start with a number
my-var = 5  # Invalid: hyphens are not allowed
if = 20     # Invalid: 'if' is a reserved keyword

3. Data Types in Python

Python has several built-in data types.

1. Numeric Types

  • Integer (int): Whole numbers.
  • Float (float): Decimal numbers.
  • Complex (complex): Numbers with real and imaginary parts.
num1 = 10       # Integer
num2 = 20.5     # Float
num3 = 3 + 4j   # Complex number

2. String Type (str)

Strings store text and are enclosed in single (' '), double (" "), or triple (''' or """) quotes.

text = "Hello, Python!"
multiline_text = """This is 
a multiline string."""

3. Boolean Type (bool)

Booleans hold True or False values.

is_active = True
is_logged_in = False

4. Sequence Types

  • List (list): Ordered, mutable collection.
  • Tuple (tuple): Ordered, immutable collection.
  • Range (range): Sequence of numbers.
fruits = ["apple", "banana", "cherry"]  # List
numbers = (1, 2, 3, 4)  # Tuple
range_nums = range(1, 10, 2)  # Range from 1 to 9 with step 2

5. Set and Dictionary Types

  • Set (set): Unordered, unique elements.
  • Dictionary (dict): Key-value pairs.
unique_numbers = {1, 2, 3, 4, 5}  # Set
student = {"name": "Rahul", "age": 25, "grade": "A"}  # Dictionary

4. Checking Data Types

To check the type of a variable, use type():

print(type(10))       # <class 'int'>
print(type(10.5))     # <class 'float'>
print(type("Hello"))  # <class 'str'>
print(type([1, 2, 3])) # <class 'list'>

5. Type Conversion

Python allows converting between data types using type casting.

Implicit Type Conversion (Automatic)

x = 5
y = 2.5
result = x + y  # Python converts 'x' to float automatically
print(result)  # 7.5
print(type(result))  # <class 'float'>

Explicit Type Conversion (Manual)

a = 10
b = "20"

# Convert string to integer before addition
c = a + int(b)  
print(c)  # 30

# Convert integer to string
d = str(a) + b  
print(d)  # "1020"

6. Summary

  • Variables store data in Python without explicitly declaring their type.
  • Python has different data types like integers, floats, strings, lists, tuples, sets, and dictionaries.
  • Use type() to check the data type.
  • Type conversion helps convert values between types when needed.