0% found this document useful (0 votes)
33 views5 pages

Python Statements Notes

The document discusses Python for loops and statements. It explains that Python for loops iterate over sequences like lists and strings, unlike in other languages where they iterate over a defined range of numbers. It provides examples of using for loops to iterate over lists and strings. It also describes the range() function for generating sequences of numbers, and break and continue statements for controlling loop behavior.

Uploaded by

Dipak Nandeshwar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
33 views5 pages

Python Statements Notes

The document discusses Python for loops and statements. It explains that Python for loops iterate over sequences like lists and strings, unlike in other languages where they iterate over a defined range of numbers. It provides examples of using for loops to iterate over lists and strings. It also describes the range() function for generating sequences of numbers, and break and continue statements for controlling loop behavior.

Uploaded by

Dipak Nandeshwar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Perhaps the most well-known statement type is the if statement.

For example:

>>>

>>> x = int(input("Please enter an integer: "))

Please enter an integer: 42

>>> if x < 0:

... x=0

... print('Negative changed to zero')

... elif x == 0:

... print('Zero')

... elif x == 1:

... print('Single')

... else:

... print('More')

...

More

There can be zero or more elif parts, and the else part is optional. The keyword ‘elif’ is short for ‘else
if’, and is useful to avoid excessive indentation. An if … elif … elif … sequence is a substitute for the
switch or case statements found in other languages.

If you’re comparing the same value to several constants, or checking for specific types or attributes,
you may also find the match statement useful. For more details see match Statements.

4.2. for Statements

The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than
always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the
ability to define both the iteration step and halting condition (as C), Python’s for statement iterates
over the items of any sequence (a list or a string), in the order that they appear in the sequence. For
example (no pun intended):

>>>

>>> # Measure some strings:

... words = ['cat', 'window', 'defenestrate']


>>> for w in words:

... print(w, len(w))

...

cat 3

window 6

defenestrate 12

Code that modifies a collection while iterating over that same collection can be tricky to get right.
Instead, it is usually more straight-forward to loop over a copy of the collection or to create a new
collection:

# Create a sample collection

users = {'Hans': 'active', 'Éléonore': 'inactive', '景太郎': 'active'}

# Strategy: Iterate over a copy

for user, status in users.copy().items():

if status == 'inactive':

del users[user]

# Strategy: Create a new collection

active_users = {}

for user, status in users.items():

if status == 'active':

active_users[user] = status

4.3. The range() Function

If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy. It
generates arithmetic progressions:

>>>

>>> for i in range(5):

... print(i)

...

0
1

The given end point is never part of the generated sequence; range(10) generates 10 values, the
legal indices for items of a sequence of length 10. It is possible to let the range start at another
number, or to specify a different increment (even negative; sometimes this is called the ‘step’):

>>>

>>> list(range(5, 10))

[5, 6, 7, 8, 9]

>>> list(range(0, 10, 3))

[0, 3, 6, 9]

>>> list(range(-10, -100, -30))

[-10, -40, -70]

To iterate over the indices of a sequence, you can combine range() and len() as follows:

>>>

>>> a = ['Mary', 'had', 'a', 'little', 'lamb']

>>> for i in range(len(a)):

... print(i, a[i])

...

0 Mary

1 had

2a

3 little

4 lamb

In most such cases, however, it is convenient to use the enumerate() function, see Looping
Techniques.
A strange thing happens if you just print a range:

>>>

>>> range(10)

range(0, 10)

In many ways the object returned by range() behaves as if it is a list, but in fact it isn’t. It is an object
which returns the successive items of the desired sequence when you iterate over it, but it doesn’t
really make the list, thus saving space.

We say such an object is iterable, that is, suitable as a target for functions and constructs that expect
something from which they can obtain successive items until the supply is exhausted. We have seen
that the for statement is such a construct, while an example of a function that takes an iterable is
sum():

>>>

>>> sum(range(4)) # 0 + 1 + 2 + 3

Later we will see more functions that return iterables and take iterables as arguments. In chapter
Data Structures, we will discuss in more detail about list().

4.4. break and continue Statements, and else Clauses on Loops

The break statement, like in C, breaks out of the innermost enclosing for or while loop.

Loop statements may have an else clause; it is executed when the loop terminates through
exhaustion of the iterable (with for) or when the condition becomes false (with while), but not when
the loop is terminated by a break statement. This is exemplified by the following loop, which
searches for prime numbers:

>>>

>>> for n in range(2, 10):

... for x in range(2, n):

... if n % x == 0:

... print(n, 'equals', x, '*', n//x)

... break
... else:

... # loop fell through without finding a factor

... print(n, 'is a prime number')

...

2 is a prime number

3 is a prime number

4 equals 2 * 2

5 is a prime number

6 equals 2 * 3

7 is a prime number

8 equals 2 * 4

9 equals 3 * 3

(Yes, this is the correct code. Look closely: the else clause belongs to the for loop, not the if
statement.)

You might also like