Data Types
In Python, data types define the kind of values a variable can hold and how those values can be manipulated.
Python is dynamically typed, meaning you don’t need to declare the data type of a variable explicitly, it is inferred at runtime based on the value assigned.
Built-in Data Types in Python
1. Integers (int)
Whole numbers, positive or negative, without decimals.
x = 10
type(x) # <class 'int'>2. Floating-Point Numbers (float)
Numbers containing decimals or written in exponential notation.
pi = 3.15
type(pi) # <class 'float'>3. Complex Numbers (complex)
Numbers with a real and imaginary part.
c = 6 + 8j
type(c) # <class 'complex'>
a, b = 2, 3
c = complex(a, b) # (2+3j)Python uses
j(noti) to represent the imaginary unit.
4. String Type (str)
A sequence of characters enclosed in quotes.
name = 'muskan'
type(name) # <class 'str'>5. Boolean Type (bool)
Booleans represent truth values: True or False. They commonly result from comparison operations.
a, b = 7, 6
greater = b > a
print(greater) # False6. None Type (NoneType)
None represents the absence of a value, similar to null in other languages.
result = None
print(result) # NoneType Conversion
- Python provides built-in functions like
int(),float(), andcomplex()to convert data types.
- Converting float → int truncates decimals (does not round).
- Converting int → float simply appends
.0.
a = 5.6
b = int(a) # float → int
print(b, type(b)) # 5 <class 'int'>
k = float(b) # int → float
print(k, type(k)) # 5.0 <class 'float'>
c = complex(4, 5) # create complex number
print(c, type(c)) # (4+5j) <class 'complex'>- Booleans can be converted into integers:
True → 1False → 0
print(int(True)) # 1
print(int(False)) # 0
print(float(False)) # 0.0Sequence Data Types
Python provides multiple built-in collection types:
1. List (list)
- Ordered, mutable collection.
- Enclosed in [ ].
- Allows duplicates.
l = [4, 5, 6, 7]
type(l) # <class 'list'>2. Tuple (tuple)
- Ordered, immutable collection.
- Enclosed in ( ).
- Useful for storing data that should not change.
t = (4, 5, 6, 7)
type(t) # <class 'tuple'>3. Set (set)
- Unordered, mutable, collection of unique elements.
- Enclosed in .
- Removes duplicates automatically.
s = {4, 5, 6, 4}
type(s) # <class 'set'>4. Range (range)
- Represents a sequence of numbers.
- Immutable, commonly used in loops.
r = range(10)
set(r) # {0,1,2,3,4,5,6,7,8,9}The general syntax is:
range(start, stop, step)
- start → beginning of the sequence (default = 0)
- stop → end of the sequence (excluded)
- step → difference between consecutive values (default = 1)
You can also generate custom sequences by specifying all three parameters:
set(range(2, 11, 2)) # {2, 4, 6, 8, 10}Summary
- Python is dynamically typed, meaning variable types are assigned automatically based on the value.
- Core data types include numbers (int, float, complex), strings, booleans, and None, with type conversions.
- Python support sequence data types:
- List (mutable, ordered)
- Tuple (immutable, ordered)
- Set (mutable, unordered, unique elements)
- String (immutable sequence of characters)
- Range (immutable sequence of numbers)
Written By: Muskan Garg
How is this guide?
Last updated on
