Data Science - Module 2_ String Methods & Control Flow
Data Science - Module 2_ String Methods & Control Flow
2 At the start of all classes, please rename yourselves to: Name + Last
3 digits and letter of your NRIC. Example: John Tan (123A)
String Methods
Python has a set of built-in methods that you can use on strings
● split()
● lower()
● upper()
● isnumeric()
● join()
● len()
String Indexing
String Indexing
Python, strings are ordered sequences of character data. Individual characters in a string can be accessed by specifying the
string name followed by a number in square brackets [ ] .
H E L L O W O R L D
0 1 2 3 4 5 6 7 8 9 10
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
String Indexing
In Python, strings are ordered sequences of character data. Individual characters in a string can be accessed by specifying
the string name followed by a number in square brackets [ ] .
‘H’ H E L L O W O R L D
0 1 2 3 4 5 6 7 8 9 10
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
String Indexing
In Python, strings are ordered sequences of character data. Individual characters in a string can be accessed by specifying
the string name followed by a number in square brackets [ ] .
’W’ H E L L O W O R L D
0 1 2 3 4 5 6 7 8 9 10
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
String Indexing
In Python, strings are ordered sequences of character data. Individual characters in a string can be accessed by specifying
the string name followed by a number in square brackets [ ] .
’HELLO’ H E L L O W O R L D
0 1 2 3 4 5 6 7 8 9 10
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
© 2024 Vertical Institute
STRING INDEXING
String Indexing
In Python, strings are ordered sequences of character data. Individual characters in a string can be accessed by specifying
the string name followed by a number in square brackets [ ] .
>>>s[0:6:2] or s[-11:-6:2]
‘HLO’ H E L L O W O R L D
0 1 2 3 4 5 6 7 8 9 10
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
© 2024 Vertical Institute
MODULE 2: STRING METHODS & CONTROL FLOW
String Concatenation
A common task you’ll need to accomplish that involves merging or combining string.
>>> a = ‘hello’
>>> b = ‘world’
>>> a + b
‘Helloworld’
.join() : Returns a string by joining all the elements of an iterable (list, string, tuple), separated by a string separator
F-Strings
Also known as ‘formatted string literals’, f-strings are string literals that have an f at the beginning and curly braces
containing expressions that will be replaced with their values
Python Iterations,
Control Flow, and
Functions
Conditional Statements
However, the world is often more complicated than that. Most of the time, a program needs to skip over some statements,
execute a series of statements repetitively, or choose between alternate sets of statements to execute.
This is where the control structures come in. A control structure directs the order of execution of the statements in a
program (referred to as the program’s control flow)
Python If Statements
If…else
Used for decision-making operations. It contains a body of code which runs when
condition given in the if statement is true. If the condition is false, the optional else statement will then run.
Syntax Example
If expression: a = 10
statement(s) if a > 5:
else: print(“This is more than 5”)
statement(s) else:
print(“This is less than 5”)
‘This is more than 5’
© 2024 Vertical Institute
CONDITIONAL STATEMENTS
Python If Statements
If…elif…else
Allows you to check multiple expressions for True and executes a block of code as soon as one of the condition is evaluates
to True
Syntax Example
If expression: a = 10
statement(s) if a > 15:
elif: print(“This is more than 15”)
statement(s) elif a > 9 :
else: print(“This is more than 9”)
statement(s) else:
print(“This is less than 5”)
© 2024 Vertical Institute ‘This is more than 9’
MODULE 2: STRING METHODS & CONTROL FLOW
Loops
For Loops
A for loop is used for iterating over a sequence (that is either a list, tuple, dictionary, set or a string. Loops are important in
important as they execute a block of code repeatedly.
Range ( )
● Returns the sequence of the given number between the given range
● When user call range() with one argument, user will get a series of numbers that starts at 0
and includes every whole number up to, but not including, the number that user have provided as the stop.
● Three ways you can call range():
Expression Output
for i in 01234
range(5):
print(i)
for i in 1234
range(1,5):
print(i)
for i in range(0, 30, 0 3 6 9 12 15 18 21 24 27
© 2024 Vertical Institute 3): print(i)
LOOPS
Functions
Python Functions
A function is a set of statements that takes input, do some specific computation and
produce output.
The main idea is to put some commonly or repeatedly done tasks together and make
a function so that instead of writing the same code again and again for different
inputs.
User-defined functions: The user-defined functions are those defined by the user to
perform a specific task
● Using functions, we can avoid rewriting the same logic/code again and again
● We can call Python functions multiple times in a program and anywhere in a program
● We can track a large Python program easily when it is divided into multiple functions
● Reusability is the main achievement of Python functions
● However, function calling is always an overhead in a python program
Thank you!