0% found this document useful (0 votes)
15 views23 pages

Data Science - Sec2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views23 pages

Data Science - Sec2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Data

Science

Section2
String Operations
and functions
String operations and functions (cont.)
➢ To get the length of a string, use the len()
function.
➢ find() Searches the string for a specified
value and returns the position of where it was
found.
✓Note : Both index() and find() are identical in that
they return the index position of the first occurrence
of the substring from the main string.
✓The main difference is that Python find() produces
-1 as output if it is unable to find the substring,
whereas index() throws a ValueError exception.
String operations and functions (cont.)
➢ replace() Returns a string where a
specified value is replaced with a
specified value.
✓Note: this function not modified the original
variable.
➢ count() Returns the number of times a
specified value occurs in a string.
➢ F-strings provide a concise and
convenient way to embed python
expressions inside string literals for
formatting.
While loop
iterations
➢Iteration means executing the same block of code over and over,
potentially many times. A programming structure that implements
iteration is called a loop.
➢Example of basic while loop syntax:

while <expr>:
<statement(s)>

<statement(s)>: represents the block to be repeatedly executed,


often referred to as the body of the loop.
Nested while loop example
Break and continue statements
➢Python provides two keywords that terminate a loop iteration
prematurely:
• With the break statement we can stop the loop even if the while
condition is true.
• With the continue statement we can stop the current iteration and
continue with the next.
For loop
For loop
➢A for loop is used for iterating over a sequence (that is either a
list, a tuple, a dictionary, a set, or a string).
For loop (cont.)
➢Looping Through a List :

➢Looping Through a String :


For loop (cont.)
➢The range() Function
✓To loop through a set of code a specified number of
times, we can use the range() function.
✓The range() function returns a sequence of numbers,
starting from 0 by default, and increments by 1 (by
default), and ends at a specified number.
✓Note that range(5) is not the values of 0 to 5, but the
values 0 to 4.
✓range(2, 6), which means values from 2 to 6 (but not
including 6).
✓The range() function defaults to increment the
sequence by 1, however it is possible to specify the
increment value by adding a third parameter: range(2,
20, 2).
Functions
Function
➢In python, a function is a group of related statements that
performs a specific task.
➢Functions help break our program into smaller chunks.
➢As our program grows larger and larger, functions make it more
organized and manageable.
➢Furthermore, it avoids repetition and makes the code reusable.
➢Syntax of function :
Function Example
➢In this example we have defined a simple function named
my_function that print a message, then we call the function
using its name.

➢Keyword def that marks the start of the function header.


➢A function name to uniquely identify the function.
➢Parameters (arguments) through which we pass values to
function.
Parameters or Arguments?
➢The terms parameter and argument can
be used for the same thing: information
that are passed into a function.
➢A parameter is the variable listed inside
the parentheses in the function definition.
➢An argument is the value that is sent to
the function when it is called.
➢Parameters are optional.
➢Return statement is optional.
Functions (cont.)
➢Default Parameter Value
▪ The following example shows how to use a
default parameter value.
▪ If we call the function without argument, it
uses the default value:
➢Return multiple values
Functions (cont.)
➢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.
➢Lamda function:
▪ A lambda function is a small anonymous
function.
▪ A lambda function can take any number of
arguments but can only have one expression.
Modules
Module
➢A file containing a set of functions
you want to include in your
application.
➢we can use any module by using the
import statement.
➢There are several built-in modules in
Python, which you can import
whenever you like.
➢We can use dir() function to see all
the functions that are present in a
module
Math Module
Math Module
➢The Python math module is an important feature designed to deal
with mathematical operations. It comes packaged with the
standard Python release.
➢Since the math module comes packaged with the python release,
you don’t have to install it separately.
➢Importing the module :
➢Constants are available in math module:
▪ Pi (math.pi)
▪ Tau (math.tau)
▪ Euler’s Number (math.e)
▪ infinity
Math Module Functions
➢Some Arithmetic Functions:
➢ math.factorial() :Returns the factorial of a number.
➢ math.ceil() : Rounds a number up to the nearest integer.
➢ math.floor() :Rounds a number down to the nearest integer.
➢ Math.trunc() : Returns the truncated integer parts of a float number.
➢Power and logarithmic functions:
➢ math.pow(x,y) :Returns the value of x to the power of y.
➢ math.exp(x) : Returns E raised to the power of x.
➢ math.log(x) : base is e.
➢ math.log10(x): base is 10.
➢ math.log2(x): base is 2.
➢ math.log(x,y): base is y.
➢ math.sqrt(x) :Returns the square root of a number x.
➢ https://www.w3schools.com/python/module_math.asp

You might also like