lOMoAR cPSD| 19616291
Introduction to Python Programming BCSPLC215B
Bapuji Educational Association ®
Bapuji Institute of Engineering and Technology, Davangere–577 004
Post Box No. :325, Davanagere -577 004, Karnataka, India.
An Autonomous Institute Affiliated to Visvesvaraya Technological University, Belagavi, Karnataka.
Approved by AlCTE, New Delhi | Accredited by NAAC with 'A' grade and NBA (UG Programmes)
Recognized by UGC,New Delhi under 2(F) and 12(B)
Introduction to Python Programming
(BPLCK105B/205B)
MODULE – 2
STUDY MATERIAL
SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.
lOMoAR cPSD| 19616291
Introduction to Python Programming BCSPLC215B
CHAPTER 4: FUNCTIONS
4.1 Functions A function is a block of reusable code that performs a specific task.
It helps in reducing repetition and makes programs modular and organized.
Syntax
Example 1 (simple)
Output: Hello Students
Example 2 (with Parameters/arguments)
Output: 7
Example 3 (with Return Value)
Output: 25
Types of Functions: Built-in Functions → Already available (e.g., print(), len()) and
User-defined Functions → Created by the programmer.
4.2 Functions can call other functions: In Python, a function can call another function
inside its body. This is called a function call within a function.
SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.
lOMoAR cPSD| 19616291
Introduction to Python Programming BCSPLC215B
Output: Hello
Welcome to Python
4.3 Flow of execution
The flow of execution refers to the order in which statements are executed in a
program.
Example
Output: Start
Inside fun2
Inside fun1
End
SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.
lOMoAR cPSD| 19616291
Introduction to Python Programming BCSPLC215B
CHAPTER 5: STRINGS
5.1 Strings
A string is a sequence of characters enclosed in quotes. Strings can be written using:
5.2 Working with Strings as Single Values
A string can be treated as a single unit (object)
5.3 Working with Parts of a String
Use indexing and slicing to access parts.
Positive indexing → Left to Right (starts from 0)
Negative indexing → Right to Left (starts from -1)
Positive indexing
0 1 2 3 4 5
b a n a n a
-6 -5 -4 -3 -2 -1
Output n Negative indexing
n
b character can be addressed using positive indexing s[0] as well as negative indexing
s[-1]. Similarly, all characters also can be addressed using both indexing methods.
String Slicing
Output
SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.
lOMoAR cPSD| 19616291
Introduction to Python Programming BCSPLC215B
5.4 Length
The len() function, when applied to a string, returns the number of characters in a string
5.5 Traversal and the for loop
Many computations involve processing a string one character at a time. Often, they start
at the beginning, select each character in turn, do something to it, and continue until the
end. This pattern of processing is called a traversal.
Traversal using while loop Traversal using for loop
while loop traverses the string and displays each letter on a line by itself. It uses ix for
the index, which does not make it any clearer. The loop condition is ix < len(fruit), so
when ix is equal to the length of the string, the condition is false, and the body of the
loop is not executed. The last character accessed is the one with the index len(fruit)-1,
which is the last character in the string. However, this code is a lot longer than it needs
to be, and not very clear at all.
The for loop can easily iterate over the elements in a list and it can do so for strings.
Each time through the loop, the next character in the string is assigned to the variable c.
The loop continues until no characters are left. Here we can see the expressive power the
for loop gives us compared to the while loop when traversing a string.
5.6 String comparison
1. Equality Comparison (==)
Used to check if two strings are exactly equal
Output
Yes, we have no bananas!
SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.
lOMoAR cPSD| 19616291
Introduction to Python Programming BCSPLC215B
2. Lexicographical Comparison (Alphabetical Order)
• Strings are compared character by character and based on ASCII/Unicode values
Output
Your word, apple, comes before banana.
Note: Uppercase letters come before lowercase letters
Output
Your word, Zebra , comes before banana.
ASCII value of 'Z' is less than 'b', so "Zebra" < "banana" is True.
More Examples
Output
5.7 Strings are immutable
Strings are immutable, which means once a string is created, it cannot be changed.
SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.
lOMoAR cPSD| 19616291
Introduction to Python Programming BCSPLC215B
5.8 The in and not in operators
The in operator is used to check membership. When used with strings, it checks whether
one string is a substring of another string.
The not in operator returns the logical opposite of in:
5.9 A find function
find() is the opposite of the indexing operator. Instead of taking an index and extracting
the corresponding character, it takes a character and finds the index where that character
appears. If the character is not found, the function returns -1.
Example using user-defined my_find() function
Output: 1
Example using the built-in find() function
Output: 1
The built-in find method
SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.
lOMoAR cPSD| 19616291
Introduction to Python Programming BCSPLC215B
5.10 Looping and counting
Looping and counting is used to count how many times a specific character appears in
a string.
Output: True
5.11 Optional parameters
An optional parameter is a parameter that has a default value. If no value is given
during a function call, the default value is used.
• Search starts from index 2
• "banana" → b a n a n a
• From index 2 → "nana"
• First 'a' found at index 3
Better still, we can combine find and find2 using an optional parameter:
When a function has an optional parameter, the caller may provide a matching argument.
If the third argument is provided to find, it gets assigned to start. But if the caller leaves
the argument out, then start is given a default value indicated by the assignment start=0
in the function definition.
5.12 The split method
The split() method splits a string into a list.
Example 1:
txt = "welcome to the jungle"
x = [Link]()
print(x)
Output: ['welcome', 'to', 'the', 'jungle']
SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.
lOMoAR cPSD| 19616291
Introduction to Python Programming BCSPLC215B
Example 2:
Example 3:
txt = "hello, my name is Peter, I am 26 years old"
x = [Link](", ")
print(x)
Output: ['hello', 'my name is Peter', 'I am 26 years old']
Example 4:
txt = "apple#banana#cherry#orange"
x = [Link]("#")
print(x)
Output: ['apple', 'banana', 'cherry', 'orange']
5.2 Tuples
Tuples are used to store multiple items of various types in a single variable.
• A tuple is an ordered collection. When we say that tuples are ordered, we mean
that their items have a defined order that will not change.
• A tuple is a collection is unchangeable or immutable. Tuples are
unchangeable, meaning that we cannot change, add or remove items after the
tuple has been created.
• Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.
Example 1:
Tuple name is year_born which groups name + year values together.
Example 2:
A tuple can store multiple types of data
Example 3:
t = "apple", "banana", "cherry"
print(t)
Output: ('apple', 'banana', 'cherry')
SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.
lOMoAR cPSD| 19616291
Introduction to Python Programming BCSPLC215B
Example 4:
If an index number is given, its value will be displayed.
5.2.1 Tuple assignment
Tuple assignment allows multiple variables to be assigned values at once using a tuple.
Output: Julia
Duplicity
Note: Values from the tuple are assigned to variables one by one
Rule is Number of variables = Number of values in tuple
Tuple Packing
Tuple unpacking
5.2.3 Tuples as return values
A function can return only one value. But that value can be a tuple, allowing us to
return multiple results together.
Output: (31.4159..., 78.5398...)
SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.
lOMoAR cPSD| 19616291
Introduction to Python Programming BCSPLC215B
5.2.4 Composability of Data Structures
Composability means: We can combine different data structures together. One data
structure can contain another.
Outer structure → List
Each element → Tuple
Inside tuple → List of subjects
Visual representation:
SHEIK IMRAN Assistant professor, Dept. of IS&E, BIET, Davangere.