python data types and its methods a dive in
Integer (int)
Integers are whole numbers.
x = 10
y = 3
Integer Operations
Addition
print(x + y)
Output:
13
Subtraction
print(x - y)
Output:
7
Multiplication
print(x * y)
Output:
30
Division
print(x / y)
Output:
3.3333333333333335
Floor Division
print(x // y)
Output:
3
Modulus
print(x % y)
Output:
1
Power
print(x ** y)
Output:
1000
Integer Methods
Integers have very few methods.
bit_length()
Returns number of bits required.
num = 10
print(num.bit_length())
Output:
4
to_bytes()
print((65).to_bytes(1, 'big'))
Output:
b'A'
from_bytes()
print(int.from_bytes(b'A', 'big'))
Output:
65
Float (float)
Stores decimal values.
price = 99.99
Float Operations
Same arithmetic operators as integers.
a = 10.5
b = 2.5
print(a + b)
print(a - b)
print(a * b)
print(a / b)
Output:
13.0
8.0
26.25
4.2
Float Methods
is_integer()
print((10.0).is_integer())
Output:
True
as_integer_ratio()
print((0.5).as_integer_ratio())
Output:
(1, 2)
hex()
print((10.5).hex())
Complex (complex)
z = 3 + 4j
Operations
a = 2 + 3j
b = 1 + 1j
print(a + b)
print(a - b)
print(a * b)
Attributes
print(z.real)
print(z.imag)
Output:
3.0
4.0
Boolean (bool)
x = True
y = False
Operations
print(x and y)
print(x or y)
print(not x)
Output:
False
True
False
String (str)
Strings have the largest number of commonly used methods.
text = "Python Programming"
String Operations
Concatenation
print("Hello" + " World")
Output:
Hello World
Repetition
print("Hi" * 3)
Output:
HiHiHi
Membership
print("Py" in text)
Output:
True
String Methods
upper()
print(text.upper())
lower()
print(text.lower())
title()
print(text.title())
capitalize()
print(text.capitalize())
swapcase()
print(text.swapcase())
strip()
print(" Hello ".strip())
lstrip()
print(" Hello".lstrip())
rstrip()
print("Hello ".rstrip())
replace()
print(text.replace("Python", "Java"))
split()
print(text.split())
join()
words = ["Python", "Java", "C++"]
print("-".join(words))
find()
print(text.find("P"))
index()
print(text.index("P"))
startswith()
print(text.startswith("Python"))
endswith()
print(text.endswith("ing"))
count()
print(text.count("m"))
isalpha()
print("Python".isalpha())
isdigit()
print("123".isdigit())
isnumeric()
print("123".isnumeric())
isalnum()
print("Python123".isalnum())
islower()
print("python".islower())
isupper()
print("PYTHON".isupper())
center()
print("Python".center(20))
zfill()
print("10".zfill(5))
List (list)
Lists are mutable.
numbers = [10, 20, 30]
List Operations
Concatenation
print([1,2] + [3,4])
Repetition
print([1] * 3)
Membership
print(10 in numbers)
List Methods
append()
numbers.append(40)
extend()
numbers.extend([50, 60])
insert()
numbers.insert(1, 15)
remove()
numbers.remove(20)
pop()
numbers.pop()
clear()
numbers.clear()
copy()
new_list = numbers.copy()
count()
print(numbers.count(10))
index()
print(numbers.index(10))
reverse()
numbers.reverse()
sort()
numbers.sort()
Tuple (tuple)
Immutable sequence.
data = (10, 20, 30)
Tuple Methods
count()
print(data.count(10))
index()
print(data.index(20))
Set (set)
Stores unique values.
s1 = {1, 2, 3}
s2 = {3, 4, 5}
Set Operations
Union
print(s1 | s2)
Intersection
print(s1 & s2)
Difference
print(s1 - s2)
Symmetric Difference
print(s1 ^ s2)
Set Methods
add()
s1.add(10)
update()
s1.update([20, 30])
remove()
s1.remove(1)
discard()
s1.discard(1)
pop()
s1.pop()
clear()
s1.clear()
union()
print(s1.union(s2))
intersection()
print(s1.intersection(s2))
difference()
print(s1.difference(s2))
issubset()
print({1,2}.issubset(s1))
issuperset()
print(s1.issuperset({1}))
Dictionary (dict)
Stores key-value pairs.
student = {
"name": "John",
"age": 21
}
Dictionary Operations
Access
print(student["name"])
Add
student["city"] = "Delhi"
Update
student["age"] = 22
Delete
del student["city"]
Dictionary Methods
get()
print(student.get("name"))
keys()
print(student.keys())
values()
print(student.values())
items()
print(student.items())
update()
student.update({"city":"Delhi"})
pop()
student.pop("age")
popitem()
student.popitem()
clear()
student.clear()
copy()
new_student = student.copy()
setdefault()
student.setdefault("country", "India")
Range (range)
numbers = range(1, 10)
Operations
print(list(numbers))
print(numbers[0])
Bytes (bytes)
data = b"ABC"
Operations
print(data[0])
Output:
65
Bytearray (bytearray)
Mutable version of bytes.
data = bytearray(b"ABC")
data[0] = 68
print(data)
Output:
bytearray(b'DBC')
Frozenset (frozenset)
Immutable set.
fs = frozenset([1, 2, 3])
Operations
print(1 in fs)
NoneType
value = None
print(type(value))
Output:
<class 'NoneType'>
Common Beginner Mistakes
Mistake 1
❌ Wrong
numbers = (1,2,3)
numbers.append(4)
Output:
AttributeError
Reason:
Tuples do not have append().
Mistake 2
❌ Wrong
name = "Python"
name.upper()
print(name)
Output:
Python
Reason:
Strings are immutable.
✅ Correct
name = name.upper()
Mistake 3
❌ Wrong
student["address"]
Output:
KeyError
✅ Better
student.get("address")
Homework 1
Create a list:
[10, 20, 30]
Add:
40
at the end.
Solution
numbers = [10, 20, 30]
numbers.append(40)
print(numbers)
Homework 2
Convert:
"python programming"
into uppercase.
Solution
text = "python programming"
print(text.upper())
Homework 3
Find common values between:
{1,2,3,4}
and
{3,4,5,6}
Solution
s1 = {1,2,3,4}
s2 = {3,4,5,6}
print(s1.intersection(s2))
Output:
{3, 4}
Homework 4
Create a dictionary:
{
"name":"John",
"age":20
}
Add:
"city":"Mumbai"
Solution
student = {
"name":"John",
"age":20
}
student["city"] = "Mumbai"
print(student)
Homework 5
Count the number of occurrences of:
"a"
in:
"banana"
Solution
print("banana".count("a"))
Output:
3
Summary
| Data Type | Common Methods |
|---|---|
| int | bit_length() |
| float | is_integer(), hex() |
| str | upper(), lower(), split(), replace() |
| list | append(), extend(), sort(), remove() |
| tuple | count(), index() |
| set | add(), remove(), union(), intersection() |
| dict | get(), keys(), values(), items() |
| bytes | indexing |
| bytearray | mutable indexing |
| frozenset | set operations only |
| range | indexing, iteration |
| NoneType | no methods |
Master these before learning:
- Operators
- Conditional Statements
- Loops
- Functions
- Exception Handling
- File Handling
- Object-Oriented Programming