Python Basics Workshop
Welcome to the Python Basics Workshop. In this session, we will learn very important building
blocks of Python programming:
1. Strings
📘 Introduction to Strings
A string is a sequence of characters written inside quotes. It can include letters, numbers,
symbols, or even spaces.
Strings are used to store text in Python.
You can use single quotes ('...') or double quotes ("...").
Strings are very common because almost all programs deal with text (names, messages, data,
etc.).
text = "Hello, World!"
print(text)
🔠 String Indexing & Slicing
A string is a sequence of characters. We can access each character using its index (position
number).
Python indexing starts from 0.
Negative indexes count from the end (-1 is last character).
indexing through String
print(text[3])
print(text[-3]) # Negative indexing
print(text[0])
Slicing
print(text[0:5])
print(text[1:3])
print(text[0:])
print(text[:5])
Concatenation
🔗 String Concatenation
Concatenation means joining two or more strings together using the + operator.
first_name = "Arooj "
last_name = "Fatima"
full_name = first_name + last_name
print(full_name)
#Concatenation with spaces
full_name = first_name + " " + last_name
print(full_name)
# Concatenating with another string
greeting = "Hello, " + full_name + "!"
print(greeting)
2. String Methods
Python provides many built-in methods (functions) that can be used with strings. These
methods help us modify, analyze, or clean text easily.
Some useful string methods are:
.lower() → Converts all letters into small letters
.upper() → Converts all letters into capital letters
.replace(old, new) → Replaces one part of the string with another
.strip() → Removes spaces from the beginning and end
len() → Finds the length (number of characters) in the string
word = " Python Basics "
#.lower() → Converts all letters into small letters
print(word.lower())
#.upper() → Converts all letters into capital letters
print(word.upper())
#.replace(old, new) → Replaces one part of the string with another
print(word.replace("Python", "Coding"))
#.strip() → Removes spaces from the beginning and end
print(word.strip())
#len() → Finds the length (number of characters) in the string
print(len(word))
3. Lists
📋Introduction to Lists
A list is a collection of items stored in one variable.
Lists can hold numbers, strings, or even a mix of both.
Lists are written inside square brackets [ ].
Each item in a list has an index (position number) starting from 0.
We can access, slice, add, and remove items in a list.
fruits = ["apple", "banana", "cherry"]
print(fruits)
➕ Adding Items
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # add at the end
print(fruits)
fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "mango") # add at a specific position
print(fruits)
❌ Removing Items
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana") # remove by value
print(fruits)
fruits = ["apple", "banana", "cherry"]
fruits.pop(1) # remove by index
print(fruits)
fruits = ["apple", "banana", "cherry"]
fruits.clear() # remove all items
print(fruits)
other useful methods
numbers = [3, 1, 4, 1, 5, 9]
numbers.sort() # sort ascending
print(numbers)
numbers = [3, 1, 4, 1, 5, 9]
numbers.reverse() # reverse order
print(numbers)
fruits = ["apple", "banana", "cherry"]
print(len(fruits)) # number of items in list
4. Tuple
📌 Tuples in Python
A tuple is like a list, but it is unchangeable (immutable). This means once a tuple is created, you
cannot add, remove, or change its items.
Tuples are written with round brackets ().
1. Creating a Tuple
fruits = ("apple", "banana", "cherry")
print(fruits)
1. Accessing Items
fruits = ("apple", "banana", "cherry")
print(fruits[0]) # first item
print(fruits[-1]) # last item
1. Slicing a Tuple
fruits = ("apple", "banana", "cherry", "mango", "orange")
print(fruits[1:4]) # from index 1 to 3
1. Tuple with Different Data Types
my_tuple = ("Ali", 25, 5.9, True)
print(my_tuple)
👉 Tuples are useful when you want to store data that should not be changed.
5. Dictionaries
📖 Dictionaries
A dictionary in Python is used to store data in key–value pairs.
Keys are like labels (unique).
Values are the data stored under those labels.
Dictionaries are written inside curly braces { }.
# Creating a dictionary
student = {
"name": "Arooj",
"age": 20,
"grade": "A"
}
# Accessing values
print(student["name"])
print(student["age"])
# Updating a value
student["grade"] = "A+"
print(student)
# Adding a new key-value pair
student["city"] = "BAHAWALPUR"
print(student)
# Removing a key-value pair
student.pop("age")
print(student)
6. SETS
🔹 Sets in Python
A set is a collection in Python that:
Stores unique items only (no duplicates).
Is unordered → items do not have a fixed position or index.
Is written with curly brackets {}.
1. Creating a Set
fruits = {"apple", "banana", "cherry"}
print(fruits)
1. No Duplicates Allowed
fruits = {"apple", "banana", "cherry", "apple"}
print(fruits) # "apple" will appear only once
1. Adding Items
fruits = {"apple", "banana"}
fruits.update(["mango", "orange"])
print(fruits)
1. Adding Multiple Items
fruits = {"apple", "banana"}
fruits.update(["mango", "orange"])
print(fruits)
1. Removing Items
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana") # removes banana
print(fruits)
1. Set Operations (Union & Intersection)
set1 = {"apple", "banana", "cherry"}
set2 = {"banana", "mango", "orange"}
print(set1.union(set2)) # all unique items
print(set1.intersection(set2)) # common items
7. Logical Operators
⚖️Logical Operators in Conditions
Logical operators let us combine conditions inside if statements.
and → True only if both conditions are True
or → True if at least one condition is True
not → Reverses the result (True becomes False, False becomes True)
1. Using and
age = 20
has_id = True
if age >= 18 and has_id:
print("You can enter.")
else:
print("Access denied.")
1. Using or
age = 16
has_parent = True
if age >= 18 or has_parent:
print("You can enter.")
else:
print("Access denied.")
1. Using not
is_raining = False
if not is_raining:
print("You can go outside.")
else:
print("Stay inside.")
8. If-Else Statements
🔎 If – Elif – Else Statements
In Python, we use if–elif–else to make decisions.
if → checks the first condition
elif → checks another condition (if the first one is False)
else → runs when no condition is True
age = 18
if age < 13:
print("You are a child.")
elif age < 20:
print("You are a teenager.")
else:
print("You are an adult.")
marks = 75
if marks >= 80:
print("Grade: A")
elif marks >= 60:
print("Grade: B")
else:
print("Grade: C")
9. Final Challenge 🎯
Task for Students:
Create a simple Student Report Card System using everything you learned:
1. Store student details (name, age, marks) in a dictionary.
2. Use if-else to assign a grade based on marks.
3. Print all student information clearly.
👉 Try this yourself! (No solution provided here — you will do it live in class.)