A variable is a named storage location that holds a value your program can read and change. A data type tells the language what kind of value that is — an integer like 42, a floating-point number like 3.14, a string like "hello", or a boolean like true — and therefore which operations make sense on it.
Getting types right early prevents the most common beginner bugs: adding a number to a string, dividing integers when you expected decimals, or comparing values that look equal but have different types.
What a Variable Actually Is
When you write x = 5, the language reserves a spot for the value 5 and lets you refer to it by the name x. Later assignments like x = x + 1 read the current value, compute a new one, and store it back under the same name.
Two details matter more than beginners expect:
- A variable is not its value. The name is a label; the value can change. After
x = x + 1, the old5is gone. - Assignment is not equality.
x = x + 1is nonsense as a math equation, but as code it means "compute the right side, then store it inx."
The Four Core Data Types
Almost every language starts from the same four primitives:
| Type | Example values | Typical use | Watch out for |
|---|---|---|---|
int |
0, -7, 42 |
counting, indexing | overflow in fixed-width languages |
float |
3.14, -0.5, 2.0 |
measurements, ratios | rounding error, never compare with == |
string |
"abc", "42" |
text, user input | "42" is not the number 42 |
bool |
true, false |
conditions, flags | implicit conversion in some languages |
In languages like C, Java, or Rust, an int has a fixed width. A 32-bit signed integer can only hold values from to , so adding 1 to the maximum wraps around or errors. Python integers grow as needed, which is one reason it feels forgiving.
Static vs Dynamic Typing
Languages differ in when types are checked:
| Static typing (Java, C, TypeScript) | Dynamic typing (Python, JavaScript) | |
|---|---|---|
| Type declared | At compile time: int x = 5; |
At runtime: x = 5 |
| Wrong-type error caught | Before the program runs | When the line executes |
| Can a variable change type? | No | Yes (x = 5 then x = "hi") |
Neither is "better" — static typing catches mistakes earlier, dynamic typing is faster to write. But in both worlds the values themselves always have a type, and the same conversion rules apply.
Worked Example 1: The String-Plus-Number Bug
Suppose a program reads a user's age and adds 1:
age = input("Your age: ") # user types 20
next_year = age + 1 # TypeError!
input returns the string "20", not the integer 20. Strings support + only with other strings, so Python raises an error. The fix is an explicit conversion:
age = int(input("Your age: ")) # "20" -> 20
next_year = age + 1 # 21
JavaScript makes the same mistake quieter and therefore worse: "20" + 1 silently produces the string "201". The lesson is identical in both languages — check what type a value really is before doing arithmetic on it.
Worked Example 2: Why 0.1 + 0.2 Is Not 0.3
Floats are stored in binary with a fixed number of bits, and most decimal fractions cannot be represented exactly. So:
That means this check fails:
if 0.1 + 0.2 == 0.3:
print("equal") # never prints
The standard fix is to compare against a small tolerance instead of using ==:
if abs((0.1 + 0.2) - 0.3) < 1e-9:
print("close enough") # prints
The rule of thumb: use int whenever values are exact counts (money in cents, array indexes, loop counters) and reserve float for genuinely continuous quantities.
Converting Between Types
Conversions are either explicit (you ask for them) or implicit (the language does them silently):
int("42")→42, butint("4.2")raises an error in Python — parse tofloatfirst.float(3)→3.0is always safe;int(3.9)→3truncates, it does not round.str(42)→"42"is how you safely glue numbers into messages.- Booleans convert from "truthiness": in Python,
0,"", and empty lists are falsy; everything else is truthy.
Prefer explicit conversion. Code that relies on implicit coercion is exactly where bugs like "201" hide.
Common Mistakes With Variables and Types
Confusing = and ==. One assigns, the other compares. Writing if x = 5 is a syntax error in Python but a silent bug in C.
Integer division surprises. In Python, 7 / 2 is 3.5 but 7 // 2 is 3. In C and Java, 7 / 2 between two ints is already 3. If a formula like an average comes out suspiciously rounded, check the division.
Comparing floats with ==. As shown above, use a tolerance.
Reusing one variable for different meanings. A variable named data that holds a string, then a list, then a number is technically legal in dynamic languages and a maintenance nightmare. One name, one meaning.
Where Types Take You Next
Every structure you will meet later is built from these primitives: a list is a sequence of typed values, a hash table maps keys to values, and a class bundles typed fields with methods. When you debug anything, the first two questions are still the same ones from this page — what value does this variable hold right now, and what type is it? Print both, and most "mysterious" bugs stop being mysterious.
Frequently Asked Questions
- What is the difference between int and float?
- An int stores whole numbers exactly, like 0, 7, or -42, and is the right choice for counts and indexes. A float stores numbers with a decimal point, like 3.14, but in binary with limited precision, so small rounding errors are normal. Use int for exact quantities and float for measurements or ratios where tiny imprecision is acceptable.
- Why is 0.1 plus 0.2 not exactly 0.3 in programming?
- Computers store floats in binary, and most decimal fractions such as 0.1 cannot be represented exactly in binary, so each is stored as the nearest representable value. Adding those approximations gives 0.30000000000000004 instead of 0.3. That is why float comparisons should use a small tolerance instead of an exact equality check.
- What is the difference between static and dynamic typing?
- In statically typed languages like Java or TypeScript, each variable has a declared type that is checked before the program runs, and the variable cannot change type. In dynamically typed languages like Python or JavaScript, types belong to values and are checked at runtime, so a variable can hold a string at one moment and a number later.
- How do I convert a string to a number?
- Use an explicit conversion function: in Python, int("42") gives 42 and float("4.2") gives 4.2; in JavaScript, Number("42") works for both. Conversion fails or produces NaN if the text is not a valid number, so validate user input first. Avoid relying on implicit coercion, which can silently turn "20" + 1 into the string "201".
- What is a boolean used for?
- A boolean holds one of two values, true or false, and is the type behind every condition: if statements, while loops, and comparison results all produce or consume booleans. Comparisons like x > 5 evaluate to a boolean, and logical operators such as and, or, and not combine them. Booleans are also used as flags to record whether something has happened.
Need help with a problem?
Upload your question and get a verified, step-by-step solution in seconds.
Open GPAI Solver →