Python Variables and Data Types
A variable is created the moment you assign a value to it. Python works out the type automatically.
name = "Asha" # str
age = 21 # int
cgpa = 8.7 # float
passed = True # bool
print(type(name), type(age), type(cgpa), type(passed))
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
Built-in Data Types
| Category | Types | Example |
|---|---|---|
| Numeric | int, float, complex | 10, 3.14, 2+3j |
| Text | str | "hello" |
| Sequence | list, tuple, range | [1,2], (1,2), range(5) |
| Mapping | dict | {"a": 1} |
| Set | set, frozenset | {1, 2, 3} |
| Boolean | bool | True, False |
| None | NoneType | None |
Multiple Assignment and Casting
a, b, c = 1, 2, 3
a, b = b, a # swap without a temp variable
x = int("25") + 5
y = float(7)
print(a, b, x, y)
2 1 30 7.0
Taking Input
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # input() always returns str
print(f"Hi {name}, next year you'll be {age + 1}")
Enter your name: Ravi
Enter your age: 20
Hi Ravi, next year you'll be 21
f-stringsPrefix a string with
f and put expressions in { }. It's the easiest way to format output.Track your progress through the Python course