Beginning+Python 2.compressed by Unknown
Author:Unknown
Language: eng
Format: mobi, epub
Published: 2015-09-11T18:57:58.789643+00:00
Chapter 18: Numerical Programming
Lists or Tuples?
Which should you use for arrays: lists or tuples? Remember that lists can be
modified, whereas tuples cannot. Therefore, if you need to add to, remove from,
or change the array, use a list. Though you can perform these operations on a
tuple by creating a new tuple with numbers added, removed, or changed, this is
more difficult to code and often runs more slowly. For fixed sequences of
numbers, you can use tuples.
Let ? s take another example of a function that operates on an array. You already wrote a function that computes the mean of an array of numbers. Now write a function that computes the standard
deviation. To remind you, the standard deviation is an indication of how much the numbers vary among themselves. If they ? re all almost the same, the standard deviation will be small, whereas if they are all over the place, the standard deviation will be large. The formula for the standard deviation that you will use is shown in Figure 18 - 1.
Figure 18-1
Here x , . . . , x are the numbers in the array, µ is their mean, and N is the length of the array.
1
N
You could implement a standard deviation function several different ways. Here ? s one of them: from math import sqrt
def stddev(numbers):
n = len(numbers)
sum = 0
sum_of_squares = 0
for number in numbers:
sum += number
sum_of_squares += number * number
return sqrt(sum_of_squares / n - (sum / n) ** 2)
This function loops over the numbers to compute their sum of squares. Simultaneously, it computes their sum, because it needs that to compute the mean. The last line computes the standard deviation according to the preceding formula. You might have noticed that the function uses number * number when computing the sum of squares instead of number ** 2 ; that ? s because squaring a number by
multiplying it by itself is faster than using the general exponentiation operator.
Watch stddev in action. Remember that it takes one argument, a sequence of numbers (not several numerical arguments):
>
>
>
print(stddev((5.6, 3.2, -1.0, 0.7)))
2.50137462208
381
Part III: Putting Python to Work
Think for a moment about some advantages and drawbacks of using lists of numbers for arrays:
❑
The elements of a Python list need not be of the same type. You can create a list for which some elements are int , float , long , and double , or other objects like strings or even other sequences. For some applications, this is very handy. For instance, you may want to store None in a sequence to indicate that a value is not known. For other applications, it ? s important to make sure that all of the values in an array are of the same type. In that case, you ? ll have to write extra code to ensure this.
❑
Lists are single - dimensional, which makes them natural for expressing one - dimensional arrays.
You can create two - dimensional arrays as lists of lists and higher - dimensional arrays
analogously, but this can get complicated.
❑
Lists are a standard part of Python.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Eco-friendly approach of bio-indigo synthesis and developing purification methods towards isolation of indigo from indirubin and bacterial fragments by Ramalingam Manivannan & Kaliyan Prabakaran & Young-A Son(147691)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74269)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(66072)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(65822)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(65257)
Unveiling the design rules for tunable emission in graphene quantum dots: A high-throughput TDDFT and machine learning perspective by Şener Özönder & Mustafa Coşkun Özdemir & Caner Ünlü(50857)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(31339)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(31299)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(31043)
What's Done in Darkness by Kayla Perrin(27101)
Topological analysis of non-conjugated ethylene oxide cored dendrimers decorated with tetraphenylethylene: Insights from degree-based descriptors using the polynomial approach by A Theertha Nair & D Antony Xavier & Annmaria Baby & S Akhila(26482)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26435)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(21010)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(20772)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(20646)
The Fifty Shades Trilogy & Grey by E L James(19604)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19486)
Shot Through the Heart by Mercy Celeste(19344)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17490)