Python PPT G
Python PPT G
Objective:
To understand and work with Python's complex data types like Strings, Lists, Tuples, and
Dictionaries. Learn how to manipulate them and organize reusable code using functions.
STRING DATA TYPE & STRING
OPERATIONS
What is a String?
A string is a sequence of characters and can contain letters, numbers, symbols and even spaces.
It must be enclosed in quotes (‘ ' or ” ") and String is Immutable Data Type.
(or)
A string is a sequence of characters enclosed in single, double, or triple quotes.
String Declaration:
str1 = 'Hello’
str2 = "Python Programming”
str3 = '''This is a multi-line string'''
STRING LENGTH
# Example
text = "Python"
print(len(text)) # Output: 6
String Comparison:
# Example
print("apple" == "Apple") # False
print("abc" < "abd") # True (lexicographically)
Case Conversion:
# Example
text = "Hello World"
print(text.lower()) # hello world
print(text.upper()) # HELLO WORLD
print(text.title()) # Hello World
print(text.capitalize())# Hello world
print(text.swapcase()) # hELLO wORLD
Search Methods:
# Example
text = "Hello Python"
print(text.find("Python")) #6
print(text.index("Hello")) #0
print(text.count("o")) #2
String Immutability:
# Example
text = "Python"
# text[0] = 'J' ❌ This will give error
text = "Java" # ✅ You can reassign, but not modify in place
STRING IMMUTABILITY
text = "Python"
# text[0] = 'J' ❌ This will give error
text = "Java" # ✅ You can reassign, but not modify in place
LISTS AND LIST SLICING
Allow Duplicates:-
Since lists are indexed, lists can have items with the same value:
#EX
list1 = ["apple", "banana", "cherry", "apple", "cherry"]
print(list1)
TABLE OF LIST METHODS
Method Description Example
# code
fruits = ["apple", "banana"]
fruits.append("mango")
print(fruits)
insert(index, value): Add an element at a specific index
#code
#code
# code
fruits = ["apple", "banana", "cherry"]
last_item = fruits.pop()
print("After pop:", fruits)
print("Removed item:", last_item)
pop(index): Removes and returns element at a specific index
# code
fruits = ["apple", "banana", "cherry"]
removed = fruits.pop(1)
print("After pop(1):", fruits)
print("Removed:", removed)
sort(): Sorts the list in ascending order
#code
numbers = [5, 3, 8, 1]
numbers.sort()
print(numbers)
reverse(): Reverses the order of the list
#code
#code
Output:
List length: 3
index(value): Returns the index of the first matching value
#code
#code
my_tuple = ("apple", "banana", "cherry")
print(my_tuple)
Tuple Characteristics:-
#code
fruits = ("apple", "banana", "cherry")
print(fruits)
Output:
('apple', 'banana', 'cherry')
Tuple with mixed data types:-
# code
Output:
('John', 25, True)
TUPLE METHODS
#code
colors = ("red", "green", "blue", "green", "yellow")
index_green = colors.index("green")
print("Index of 'green':", index_green)
Accessing Tuple Elements:-
#code
fruits = ("apple", "banana", "cherry")
print(fruits[1]) # Output: banana
Tuple Slicing:-
#code
fruits = ("apple", "banana", "cherry", "mango")
print(fruits[1:3]) # Slices from index 1 to 2
TUPLE VS LIST
Feature Tuple List
Syntax () []
Mutable ❌ No ✅ Yes
student = {
"name": "Alice",
"age": 20,
"city": "Delhi"
}
print(student["name"]) # Output: Alice
print(student.get("city")) # Output: Delhi
TABLE OF DICTIONARY METHODS
Method Description Example
#code
student = {"name": "Alice", "age": 20, "city": "Delhi"}
print(student.keys())
#code
print(student.get("age")) # 20
print(student.get("gender")) # None (does not crash like student["gender"])
update() – Updates dictionary with new key-value pairs
#code
student.update({"age": 21, "gender": "female"})
print(student)
pop(key) – Removes item with the specified key
#code
student.pop("city")
print(student)
clear() – Removes all key-value pairs
#code
student.clear()
print(student)
TOPIC: PYTHON FUNCTIONS &
ORGANIZING CODE USING FUNCTIONS
Introduction to Functions:- A function is a block of code which only runs when it
is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.
Why Use Functions?
• Improves code readability and organization
• Promotes reusability (write once, use many times)
• Helps in modular programming
• Makes testing and debugging easier
Types of Functions in Python:-
Type Description
#Code
def my_function():
print("Hello from a function")
my_function()
#code
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
Parameter:-
A parameter is a variable defined inside the parentheses in the function definition.
It acts as a placeholder for the value you pass when you call the function.
In your code:
fname is the parameter.
Argument:-
An argument is the actual value passed to the function when it is called.
Arguments are assigned to parameters when the function is executed.
In your code:
"Emil"
# code
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
Arbitrary Arguments, *args:- If you do not know how many arguments that will
be passed into your function, add a * before the parameter name in the
function definition.
This way the function will receive a tuple of arguments, and can access the
items accordingly:
If the number of arguments is unknown, add a * before the parameter name:
def my_function(*kids):
print("The youngest child is " + kids[2])
#code
#code
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Passing a List as an Argument:- You can send any data types of argument to a
function (string, number, list, dictionary etc.), and it will be treated as the
same data type inside the function.
E.g. if you send a List as an argument, it will still be a List when it reaches
the function:
#code
def my_function(food):
for x in food:
print(x)
my_function(fruits)
Return Values:- To let a function return a value, use the return statement.
#code
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))