Main Content
Finding sum, minimum, maximum and average in Python (and Ruby) Archive - Originally posted on "The Horse's Mouth" - 2015-01-19 18:04:24 - Graham Ellis
A fresh example (in Python) from today's "learning to program in" course ... finding the number, sum, minimum and maximum of a series of numbers typed in. See [here] .
We start off with zero for the count and sum to date, but we do not initialise the minimum or the maximum as we don't know what they would be initialised to - instead, when we read the first value we store that into the minimum (so far) and maximum (so far) variables.
Note the contrast to the similar Ruby example ([here] in which we initialise the variables to nil - a slightly different strategy caused by different scoping rules.
In all cases, we also need to check for a "divide by zero" if there is no input data. My Python program from today does that, I note; the previous Ruby one doesn't have that in.
Some other articles
R104 - Control Structures Alternating valuses / flip-flop / toggle - example in Ruby Where does Ruby load modules from, and how to load from current directory Separating your code for easier testing, understanding and re-use; example in Ruby This article Conditionals, loops and methods in Ruby - a primer with simple examples Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Muttable v immutable and implications - Ruby Finding the total, average, minimum and maximum in a program Ruby v Perl - a comparison example Assigning values to variables within other statements - Ruby Does a for loop evaluate its end condition once, or on every iteration? Multiple inputs, multiple out, ruby functions Is this number between? Does this list include? - Ruby How a for loop works Java, Perl and other languages Returning multiple values from a function call in various languages - a comparison Ruby training - some fresh examples for string handling applications Splitting data reading code from data processing code - Ruby Why do I need brackets in Ruby ... or Perl, Python, C or Java Alternative loops and conditionals in Ruby and Perl For loop - checked once, or evety time? Ruby v Perl comparison and contrast Passing code to procedures and yield in Ruby A short form of if ... then ... else Learning to program in Ruby - examples of the programming basics Ruby, Perl, Linux, MySQL - some training notes Ruby to access web services Ruby Programming Course - Saturday and Sunday What to do with a huge crop of apples Clean code, jump free (Example in Lua) Saying NOT in Perl, PHP, Python, Lua ... Some Ruby programming examples from our course Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) A better alternative to cutting and pasting code Ruby's case - no break Equality in Ruby - == eql? and equal? Breaking a loop - Ruby and other languages 1st, 2nd, 3rd revisited in Ruby Y103 - Conditionals and Loops Conditional operators in Python Equality (in Python) Method, Class, Module, Package - how to they relate in Python? Setting up and tearing down with the Python with keyword This article Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) If elif elif elif - multiway selection in Python Identity in Python Flowchart to program - learning to program with Well House Learning to program - the if statement. Python. Finding the total, average, minimum and maximum in a program Python or Lua - which should I use / learn? Python for loops - applying a temporary second name to the same object Does a for loop evaluate its end condition once, or on every iteration? How a for loop works Java, Perl and other languages Python - fresh examples from recent courses Groupsave tickets - 3 or 4 train tickets for the price of 2 Learning to program in Python 2 ... and / or in Python 3 Saying NOT in Perl, PHP, Python, Lua ... Equality, sameness and identity - Python Decisions - small ones, or big ones? No switch in Python Python is like a narrowboat Python - when to use the in operator New - Conditional expressions in Python 2.5 Python - block insets help with documentation The ternary operator in Python Wimbledon Neck What - no switch or case statement? Q110 - Programming Algorithms Some gems from an introduction to Python Identifying the first and last records in a sequence Testing new algorithms in PHP A good example of recursion - a real use in Python This article Selecting RECENT and POPULAR news and trends for your web site users Learning to program - what are algorithms and design patterns? Finding all the unique lines in a file, using Python or Perl Finding the total, average, minimum and maximum in a program Why would you want to use a Perl hash? AND and OR operators - what is the difference between logical and bitwise varieties? How many toilet rolls - hotel inventory and useage Finding elements common to many lists / arrays Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl Arrays v Lists - what is the difference, why use one or the other Lots of way of converting 3 letter month abbreviations to numbers Sorting people by their names Comparing floating point numbers - a word of caution and a solution And and Or illustrated by locks A life lesson from the accuracy of numbers in Excel and Lua Grouping rows for a summary report - MySQL and PHP Matching disparate referencing systems (MediaWiki, PHP, also Tcl) Nuclear Physics comes to our web site Validating Credit Card Numbers Ordnance Survey Grid Reference to Latitude / Longitude Updating a page strictly every minute (PHP, Perl) Speed Networking - a great evening and how we arranged it How similar are two words Bellringing and Programming and Objects and Perl Searching for numbers