Variables and Data Types
Naming the Building Blocks
Imagine you’re a hacker setting up a secret lab. Every tool, every gadget needs a label so you can find it later. In programming, those labels are called variables. They’re the names we give to pieces of data so we can store, reuse, and manipulate them.
But variables are only half the story. The other half is data types - the nature of the thing you’re storing. Is it a number? A word? A list of items? Python, unlike many languages, makes this process simple and flexible. And when you need to change one type into another, you use type casting like reshaping clay into a new form.
This chapter is about learning how to name, classify, and transform data -the hacker’s way of organizing information for powerful scripts.
Why Variables & Types Matter
- Variables as Containers: A variable is a symbolic name pointing to a value in memory. Instead of hard‑coding values everywhere, you store them once and reuse them.
- Dynamic Typing: Python doesn’t require you to declare a type explicitly. It infers the type at runtime. This makes coding faster and more flexible.
- Data Types as Foundations: Every operation depends on the type of data. You can’t add a string to an integer without converting one. Understanding types prevents errors.
- Type Casting as Transformation: Sometimes you need to change a value’s type - like turning
"25"(string) into25(integer). Casting ensures compatibility and correctness.
Variables in Python
# Assigning values
name = "Shubham"
age = 25
is_engineer = True
- Why? Variables let you store information with meaningful names. Instead of writing
25everywhere, you useage. - Naming Rules: Must start with a letter or underscore, can’t use reserved keywords, and are case‑sensitive.
Data Types in Python
Python’s built‑in types cover most needs:
- Numbers:
int,float,complex - Text:
str - Boolean:
bool - Collections:
list,tuple,set,dict
temperature = 36.6 # float
items = ["laptop", "phone", "tablet"] # list
config = {"version": 3, "debug": True} # dictionary
- Why? Each type has unique behaviors. Lists are mutable, tuples are immutable, sets remove duplicates, and dictionaries map keys to values.
Type Casting in Python
# String to integer
num_str = "100"
num_int = int(num_str)
# Integer to string
age = 25
age_str = str(age)
# Float to integer
pi = 3.14
pi_int = int(pi)
- Why? Casting ensures compatibility. You can’t concatenate
"25"(string) with5(integer) unless you convert one. - Explicit vs Implicit: Python sometimes converts types automatically (implicit), but often you must cast explicitly to avoid errors.
The Hacker’s Notebook
- Variables are labels: Treat them like names for your tools - clear, descriptive, and reusable.
- Dynamic typing is freedom: Python lets you assign without declaring, but hackers stay mindful of what type they’re working with.
- Data types define behavior: Lists, sets, and dictionaries aren’t just storage—they’re strategies for organizing information.
- Type casting is transformation: Always know when to reshape data to fit the operation.
Hacker’s mindset: Think of variables as your inventory, data types as categories, and casting as the art of adaptation. Master these, and you control the flow of information in your scripts.
