Python Variables and Data Types

A variable is created the moment you assign a value to it. Python works out the type automatically.

Python
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

CategoryTypesExample
Numericint, float, complex10, 3.14, 2+3j
Textstr"hello"
Sequencelist, tuple, range[1,2], (1,2), range(5)
Mappingdict{"a": 1}
Setset, frozenset{1, 2, 3}
BooleanboolTrue, False
NoneNoneTypeNone

Multiple Assignment and Casting

Python
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

Python
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