Sei sulla pagina 1di 9

MECN2012: Computing

Skills and Software


Development
Lecture 17:
Python - More Lists and
Functions
1

More on Lists

Lists are general purpose and can contain any


objects.
umList =python
list(range(0,
8))

venList = []

Convert the range object into a list

or i in numList:
evenList.append(2*i)

rint("numList:", numList)
rint("evenList:", evenList)

entence = "The name is Bond, James Bond"


lauses = sentence.split(",")
rint("clauses:", clauses)

ords = sentence.split(" ")


rint("words:", words)
eply = "Good evening Mr " + words[-1]
rint("reply:", reply)

Creates a list by separating


the string wherever it finds a
comma.
# access last entry of the list

numList: [0, 1, 2, 3, 4, 5, 6, 7]
evenList: [0, 2, 4, 6, 8, 10, 12, 14]
clauses: ['The name is Bond', ' James Bond']
words: ['The', 'name', 'is', 'Bond,', 'James', 2
'Bond']
reply: Good evening Mr Bond

More on Lists
You can also create lists of lists to generate a
matrix.
matrix = [[5,3,5],
[3,4,9],
[2,6,7]]
# a nested list of lists
print("matrix:", matrix)
print("matrix[2]:", matrix[2])
# access 3rd entry of the list
print("matrix[1][0]:", matrix[1][0])
del matrix[1]
print("reduced matrix:", matrix)
matrix.insert(1, [9,9,9])
print("new matrix:", matrix)

# remove second entry


# insert a new second row

matrix: [[5, 3, 5], [3, 4, 9], [2, 6, 7]]


matrix[2]: [2, 6, 7]
matrix[1][0]: 3
reduced matrix: [[5, 3, 5], [2, 6, 7]]
new matrix: [[5, 3, 5], [9, 9, 9], [2, 6,3 7]]

Referencing
A = [1, 2, 3, 4, 5]
B = A
print("A:", A)
print("B:", B)
B[2] = 999
B = [9.9, 8.8, 7.7]
C = A
print("A:", A)
print("B:", B)
print("C:", C)

A
B

99
4
3
9
Connection is broken

When a non-primitive object is


assigned to a variable, only a
reference to the original object is
copied. This is known as aliasing.
Faster
More memory efficient.

9.
9

8.
8
A:
B:
A:
5]
B:
C:
5]

7.
7
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 999, 4,
[9.9, 8.8, 7.7]
[1, 2, 999, 4,

Referencing

You can copy the actual values using slice referencing. This is
called a deep copy.

# now do a deep copy


A = [1, 2, 3, 4, 5]
B = A[:]
# copy each element one at a time
print("A:", A)
print("B:", B)
A
1
2
B[2] = 999
print("B:", B)
B = [9.9, 8.8, 7.7]
C = A
print("A:", A)
print("B:", B)
print("C:", C)
A:
B:
B:
A:
B:
C:

Connection is broken

C
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 999, 4, 5]
[1, 2, 3, 4, 5]
[9.9, 8.8, 7.7]
[1, 2, 3, 4, 5]

9.
9

8.
8

99
3
9
7.
7

Functions

Programs are usually more useful and easier to understand


if a modular approach is used.

We can remove a block of code and replace it with a call to


a function that does the same thing.

A function has the following form:

Called the argument list

def FunctionName (transferred variables):


statements
return returnedVariable
These values are sent to
powerFunc when it is call
E.g. def powerFunc(x, n):
y = 1
for i in range(n):
y = y*x
This is what will
be sent back to
return y
6
the call

Functions

import math

def circleArea(dia):
Area = math.pi*dia*dia/4
return Area

Any function can call any


other function, as long as
it is defined

def tubeVolume (di, do, L):


Vol = (circleArea(do) - circleArea(di))*L
return Vol

# start of main code


ID = float(input("enter inner diameter: "))
OD = float(input("enter outer diameter: "))
length = float(input("enter cylinder length: "))

XsectnArea = circleArea(OD) - circleArea(ID)

# outer area inner area

Function calls

print("cross-sectional area = ", XsectnArea)


print("total volume of hollow shaft = ", tubeVolume(ID, OD, length))
7

Variable Scope

A local variable exists only within the function in which it


was declared. This function is referred to as the variables
scope.

def factoFunc (n):


if n == 0:
n = 1
else:
for i in range(n-1, 1, -1) :
n = n*i
print("in factorial n = ", n)
funcVar = n
return n
n = int(input("enter an integer: "))

print("n! =", factoFunc(n))


print("in main body of code n =", n)
print("in main body of code funcVar =",
funcVar)

This n is only defined in factoFunc


The n in the main code is a
different variable. Only the
value is passed when
factoFunc is called. The value
of n in the main code remains
unaffected.
enter an integer: 5
in factorial n = 120
n! = 120
in main body of code n = 5
Traceback (most rec
File "C:\...
print("in main body
NameError: name 'funcVar'
8
is not defined

Functions
def convert_mileh_2_ms (x):
x = x*0.44704
return x

These xs are different


and only exist within their
functions

def convert_tonne_2_kg (x):


x = x*1000
return x

speed = float(input("enter crash speed [mile/h]: "))


mass = float(input( "enter vehicle mass [tonne]: "))
speed = convert_mileh_2_ms(speed)
mass = convert_tonne_2_kg(mass)

# convert to SI units

These functions change the value of the varia

crashEnergy = 0.5*mass*speed*speed/1000

# E = 1/2*m*v^2 [kJ]

print( "speed =", speed, "m/s" )


print( "mass =", mass, "kg" )
print( "crash energy =", crashEnergy, "kJ" )

Potrebbero piacerti anche