Beginning+Python 2.compressed by Unknown

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



Copyright Disclaimer:
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.
Popular ebooks
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(232561)
Personalized inhaled bacteriophage therapy for treatment of multidrug-resistant Pseudomonas aeruginosa in cystic fibrosis by unknow(201092)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(102426)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(101961)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(101759)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74556)
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ü(50973)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(40345)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(40269)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(40170)
Alkaline-earth metals promote propane dehydrogenation with carbon dioxide through geometric effects: Altering the reaction pathway by unknow(32795)
Induced iron vacancies boosting FeOOH loaded on sustainable Fenton-like collagen fiber membrane for efficient removal of emerging contaminants by unknow(32589)
Efficient electric-field-assisted photochemical conversion of methane to n-propanol exclusively over penetrated TiO2Ti hollow fibers by Guanghui Feng(32509)
Bi2SiO5 nanosheets as piezo-photocatalyst for efficient degradation of 2,4-Dichlorophenol by Hangyu Shi & Yifu Li & Lishan Zhang & Guoguan Liu & Qian Zhang & Xuan Ru & Shan Zhong(32450)
A novel NDIPTA organic heterojunction photocatalyst with built-in electric field for efficient hydrogen production by Jiahui Yang & Baojun Ma & Yongfa Zhu(32421)
Enhanced conversion of methane to liquid-phase oxygenates via hollow ferrite nanotube@horseradish peroxidase based photoenzymatic catalysis by Jun Duan & Shiying Fan & Xinyong Li & Shaomin Liu(32388)
Ordered macroporous superstructure of defective carbon adorned with tiny cobalt sulfide for selective electrocatalytic hydrogenation of cinnamaldehyde by Xiao-Shi Yuan & Sheng-Hua Zhou & San-Mei Wang & Wenbo Wei & Xiaofang Li & Xin-Tao Wu & Qi-Long Zhu(32307)
What's Done in Darkness by Kayla Perrin(27218)
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(26600)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26522)