0% found this document useful (0 votes)
14 views24 pages

Python Objects and Data Structures Guide

Uploaded by

Hamna
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)
14 views24 pages

Python Objects and Data Structures Guide

Uploaded by

Hamna
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

Semester I, session 2025-26

BBL772

Data Analytics and Informatics for Biotechnology

Practical Lab — day 3


Objects in python

In python everything is an object

a combination of data and functions

These are all different


features, like data and
functions associated with
the variable x
Variable types

Integer object

Float object

String object
Objects have value, ’methods’ and attributes

Specialized functions attached to objects


that can act on that object’s data

Protocol to access a method <Object name>.<method name>()

Protocol to access a attribute

<Object name>.<attribute name>


Here parenthesis is not needed
Lists, Tuples and Arrays

Data structures to organize a batch or series of numbers

List is a built-in data structure in python that can contain anything


A list is a sequence (ordered collection) of objects, endowed with methods that
can perform certain operations on its contents like searching and counting

Data in a list can be changed


Lists have interesting methods
Tuples are immutable ‘list’ objects

Data in a tuple can


not be changed
Immutable!
Numpy array is the most versatile data structure

A list object is generally not the most convenient data type for numerical calculations

A more convenient data structure in python is the numpy array

Here [Link] accepts a single argument


— the tuple (3,5) and creates a ‘ndarray’
object that is a 3x5 matrix
n-dimensional numpy array
numpy array has useful methods
numpy array attributes for size and shape
Sometimes shape of an array can be a tricky affair

Any vector, just a one dimensional


array, so only one index

Column vector, a two


dimensional array, two indices

Row vector, a two dimensional


array, two indices

In certain matrix and vector operations,


the shape of the array is important.
Check if you have the correct shape of
the array. You can reshape arrays also
Filling an array with values

The arange syntax


• [Link](start,end,increment).
• The increment and starting value are
optional.
• The default start value is 0; the default
increment is 1.
• The end value is not included in the array.
Sometimes you might want to include the end value

Use linspace

The linspace syntax


• [Link](start, end, num_points).
• The end value is included in the array.
• You cannot change the spacing

You can use the round method on the array object to


round it o to the decimal point of your liking
ff
The linspace syntax is [Link](start, end, num_point
The end value is included in the array.
Your exercise

our
urn a. Try
2C
a = [Link](0, 10, 2)
b = [Link](0, 10, 6)

and explain the results. See if you can use [Link] and [Link]
to create identical arrays. [Hint: What happens when you add the increment
to the end value in [Link]?]
b. Now try
a = [Link](0, 10, 1.5)
b = [Link](0, 10, 7)

use if you want to evaluate a function over the range 0 to 10.


Describe how a and b differ. Explain why, and decide which form you should

When creating a series to compute the values of a function over a range, np.l
appropriate function. It allows you to specify the start and end of the range (and the nu
If you want to control both spacing and end-value
Useful array operations

Concatenation of arrays Stacking arrays on top or by side

NumPy offers two useful methods for building up an array from smaller ones

• [Link] (horizontal stack): stacks arrays with same number of rows


• [Link] (vertical stack): stacks arrays with same number of columns
Useful array operations

Accessing array elements

Array indices (o sets) are enclosed in square brackets

• Note that the indices in Python start at 0.


• If is a one-dimensional array with elements, the rst element is
A[0] and the th element is A[N-1].
• Asking for element A[N] will result in an error - try this!!
𝐴
𝑁
ff
𝑁
fi
Useful array operations

Arrays and assignments

If multiple variables are bound to the same array, they


can all use its methods to modify its data.

Test this for ordinary


variables that are
not arrays, it does
not work like this!
Useful array operations

Slicing arrays

Often you will wish to extract more than one element from 2.2
an array
Lists, tuples, and arrays 27

2.2.7 Slicing
E.g. Say the rst 20 elements or the 11th column
Often you will wish to extract more than one element from an array. For example, you may wish to
examine the first ten elements of an array or the 17th column of a matrix. These operations are possible
using a technique called slicing. The slicing syntax

The syntax for slicing is a[start:end:stride].


For an array a, do this: a[start:end:stride]. This returns:
In Python, a colon indicates slicing when it is part of an index. Thus, a[start:end:stride]
returns a new array whose entries are
a[start], a[start + stride], a[start + 2*stride], ..., a[start + M*stride]
where M is the largest integer for which start + M*stride < end. The stride comes last. If it is
omitted, the default is 1. If start or end are omitted, their defaults are the first and last indices,
respectively. If the stride is omitted, the second colon may also be omitted.
If you want to see the first ten entries in a, you could use the expression a[0:10:1], or more concisely
a[:10]. The same syntax can be used to slice along each dimension of a multidimensional array. For
example, to get a slice of the third column of a matrix, you could use A[start:end:stride,2].
Suppose that you have been given experimental data in an array A with two columns. For your analysis,
you need to split the two columns of data into separate arrays. If you don’t know the number of rows in A,
fi
Slicing examples

Note again that the


by default indices
start at 0.
Floating point error in python

As it is known that 1.2 – 1.0 = 0.2


But when you try to the same in python you will surprised by results:

Is this a bug in python? What do you think?

This has to do with — oating point calculation

Basically how the underlying system treats decimal numbers


fl
Floating point error in python

Decimal numbers are converted into binary and stored with a xed
precision — typically 53 digits. This creates a round-o error

That way 1.2 becomes

1.0011001100110011001100110011001100110011001100110011

Which is exactly equal to

1.1999999999999999555910790149937383830547332763671875

So, when you subtract 1.0, it gives back 0.19999999…..

ff
fi
Floating point error in python

What can be a practical way to handle this?

Round it o to required decimal points

The ‘round’ function can perform this apparent miracle for you!
ff
Use-de ned functions

• You might even de ne some functions to take more than one


argument and give back one value

Can you tell what the function f is doing here?

Note, the order of


entering the arguments
does not matter since
they are mentioned
explicitly
fi
fi

You might also like