Well House Consultants
You are on the site of
Well House Consultants
Open Source Training Courses
hotel accommodation. You are welcome to browse and use
our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Well House Manor Resources
Object Orientation and General topics
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
C and C++ Resources
Web and Intranet Resources
Python module Y103
Conditionals and Loops
Exercises, examples and other material relating to training module Y103. This topic is presented on public courses Learning to program in Python
, Python Programming
How to write code in Python that's only run if a certain condition is met, and how to repeat a piece of code more than once.
Related technical and longer articlesthe while and else structure
|Articles and tips on this subject||updated|
|4210||If elif elif elif - multiway selection in Python|
Most languages have "if / else if / else if / else " type constructs for selecting conditional code, although the format / keyword varies between languages, in the case if Python, the intermediate keyword is elif.
In all cases with this construct, you can think of it as like peeling an onion - taking ...
|4092||Identity in Python|
There's a differece between "the same", "equal" and "identical". I can have two identical 5p coins, but they're not the same coin. And I can have two coins which are not identical, but are equal in value. And when you're checking for equality in a program, you should know which you're looking at. Take ...
|3895||Flowchart to program - learning to program with Well House|
Drawing a flowchart is how many people understand a process; it's something that's been used in schools and textbooks for many generations, and it provides a graphic representation of choices and actions that most people find easy to follow.
For newcomers to programming, such as on my Learning to program ...
|3620||Finding the total, average, minimum and maximum in a program|
There are a number of programming techniques which the experienced coder takes for granted, but which aren't necessarily intuitive for the newcomer. Call them "design techniques" or "design patterns" that need to be learned, if you want some fashionable buzzwords.
• To produce the sum of a stream ...
|3762||Learning to program - the if statement. Python.|
Statements in a program run sequentially, unless you add specific code to make them do otherwise, with each statement being separated from the next in some way or another.
Should you wish to run a statement in only some circumstances, you can use a conditional statement:
• the word if (if is available ...
|3397||Does a for loop evaluate its end condition once, or on every iteration?|
All the languages that we teach have a for loop or the equivalent, which is a clean way of repeating a block of code with a rising or falling index number. It's used in many circumstances - for example in iterating through the months of the year (for m goes from 1 to 12) of in stepping through all ...
|3558||Python or Lua - which should I use / learn?|
"Should I learn and use Python or Lua" - a question to me. The questioner was perceptive, knowing that he shouldn't try to learn lots of languages at the same time, and that he should always limit the "basket" of languages used within his applications - in fact, he was wondering if he could select just ...
|3439||Python for loops - applying a temporary second name to the same object|
If you write a for loop in Python to go through each element of a list, you assign an extra (temporary) name to each member of the list while within the loop. So than any changes made to the named variable will also be changes to the object in the original list. However, that's overshaddowed / overtaken ...
|3200||How a for loop works Java, Perl and other languages|
Java, Perl, PHP, Ruby, C, C++, Tcl and many other languages support a "for" loop construct.
When your program enters the top of the loop, it performs the first statement in the brackets ... in this Perl example, that's to initialise the $now variable to 1. It then evaluates the expression given as ...
|3083||Python - fresh examples from recent courses|
During courses, I write a lot of examples in front of delegates so that the get to see both a program to perform a certain task and how that solution was reached - after all, they'll be expected to modify and write programs after the course, and just showing them the final result would only be telling ...
|2899||Groupsave tickets - 3 or 4 train tickets for the price of 2|
First Great Western offer "Groupsave" tickets where 3 or 4 adults can travel for the price of two on most off peak, book at time of travel, services. And children can go along at a pound each. But there's no "Groupsave 5", so if there are five of you, you'll need to buy a Groupsave 4 plus an extra ...
|2778||Learning to program in Python 2 ... and / or in Python 3|
Yesterday - "Learning to Program in Python" - and I'm now teaching the day so that it's both Python 2 and Python 3 compatible. But it's really an intro day, and we'll vary how we go for the following 3 days of the course, majoring in an appropriate direction for our group - that's a luxury we have when ...
|657||The ternary operator in Python|
The ? : operator that you may have come across in Perl, PHP, C and Java - known as the ternary or conditional operator - is ABSENT from Python. "But it's so useful" I hear you cry. Ah yes, but isn't this elegant:
val = float(raw_input("Age: "))
status = ("working","retired")[val>65]
print "You should ...
|909||Python is like a narrowboat|
I'm giving a Python course today, to a team of seasoned programmers, and of course I'm coming up with a few things that are different to the languages they know. So far, we've had ...
* No switch statement
* No ++ operator
* blocks indicated via insets
* else clauses on while loops
Of course, this ...
|1696||Saying NOT in Perl, PHP, Python, Lua ...|
"Isn't there one standard way to say NOT?" asked one of my delegates on today's course - and it's an excellent question. But the answer to a question about a negative subject is itself in the negative - no, there isn't just a single way!
In fact .. I can think of no fewer that 12 ways!
• 1. ! ...
|1661||Equality, sameness and identity - Python|
Is the number 7.0 the same as the number 7.00000? A trick question, because it depends on how you look at it. It has the same value, but it is not identical. And if eachof them is held in computer memory, there's no chance at all that they're both the same thing - i.e. held in the same memory location ...
|1477||Decisions - small ones, or big ones?|
When you're traveling, you'll sometimes come to a point at which you make a decision - to go one way or to go another. Sometimes the decision is a small one - for example, if I'm driving through Marlborough there's a choice of the town centre or the road around the back; whichever I take, they come ...
|1201||No switch in Python|
Question There's no switch statement in Python. Why not?
(Poor) Answer Because there's no need .. you can use a series of if and elif statments. And we can't do it in Python because there isn't a label syntax that would be needed for case.
Better Answer If you come to write some code where a switch ...
|835||Python - when to use the in operator|
Python's in construct, when used with an if statement, lets you loop through all the members of a collection (such as a list or a tuple) and see if there's a member in the list that's equal to the pattern - thus
val = 17
if val in [1,4,5,7,12,14,17,20,34]: print "yes"
will print out "Yes". For looking ...
|788||New - Conditional expressions in Python 2.5|
Python has always been described as a "tight" language in that new syntaxes are only added with a great deal of thought, and then only if they provide a significant benefit without diluting the crispness of the language.
There have been long discussions in the Python world about adding in a conditional ...
|668||Python - block insets help with documentation|
For many programmers, documenting their code is a chore, yet every piece of code that's going to be (re)used needs both instructions for the user, and adequate explanation of techniques for the code maintainer. The need for BOTH of these forms of support documentation is often overlooked by the newcomer, ...
Examples from our training material
|b1.py|| if - elif - else conditional|
|bp|| Exchange Rate Conversion answer|
|cally.py|| Days and months exercise - answer using methods|
|combo.py|| Combining conditions with and and or|
|george|| if / elif / elif - how it works|
|goal.py|| when is a number "false"?|
|groupsave|| Groupsave ticket calculator|
|if1.py|| an if statement in python |
|if2.py|| if with a block |
|if3.py|| Nested blocks |
|if4.py|| if, elif and else |
|ken|| single and multiple statement blocks / conditionals|
|lenny|| First Loop|
|python_switch_case|| Alternative to switch, case and default|
|shand.py|| Shorthand operators|
|sintea|| Singapore Teapot - exercise answer|
|sintea2|| Singapore Teapot - exercise answer (2)|
|swans.py|| Swan family - Python|
|tab2.py|| loop controls - break and else|
|table.py|| a loop in python|
|three.py|| if statments - various ways of writing them|
PicturesLearning to program in Python
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from http://www.training-notes.co.uk
Topics covered in this module
Conditional statement - if.
blocks in Python.
else and elif.
Conditions, and what is truth.
The while loop.
break and continue; else clauses.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.