Download presentation

Presentation is loading. Please wait.

Published byRosa Winslow Modified over 4 years ago

1
Computer Science 101 Lists in Python

2
The need for lists Often we need many different variables that play a similar role in the program Often we need many different variables that play a similar role in the program For example, suppose we have a temperature reading for each day of the year and need to manipulate these values several times within a program. For example, suppose we have a temperature reading for each day of the year and need to manipulate these values several times within a program. With simple variables, With simple variables, We would need to use 365 variables.We would need to use 365 variables. We would not be able to loop over these variables.We would not be able to loop over these variables.

3
Lists A list is a numbered collection of variables. A list is a numbered collection of variables. The variables in a list share the same name and are distinguished from one another by their numbers or subscripts. The variables in a list share the same name and are distinguished from one another by their numbers or subscripts. We can loop through the variables of a list by using a variable for the subscript. We can loop through the variables of a list by using a variable for the subscript. The subscripts are always 0,1,…, size-1 The subscripts are always 0,1,…, size-1

4
Referencing List Elements Suppose we have a list, temperature, for the temperature readings for the year. Suppose we have a list, temperature, for the temperature readings for the year. The subscripts would be 0,1,…,364. The subscripts would be 0,1,…,364. To refer to the reading for a given day, we use the name of the list with the subscript in brackets: temperature[4] for the fifth day. To refer to the reading for a given day, we use the name of the list with the subscript in brackets: temperature[4] for the fifth day. 75798270686558636761 temperature temperature[4]

5
Python Session

7
Appending to a List If we have a list, say scores, we can add a value, say num, to the list by scores.append(num) If we have a list, say scores, we can add a value, say num, to the list by scores.append(num)

8
… and all the children are above average

10
Random numbers For testing purposes or for simulations, it is convenient to let the computer generate random data for us. For testing purposes or for simulations, it is convenient to let the computer generate random data for us. In Python there is a library, random, that has a lot of tools for working with random numbers. In Python there is a library, random, that has a lot of tools for working with random numbers.

11
Randrange random.randrange random.randrange random.randrange(num) gives a random number in range 0,…,(num-1) random.randrange(num) gives a random number in range 0,…,(num-1) random.randrange(num1,num2) gives random number in range num1,…,(num2-1)random.randrange(num1,num2) gives random number in range num1,…,(num2-1)

12
Randrange

13
Random Lists To build a random list of values, we can To build a random list of values, we can Start with an empty list, []Start with an empty list, [] Then append random values to the listThen append random values to the list

14
Find Largest Example

Similar presentations

OK

Inputting and presenting data. “ I know what a spreadsheet is, I can enter simple data into a spreadsheet.” By the end of this session, I want you to.

Inputting and presenting data. “ I know what a spreadsheet is, I can enter simple data into a spreadsheet.” By the end of this session, I want you to.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google