Python Data Handling
Python Data Handling for Beginners
In Python, data handling means storing, changing, and using data in different ways.
In this post, you will learn:
-
Lists
-
Tuples
-
Dictionaries
-
Sets
-
String Methods
-
Type Conversion
1. Python Lists :Lists store many values in one variable.
fruits = ["apple", "banana", "mango"]
| Method | Use |
|---|---|
append() | Add item |
remove() | Remove item |
pop() | Remove last item |
insert() | Add at position |
sort() | Sort list |
reverse() | Reverse list |
len() | Count items |
Example:
fruits.append("orange")
fruits.remove("banana")
fruits.sort()
print(fruits)
2. Python Tuples :Tuples are like lists, but cannot be changed.
colors = ("red", "green", "blue")
| Method | Use |
|---|---|
count() | Count value |
index() | Find position |
Example:
print(colors.count("red"))
Example:
print(colors.index("blue"))
3. Python Dictionaries : Dictionaries store data in keyvalue pairs.
student = {"name":"Bency","age":26}
| Method | Use |
|---|---|
keys() | All keys |
values() | All values |
items() | Key-value pairs |
get() | Safe access |
update() | Add/update |
pop() | Remove |
student.update({"course":"Python"})
print(student.keys())
4. Python Sets : Sets store unique values.
nums = {1,2,3,4}
| Method | Use |
|---|---|
add() | Add item |
remove() | Remove |
union() | Combine |
intersection() | Common |
difference() | Difference |
Example:
a={1,2,3}
b={3,4,5}
print(a.union(b))
5. Python String Methods
text = "python programming"
| Method | Use |
|---|---|
upper() | Uppercase |
lower() | Lowercase |
capitalize() | First letter |
title() | Each word |
strip() | Remove spaces |
replace() | Replace |
split() | Split words |
find() | Find position |
Example:
print(text.upper())
print(text.split())
6. Python Type Conversion :Convert one type to another.
| Function | Use |
|---|---|
int() | To integer |
float() | To float |
str() | To string |
list() | To list |
tuple() | To tuple |
set() | To set |
Example:
x = "10"
y = int(x)
print(y + 5)
Now you know how to store, modify, and convert data in Python.
Comments
Post a Comment