0% found this document useful (0 votes)
117 views53 pages

Essential Software: Dr. Muhammad Yousuf Tufail

The document discusses representing family member height data in list format. It shows an example heights list with names and measurements. It demonstrates how to access specific elements in the list by index and provides an alternative way using negative indexing. Slicing is also introduced to extract a subset of elements from the start to a given point in the list.

Uploaded by

Saadat Shaikh
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)
117 views53 pages

Essential Software: Dr. Muhammad Yousuf Tufail

The document discusses representing family member height data in list format. It shows an example heights list with names and measurements. It demonstrates how to access specific elements in the list by index and provides an alternative way using negative indexing. Slicing is also introduced to extract a subset of elements from the start to a given point in the list.

Uploaded by

Saadat Shaikh
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/ 53

Essential Software

Dr. Muhammad Yousuf Tufail

Department of Mathematics
Institute of Business Administration.
‡ Yousuf Tufail ¯ Yousuf Tufail
ytufail@iba.edu.pk

Lecture 4

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 1/8


Lists with in a list

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 2/8


Lists with in a list
It is better to represent the heights information in the form of lists with in
a list format, as below:

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 2/8


Lists with in a list
It is better to represent the heights information in the form of lists with in
a list format, as below:
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 2/8


Lists with in a list
It is better to represent the heights information in the form of lists with in
a list format, as below:
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
It is one of the best possible way to represent the heights information in
the form of lists.

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 2/8


Lists with in a list
It is better to represent the heights information in the form of lists with in
a list format, as below:
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
It is one of the best possible way to represent the heights information in
the form of lists.
Example (1)
Find first and third elements in the list

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 2/8


Lists with in a list
It is better to represent the heights information in the form of lists with in
a list format, as below:
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
It is one of the best possible way to represent the heights information in
the form of lists.
Example (1)
Find first and third elements in the list

Solution

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 2/8


Lists with in a list
It is better to represent the heights information in the form of lists with in
a list format, as below:
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
It is one of the best possible way to represent the heights information in
the form of lists.
Example (1)
Find first and third elements in the list

Solution
print(fam heights new[0])

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 2/8


Lists with in a list
It is better to represent the heights information in the form of lists with in
a list format, as below:
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
It is one of the best possible way to represent the heights information in
the form of lists.
Example (1)
Find first and third elements in the list

Solution
print(fam heights new[0])
[’John’, 1.64]

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 2/8


Lists with in a list
It is better to represent the heights information in the form of lists with in
a list format, as below:
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
It is one of the best possible way to represent the heights information in
the form of lists.
Example (1)
Find first and third elements in the list

Solution
print(fam heights new[0])
[’John’, 1.64]
print(fam heights new[3])

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 2/8


Lists with in a list
It is better to represent the heights information in the form of lists with in
a list format, as below:
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
It is one of the best possible way to represent the heights information in
the form of lists.
Example (1)
Find first and third elements in the list

Solution
print(fam heights new[0])
[’John’, 1.64]
print(fam heights new[3])
[’dad’, 1.80]

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 2/8


Lists with in a list
It is better to represent the heights information in the form of lists with in
a list format, as below:
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
It is one of the best possible way to represent the heights information in
the form of lists.
Example (1)
Find first and third elements in the list

Solution
print(fam heights new[0])
[’John’, 1.64]
print(fam heights new[3])
[’dad’, 1.80]

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 2/8


Subsetting lists and slicing

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 3/8


Subsetting lists and slicing
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 3/8


Subsetting lists and slicing
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
There are two possible ways to call an element from the list which is
explained as below:

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 3/8


Subsetting lists and slicing
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
There are two possible ways to call an element from the list which is
explained as below:

Example (2)
Find the fourth element from the given list using python.

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 3/8


Subsetting lists and slicing
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
There are two possible ways to call an element from the list which is
explained as below:

Example (2)
Find the fourth element from the given list using python.

Solution

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 3/8


Subsetting lists and slicing
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
There are two possible ways to call an element from the list which is
explained as below:

Example (2)
Find the fourth element from the given list using python.

Solution
print(fam heights new[3])

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 3/8


Subsetting lists and slicing
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
There are two possible ways to call an element from the list which is
explained as below:

Example (2)
Find the fourth element from the given list using python.

Solution
print(fam heights new[3])
[’dad’, 1.80]

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 3/8


Subsetting lists and slicing
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
There are two possible ways to call an element from the list which is
explained as below:

Example (2)
Find the fourth element from the given list using python.

Solution
print(fam heights new[3])
[’dad’, 1.80]

An alternative way to get the above results is as below:

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 3/8


Subsetting lists and slicing
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
There are two possible ways to call an element from the list which is
explained as below:

Example (2)
Find the fourth element from the given list using python.

Solution
print(fam heights new[3])
[’dad’, 1.80]

An alternative way to get the above results is as below:


print(fam heights new[-1])

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 3/8


Subsetting lists and slicing
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
There are two possible ways to call an element from the list which is
explained as below:

Example (2)
Find the fourth element from the given list using python.

Solution
print(fam heights new[3])
[’dad’, 1.80]

An alternative way to get the above results is as below:


print(fam heights new[-1])# An alternative way

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 3/8


Subsetting lists and slicing
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
There are two possible ways to call an element from the list which is
explained as below:

Example (2)
Find the fourth element from the given list using python.

Solution
print(fam heights new[3])
[’dad’, 1.80]

An alternative way to get the above results is as below:


print(fam heights new[-1])# An alternative way
[’dad’, 1.80]
Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 3/8
Subsetting lists and slicing
fam heights new=[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”,
1.80]]
There are two possible ways to call an element from the list which is
explained as below:

Example (2)
Find the fourth element from the given list using python.

Solution
print(fam heights new[3])
[’dad’, 1.80]

An alternative way to get the above results is as below:


print(fam heights new[-1])# An alternative way
[’dad’, 1.80]
Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 3/8
Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:
Example (3)
Find first three values in the list.?

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:
Example (3)
Find first three values in the list.?
Remember,

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:
Example (3)
Find first three values in the list.?
Remember, Remember,

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:
Example (3)
Find first three values in the list.?
Remember, Remember, Remember!

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:
Example (3)
Find first three values in the list.?
Remember, Remember, Remember!

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:
Example (3)
Find first three values in the list.?
Remember, Remember, Remember!

When you write [0:n] for the selection in python

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:
Example (3)
Find first three values in the list.?
Remember, Remember, Remember!

When you write [0:n] for the selection in python, it means 0 (which
represent the first position) is included.

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:
Example (3)
Find first three values in the list.?
Remember, Remember, Remember!

When you write [0:n] for the selection in python, it means 0 (which
represent the first position) is included. But n (which represent the n+1
position) is not included.

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:
Example (3)
Find first three values in the list.?
Remember, Remember, Remember!

When you write [0:n] for the selection in python, it means 0 (which
represent the first position) is included. But n (which represent the n+1
position) is not included. Therefore [0:n] means select all the positions
from 0 to (n-1).

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:
Example (3)
Find first three values in the list.?
Remember, Remember, Remember!

When you write [0:n] for the selection in python, it means 0 (which
represent the first position) is included. But n (which represent the n+1
position) is not included. Therefore [0:n] means select all the positions
from 0 to (n-1). i.e., [0:3] means select all the positions from 0 to 2.

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:
Example (3)
Find first three values in the list.?
Remember, Remember, Remember!

When you write [0:n] for the selection in python, it means 0 (which
represent the first position) is included. But n (which represent the n+1
position) is not included. Therefore [0:n] means select all the positions
from 0 to (n-1). i.e., [0:3] means select all the positions from 0 to 2.
Solution
print(fam heights new[0:3])

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Slicing:
Example (3)
Find first three values in the list.?
Remember, Remember, Remember!

When you write [0:n] for the selection in python, it means 0 (which
represent the first position) is included. But n (which represent the n+1
position) is not included. Therefore [0:n] means select all the positions
from 0 to (n-1). i.e., [0:3] means select all the positions from 0 to 2.
Solution
print(fam heights new[0:3])
[[”John”, 1.64], [”Hazel”, 1.42], [”mom”, 1.56]]
Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 4/8
Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (4)
Find the first two values from the given list.

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 5/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (4)
Find the first two values from the given list.

Solution

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 5/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (4)
Find the first two values from the given list.

Solution
print(fam heights new[0:2])

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 5/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (4)
Find the first two values from the given list.

Solution
print(fam heights new[0:2])
[[”John”, 1.64], [”Hazel”, 1.42]]

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 5/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (5)
Find all the values in the given list except first value.

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 6/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (5)
Find all the values in the given list except first value.

Solution

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 6/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (5)
Find all the values in the given list except first value.

Solution
There are two ways to solve it: Method 1:

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 6/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (5)
Find all the values in the given list except first value.

Solution
There are two ways to solve it: Method 1:
print(fam heights new[1:4])

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 6/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (5)
Find all the values in the given list except first value.

Solution
There are two ways to solve it: Method 1:
print(fam heights new[1:4])
[[”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 6/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (5)
Find all the values in the given list except first value.

Solution
There are two ways to solve it: Method 1:
print(fam heights new[1:4])
[[”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Second Method:

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 6/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (5)
Find all the values in the given list except first value.

Solution
There are two ways to solve it: Method 1:
print(fam heights new[1:4])
[[”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Second Method:
print(fam heights new[1:])

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 6/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (5)
Find all the values in the given list except first value.

Solution
There are two ways to solve it: Method 1:
print(fam heights new[1:4])
[[”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Second Method:
print(fam heights new[1:])
[[”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 6/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (5)
Find all the values in the given list except first value.

Solution
There are two ways to solve it: Method 1:
print(fam heights new[1:4])
[[”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Second Method:
print(fam heights new[1:])
[[”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

This second method is more appropriate than first.

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 6/8


Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Example (5)
Find all the values in the given list except first value.

Solution
There are two ways to solve it: Method 1:
print(fam heights new[1:4])
[[”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Second Method:
print(fam heights new[1:])
[[”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

This second method is more appropriate than first.


WHY?
Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 6/8
Subsetting lists and slicing: fam heights new=[[”John”,
1.64], [”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

Solution
There are two ways to solve it: Method 1:
print(fam heights new[1:4]
[[”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]
Second Method:
print(fam heights new[1:])
[[”Hazel”, 1.42], [”mom”, 1.56], [”dad”, 1.80]]

This second method is more appropriate than first.


WHY?
Ans. Because (in first method) you need to know how many entries in the
list. However there is no such requirement in the second method.
Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 7/8
Some built-in functions

Max/min Function
Suppose A=[1, 3, 7, 11, 9, 2]
print(max(A))
11
print(min(A))
1
Round function
A=4.76
print(round(A))
5

Dr. Muhammad Yousuf Tufail (IBA.) Essential Software Lecture 4 8/8

You might also like