Docsity

Python Listas: Tipos de Colecciones Mutables, Diapositivas de Computación aplicada

Vista previa parcial del texto

¡Descarga Python Listas: Tipos de Colecciones Mutables y más Diapositivas en PDF de Computación aplicada solo en Docsity!

Python Lists

Chapter 8

Python for Everybody

www.py4e.com

Programming

  • (^) Algorithm
    • A set of rules or steps used to solve a problem
  • (^) Data Structure
    • A particular way of organizing data in a computer

https://en.wikipedia.org/wiki/Algorithm

https://en.wikipedia.org/wiki/Data_structure

A List is a Kind of Collection

  • (^) A collection allows us to put many values in a single “variable”
  • (^) A collection is nice because we can carry all many values around in one convenient package. friends = [ 'Joseph', 'Glenn', 'Sally' ] carryon = [ 'socks', 'shirt', 'perfume' ]

List Constants

  • (^) List constants are surrounded by square brackets and the elements in the list are separated by commas
  • (^) A list element can be any Python object - even another list
  • (^) A list can be empty

    print([1, 24, 76]) [1, 24, 76] print(['red', 'yellow', 'blue']) ['red', 'yellow', 'blue'] print(['red', 24, 98.6]) ['red', 24, 98.6] print([ 1, [5, 6], 7]) [1, [5, 6], 7] print([]) []

Lists and Definite Loops - Best Pals friends = ['Joseph', 'Glenn', 'Sally'] for friend in friends : print('Happy New Year:', friend) print('Done!')

Happy New Year: Joseph

Happy New Year: Glenn

Happy New Year: Sally

Done!

z = ['Joseph', 'Glenn', 'Sally'] for x in z: print('Happy New Year:', x) print('Done!')

Looking Inside Lists

Just like strings, we can get at any single element in a list using an index specified in square brackets 0 Joseph

friends = [ 'Joseph', 'Glenn', 'Sally' ] print(friends[ 1 ]) Glenn 1 >>> Glenn 2 Sally

How Long is a List?

  • (^) The len() function takes a list as a parameter and returns the number of elements in the list
  • (^) Actually len() tells us the number of elements of any set or sequence (such as a string...) >>> greet = 'Hello Bob' >>> print(len(greet)) 9 >>> x = [ 1, 2, 'joe', 99] >>> print(len(x)) 4 >>>

Using the range Function

  • (^) The range function returns a list of numbers that range from zero to one less than the parameter
  • (^) We can construct an index loop using for and an integer iterator >>> print(range( 4 )) [0, 1, 2, 3] >>> friends = ['Joseph', 'Glenn', 'Sally'] >>> print(len(friends)) 3 >>> print(range(len(friends))) [0, 1, 2] >>>

Concatenating Lists Using +

We can create a new list by adding two existing lists together

a = [1, 2, 3] b = [4, 5, 6] c = a + b

>>> print(c)

[1, 2, 3, 4, 5, 6]

>>> print(a)

[1, 2, 3]

Lists Can Be Sliced Using :

t = [9, 41, 12, 3, 74, 15] t[1: 3 ] [41,12] t[: 4 ] [9, 41, 12, 3] t[3:] [3, 74, 15] t[:] [9, 41, 12, 3, 74, 15] Remember: Just like in strings, the second number is “up to but not including”

Building a List from Scratch

  • (^) We can create an empty list and then add elements using the append method
  • (^) The list stays in order and new elements are added at the end of the list >>> stuff = list() >>> stuff.append('book') >>> stuff.append(99) >>> print(stuff) ['book', 99] >>> stuff.append('cookie') >>> print(stuff) ['book', 99, 'cookie']

Is Something in a List?

  • (^) Python provides two operators that let you check if an item is in a list
  • (^) These are logical operators that return True or False
  • (^) They do not modify the list

    some = [1, 9, 21, 10, 16] 9 in some True 15 in some False 20 not in some True

Built-in Functions and Lists

  • (^) There are a number of functions built into Python that take lists as parameters
  • (^) Remember the loops we built? These are much simpler. >>> nums = [3, 41, 12, 9, 74, 15] >>> print(len(nums)) 6 >>> print(max(nums)) 74 >>> print(min(nums)) 3 >>> print(sum(nums)) 154 >>> print(sum(nums)/len(nums)) 25.

numlist = list() while True : inp = input('Enter a number: ') if inp == 'done' : break value = float(inp) numlist.append(value) average = sum(numlist) / len(numlist) print('Average:', average) total = 0 count = 0 while True : inp = input('Enter a number: ') if inp == 'done' : break value = float(inp) total = total + value count = count + 1 average = total / count print('Average:', average) Enter a number: 3 Enter a number: 9 Enter a number: 5 Enter a number: done Average: 5.