0% found this document useful (0 votes)
3 views

6 Tuples listes

This document discusses lambda functions, tuples, and lists in Python. It explains the usage of anonymous functions, the immutability of tuples, and the mutability of lists, along with examples and exercises. Additionally, it highlights the similarities between lists, tuples, and strings in terms of indexing, slicing, and iteration.

Uploaded by

ghouti.haoulia
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)
3 views

6 Tuples listes

This document discusses lambda functions, tuples, and lists in Python. It explains the usage of anonymous functions, the immutability of tuples, and the mutability of lists, along with examples and exercises. Additionally, it highlights the similarities between lists, tuples, and strings in terms of indexing, slicing, and iteration.

Uploaded by

ghouti.haoulia
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/ 16

13/12/2024

LAMBDA FUNCTIONS,
TUPLES and LISTS

FROM LAST TIME

def apply(criteria,n):
"""
* criteria: function that takes in a number and returns a bool
* n: an int
Returns how many ints from 0 to n (inclusive) match the
criteria (i.e. return True when run with criteria) """
count = 0
for i in range(n+1):
if criteria(i):
count += 1
return count

def is_even(x):
return x%2==0

print(apply(is_even,10))

1
13/12/2024

ANONYMOUS FUNCTIONS

 Sometimes don’t want to name functions, especially simple


ones. This function is a good example:
def is_even(x):
return x%2==0
 Can use an anonymous procedure by using lambda

lambda x: x%2 == 0

Body of lambda
parameter Note no return keyword

 lambda creates a procedure/function object, but simply does


not bind a name to it

ANONYMOUS FUNCTIONS

 Function call with a named function:


apply( is_even , 10 )

 Function call with an anonymous function as parameter:

apply( lambda x: x%2 == 0 , 10 )

 lambda function is one-time use. It can’t be reused because it


has no name!

2
13/12/2024

YOU TRY IT!


 What does this print?

def do_twice(n, fn):


return fn(fn(n))

print(do_twice(3, lambda x: x**2))

YOU TRY IT!


 What does this print?

def do_twice(n, fn):


return fn(fn(n))

print(do_twice(3, lambda x: x**2))

Global environment

do_twice function object

3
13/12/2024

YOU TRY IT!


 What does this print?

def do_twice(n, fn):


return fn(fn(n))

print(do_twice(3, lambda x: x**2))

Global environment do_twice environment

do_twice function object n 3


fn lambda x: x**2

YOU TRY IT!


 What does this print?

def do_twice(n, fn):


return fn(fn(n))

print(do_twice(3, lambda x: x**2))

Global environment do_twice environment lambda x: x**2


environment
do_twice function object n 3
fn lambda x: x**2 x ???

4
13/12/2024

YOU TRY IT!


 What does this print?

def do_twice(n, fn):


return fn(fn(n))

print(do_twice(3, lambda x: x**2))

Global environment do_twice environment lambda x: x**2


environment
do_twice function object n 3
fn lambda x: x**2 x ???

lambda x: x**2
environment

x 3

YOU TRY IT!


 What does this print?

def do_twice(n, fn):


9
return fn(fn(n))

print(do_twice(3, lambda x: x**2))

Global environment do_twice environment lambda x: x**2


environment
do_twice function object n 3
fn lambda x: x**2 x ???
9

lambda x: x**2
environment

x 3
Returns 9

5
13/12/2024

YOU TRY IT!


 What does this print?

def do_twice(n, fn):


81
return fn(fn(n))

print(do_twice(3, lambda x: x**2))

Global environment do_twice environment lambda x: x**2


environment
do_twice function object n 3
fn lambda x: x**2 x 99
Returns 81

YOU TRY IT!


 What does this print?

def do_twice(n, fn):


return fn(fn(n))
81
print(do_twice(3, lambda x: x**2))

Global environment do_twice environment

do_twice function object n 3


fn lambda x: x**2
PRINTS 81
Returns 81

6
13/12/2024

TUPLES

A NEW DATA TYPE

 Have seen scalar types: int,float,bool


 Have seen one compound type: string
 Want to introduce more general compound data types
 Indexed sequences of elements, which could themselves be compound
structures
 Tuples – immutable
 Lists – mutable

 Next lecture, will explore ideas of


 Mutability
 Aliasing
 Cloning

7
13/12/2024

TUPLES

 Indexable ordered sequence of objects


 Objects can be any type – int, string, tuple, tuple of tuples, …
 Cannot change element values, immutable
te = ()
ts = (2,)

t = (2, "mit", 3)
t[0]  evaluates to 2
(2,"mit",3) + (5,6)evaluates to a new tuple(2,"mit",3,5,6)
t[1:2]  slice tuple, evaluates to ("mit",)
t[1:3]  slice tuple, evaluates to ("mit",3)
len(t)  evaluates to 3
max((3,5,0))  evaluates 5
t[1] = 4  gives error, can’t modify object

INDICES AND SLICING

seq = (2,'a',4,(1,2))
index: 0 1 2 3
print(len(seq))  4
print(seq[3])  (1,2)
print(seq[-1])  (1,2)
print(seq[3][0])  1
print(seq[4])  error

print(seq[1])  'a'
print(seq[-2:]  (4,(1,2))
print(seq[1:4:2]  ('a',(1,2))
print(seq[:-1])  (2,'a',4)
print(seq[1:3])  ('a',4)

for e in seq:  2
print(e) a
4
(1,2)

8
13/12/2024

TUPLES

 Conveniently used to swap variable values


x = 1 x = 1 x = 1
y = 2 y = 2 y = 2
x = y temp = x (x, y) = (y, x)
y = x x = y
y = temp

TUPLES

 Used to return more than one value from a function


def quotient_and_remainder(x, y):
q = x // y
r = x % y
return (q, r)

both = quotient_and_remainder(10,3)

(quot, rem) = quotient_and_remainder(5,2)

9
13/12/2024

BIG IDEA
Returning
one object (a tuple)
allows you to return
multiple values (tuple elements)

YOU TRY IT!


 Write a function that meets these specs:
 Hint: remember how to check if a character is in a string?

def char_counts(s):
""" s is a string of lowercase chars
Return a tuple where the first element is the
number of vowels in s and the second element
is the number of consonants in s """

10
13/12/2024

VARIABLE NUMBER of
ARGUMENTS

 Python has some built-in functions that take variable number


of arguments, e.g, min
 Python allows a programmer to have same capability,
using * notation
def mean(*args):
tot = 0
for a in args:
tot += a
return tot/len(args)
 numbers is bound to a tuple of the supplied values
 Example:
 mean(1,2,3,4,5,6)

LISTS

11
13/12/2024

LISTS

 Indexable ordered sequence of objects


• Usually homogeneous (i.e., all integers, all strings, all lists)
• But can contain mixed types (not common)
 Denoted by square brackets, []
 Mutable, this means you can change values of specific
elements of list

INDICES and ORDERING

a_list = []
L = [2, 'a', 4, [1,2]]
[1,2]+[3,4]  evaluates to [1,2,3,4]
len(L)  evaluates to 4
L[0]  evaluates to 2
L[2]+1  evaluates to 5
L[3]  evaluates to [1,2], another list!
L[4]  gives an error
i = 2
L[i-1]  evaluates to 'a' since L[1]='a'
max([3,5,0])  evaluates 5

12
13/12/2024

ITERATING OVER a LIST

 Compute the sum of elements of a list


 Common pattern

total = 0 total = 0
for i in range(len(L)): for i in L:
total += L[i] total += i
print(total) print(total)

 Notice
• list elements are indexed 0 to len(L)-1
and range(n) goes from 0 to n-1

ITERATING OVER a LIST

 Natural to capture iteration over a list inside a function


def list_sum(L):
total = 0 total = 0
for i in L: for i in L:
# i is 8 then 3 then 5
total += i total += i
print(total) return total

 Function call list_sum([8,3,5])


 Loop variable i takes on values in the list in order! 8 then 3 then 5
 To help you write code and debug, comment on what the loop var
values are so you don’t get confused!

13
13/12/2024

LISTS SUPPORT ITERATION

 Because lists are ordered sequences of elements, they naturally


interface with iterative functions

Add the elements of a list Add the length of elements of a list


def list_sum(L): def len_sum(L):
total = 0 total = 0

for e in L: for s in L:
total += e total += len(s)
return(total) return(total)

list_sum([1,3,5])  9 len_sum(['ab', 'def', 'g'])  6

YOU TRY IT!


 Write a function that meets these specs:
def sum_and_prod(L):
""" L is a list of numbers
Return a tuple where the first value is the
sum of all elements in L and the second value
is the product of all elements in L """

14
13/12/2024

Implement the function that meets the


specifications below:

def dot_product(tA, tB):


tot = 0
for i in range(len(tA)):
tot += tA[i]*tB[i]
return (len(tA), tot)

15
13/12/2024

SUMMARY

 Lambda functions are useful when you need a simple function


once, and whose body can be written in one line
 Tuples are indexable sequences of objects
 Can’t change its elements, for ex. can’t add more objects to a tuple
 Syntax is to use ()
 Lists are indexable sequences of objects
 Can change its elements. Will see this next time!
 Syntax is to use []
 Lists and tuples are very similar to strings in terms of
 Indexing,
 Slicing,
 Looping over elements

16

You might also like