Combining Strings
first_name = “John”
last_name = “Doe”
full_name = first_name + ” ” + last_name
print(full_name)
Output:
John DoeString Length
name = “Python”
print(len(name))
Output:
6
Accessing Characters
word = “Python”
print(word[0])
print(word[1])
Output:
P
y
Real Example
name = “Kevin”
print(“Welcome ” + name)
Output:
Welcome Kevin
4. List (list)
A list stores multiple items in order.
fruits = [“Apple”, “Banana”, “Orange”]
Accessing List Items
fruits = [“Apple”, “Banana”, “Orange”]
print(fruits[0])
print(fruits[1])Output:
Apple
Banana
Adding Items
fruits = [“Apple”, “Banana”]
fruits.append(“Orange”)
print(fruits)
Output:
[‘Apple’, ‘Banana’, ‘Orange’]
Removing Items
fruits = [“Apple”, “Banana”, “Orange”]
fruits.remove(“Banana”)
print(fruits)
Output:
[‘Apple’, ‘Orange’]
Looping Through a List
fruits = [“Apple”, “Banana”, “Orange”]
for fruit in fruits:
print(fruit)
Output:
Apple
Banana
Orange
Real Example
cart = [“Shoe”, “Bag”, “Watch”]
print(“Items in cart:”)for item in cart:
print(item)
Output:
Items in cart:
Shoe
Bag
Watch
5. Dictionary (dict)
A dictionary stores data as key-value pairs.
user = {
“name”: “Kevin”,
“age”: 25,
“city”: “Lagos”
}
Accessing Dictionary Values
user = {
“name”: “Kevin”,
“age”: 25
}
print(user[“name”])
print(user[“age”])
Output:
Kevin
25
Adding New Data
user = {
“name”: “Kevin”
}
print(user)
Output:
{
‘name’: ‘Kevin’,
Updating Data
user = {
“name”: “Kevin”,
“age”: 20
}
user[“age”] = 25
print(user)
Output:
{
‘name’: ‘Kevin’,
‘age’: 25
}
Looping Through Dictionary
user = {
“name”: “Kevin”,
“age”: 25
}
for key, value in user.items():
print(key, value)
Output:
name Kevin
age 25
Real Example
product = {
“name”: “Nike Shoe”,
“price”: 50000,
“stock”: 20
}
print(product[“name”])
print(product[“price”])
Output:
Nike Shoe
500006. Set (set)
A set stores unique values only.
numbers = {1, 2, 3, 4}
Duplicate Values Are Removed
numbers = {1, 2, 2, 3, 3, 4}
print(numbers)
Output:
{1, 2, 3, 4}
Adding Items
numbers = {1, 2, 3}
numbers.add(4)
print(numbers)
Output:
{1, 2, 3, 4}
Removing Items
numbers = {1, 2, 3, 4}
numbers.remove(2)
print(numbers)
Output:
{1, 3, 4}
Real Example
Imagine users like products:
likes = {
“Nike Shoe”,“Nike Shoe”,
“iPhone”,
“Watch”
}
print(likes)
Output:
{‘Nike Shoe’, ‘iPhone’, ‘Watch’}
The duplicate “Nike Shoe” appears only once.
Type Checking
You can check the type of any variable using type().
name = “Kevin”
age = 25
fruits = [“Apple”, “Banana”]
print(type(name))
print(type(age))
print(type(fruits))
Output:
<class ‘str’>
<class ‘int’>
<class ‘list’>