Getting Started · Lesson 2 of 6
Python Variables and Data Types Explained with Examples
int, float, str and bool, how Python decides the type for you, type conversion, and the input() trap that catches every beginner.
A variable is a name that points to a value. In Python you create one simply by assigning to it — no type declaration needed:
student = "Priya" # str (text)
age = 21 # int (whole number)
cgpa = 8.4 # float (decimal number)
placed = False # bool (True / False)
Python works out the type from the value. This is called dynamic typing. You can check a type any time with type():
print(type(cgpa)) # <class 'float'>
The four basic types
| Type | Example | Used for |
|---|---|---|
int | 42, -7 | Counts, ages, marks |
float | 8.4, 3.14 | Percentages, prices, measurements |
str | "Pune", 'B.Tech' | Names, text, anything typed by a user |
bool | True, False | Yes/no conditions |
The input() trap
This catches everyone once:
a = input("First number: ") # user types 10
b = input("Second number: ") # user types 20
print(a + b) # prints 1020, not 30!
input() always returns a string, and + on strings joins them. Convert first:
a = int(input("First number: "))
b = int(input("Second number: "))
print(a + b) # 30
Type conversion functions: int(), float(), str() and bool(). If the conversion is impossible — int("abc") — Python raises a ValueError. We handle that properly in the errors lesson.
Working with strings
city = " bengaluru "
print(city.strip()) # "bengaluru" (removes spaces at both ends)
print(city.strip().title()) # "Bengaluru"
print(len("Python")) # 6
print("Py" in "Python") # True
print("Python"[0]) # "P" (indexing starts at 0)
print("Python"[-1]) # "n" (negative index counts from the end)
print("Python"[0:3]) # "Pyt" (slicing: start up to, not including, end)
Numbers: two operators that surprise people
print(7 / 2) # 3.5 — normal division always gives a float
print(7 // 2) # 3 — floor division
print(7 % 2) # 1 — remainder (great for checking even/odd)
print(2 ** 10) # 1024 — power
Naming variables well
- Use
snake_case:total_marks, notTotalMarksortm. - Names are case-sensitive:
Marksandmarksare different variables. - Do not name variables
list,strorinput— you will hide Python’s built-in functions.
Try it yourself: Write a program that asks for marks in 5 subjects, then prints the total, the percentage (to 2 decimal places using round(x, 2)) and whether the student passed (percentage ≥ 40).
Common interview questions
Try answering each one aloud before you open it.
1What is dynamic typing?
Variable types are decided at runtime from the value assigned, and a name can later refer to a value of a different type. No type declarations are needed.
2What is the difference between / and // in Python?
/ is true division and always returns a float (7/2 = 3.5). // is floor division and returns the largest whole number not greater than the result (7//2 = 3).
3Are strings mutable in Python?
No. Strings are immutable — methods like upper() or replace() return a new string instead of changing the original.
Practise this
Build something with it
FAQs
Single or double quotes for strings?
Both work the same way. Pick one style and be consistent; use the other when your text contains a quote, like "It's easy".
Why does 0.1 + 0.2 not equal 0.3 exactly?
Floats are stored in binary, which cannot represent some decimals exactly. Use round() for display, or the decimal module when exact money calculations matter.
References & further reading
Finished this lesson?
Mark it complete. Create a free account to save it on every device.