Python Data Types Explained: Full Beginner Guide with Examples
π Data Types in Python
int · float · str · bool · list · tuple · set · dict · None · range · complex
Data types define the kind of data a variable can store in a program. Python has several built-in data types that are used to store different kinds of values such as numbers, text, collections, and more. Python automatically detects the data type — you do not need to declare it manually.
| Data Type | Category | Example |
|---|---|---|
| int | Numeric | x = 10 |
| float | Numeric | x = 3.14 |
| complex | Numeric | x = 2+3j |
| str | Text | x = "Hello" |
| bool | Boolean | x = True |
| list | Collection | x = [1, 2, 3] |
| tuple | Collection | x = (1, 2, 3) |
| set | Collection | x = {1, 2, 3} |
| dict | Mapping | x = {"a": 1} |
| range | Sequence | x = range(5) |
| None | None Type | x = None |
print(type(x))
Integer is a whole number — positive, negative, or zero — without any decimal point. In Python, there is no size limit for integers. They are one of the most commonly used data types in programming.
a = 10 b = -25 c = 0 print(a, b, c) # 10 -25 0 print(type(a)) # <class 'int'>
Float is a number that contains a decimal point. It is used when more precision is needed, such as in scientific calculations or prices. Float values are accurate up to about 15 decimal places.
x = 3.14 y = -9.81 z = 2.0 print(x, y, z) # 3.14 -9.81 2.0 print(type(x)) # <class 'float'>
Complex numbers have two parts — a real part and an imaginary part. The imaginary part is written with the letter j in Python. Complex numbers are mainly used in engineering and scientific computing.
c = 2 + 3j print(c) # (2+3j) print(c.real) # 2.0 print(c.imag) # 3.0 print(type(c)) # <class 'complex'>
A string is a sequence of characters enclosed inside single quotes, double quotes, or triple quotes. Strings are used to store text data. Each character in a string has an index starting from 0.
name = "Ali" city = 'Karachi' print(name[0]) # A print(len(name)) # 3 print(name.upper()) # ALI print(name + " " + city) # Ali Karachi
s = "hello world" print(s.upper()) # HELLO WORLD print(s.capitalize()) # Hello world print(s.replace("hello", "hi")) # hi world print(s.split(" ")) # ['hello', 'world']
f"My name is {name}"
Boolean data type has only two possible values — True or False. Booleans are mainly used in conditions and comparisons. In Python, True equals 1 and False equals 0 in numeric operations.
x = True y = False print(type(x)) # <class 'bool'> print(5 > 3) # True print(5 == 3) # False print(True + True) # 2 (1+1)
A list is an ordered collection used to store multiple items in a single variable. Lists are created using square brackets [ ]. They are mutable — meaning items can be added, removed, or changed after creation.
fruits = ["apple", "banana", "mango"] print(fruits[0]) # apple fruits.append("grape") # Add fruits.remove("banana") # Remove fruits[0] = "orange" # Update print(len(fruits)) # Length
x = [1, "hello", 3.14, True]
A tuple is similar to a list but it is immutable — its values cannot be changed after creation. Tuples are created using round brackets ( ). They are faster than lists and used when data should not be modified.
colors = ("red", "green", "blue") print(colors[0]) # red print(len(colors)) # 3 print(type(colors)) # <class 'tuple'> # This will cause an ERROR: # colors[0] = "yellow" # TypeError!
A set is an unordered collection with no duplicate values. Sets are created using curly braces { }. Since sets are unordered, you cannot access items by index. Sets are useful for removing duplicates and performing math set operations.
nums = {1, 2, 3, 2, 1} print(nums) # {1, 2, 3} — no duplicates a = {1, 2, 3} b = {3, 4, 5} print(a | b) # {1,2,3,4,5} Union print(a & b) # {3} Intersection
{} creates an empty dictionary.
A dictionary stores data as key-value pairs. Each key must be unique and is used to access its value. Dictionaries are created using curly braces { } with a colon : between key and value. They are mutable and unordered.
student = { "name": "Ali", "age": 18, "grade": "A" } print(student["name"]) # Ali student["age"] = 20 # Update print(student.keys()) # all keys print(student.values()) # all values
The range type represents a sequence of numbers. It is mostly used in for loops to repeat a block of code a specific number of times. Range is memory-efficient because it does not store all values at once.
# range(stop) for i in range(5): print(i) # 0 1 2 3 4 # range(start, stop, step) for i in range(1, 10, 2): print(i) # 1 3 5 7 9
None represents the absence of a value — it means "nothing" or "empty". It is Python's equivalent of NULL in other languages. None is often used as a default value for variables or to indicate that a function returned nothing.
x = None print(x) # None print(type(x)) # <class 'NoneType'> if x is None: print("No value assigned")
Type conversion means changing one data type into another. Python provides built-in functions for this. There are two kinds — implicit (automatic) and explicit (manual using functions).
| Function | Converts To | Example |
|---|---|---|
| int() | Integer | int("5") → 5 |
| float() | Float | float(3) → 3.0 |
| str() | String | str(10) → "10" |
| bool() | Boolean | bool(0) → False |
| list() | List | list((1,2)) → [1,2] |
| tuple() | Tuple | tuple([1,2]) → (1,2) |
| set() | Set | set([1,1,2]) → {1,2} |
x = "10" # str y = int(x) # convert to int print(y + 5) # 15 a = 3.99 print(int(a)) # 3 (decimal removed)
| Type | Mutable? | Ordered? | Duplicates? |
|---|---|---|---|
| int | No | — | — |
| float | No | — | — |
| str | No | Yes | Yes |
| bool | No | — | — |
| list | Yes | Yes | Yes |
| tuple | No | Yes | Yes |
| set | Yes | No | No |
| dict | Yes | Yes* | No (keys) |
Comments
Post a Comment