KEMBAR78
Tutorial 21 30 | PDF | Control Flow | Parameter (Computer Programming)
0% found this document useful (0 votes)
33 views10 pages

Tutorial 21 30

Uploaded by

k4mile.erdogan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views10 pages

Tutorial 21 30

Uploaded by

k4mile.erdogan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Python Tutorial, Release 3.12.

3.1.3 Lists

Python knows a number of compound data types, used to group together other values. The most versatile is the list, which
can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different
types, but usually the items all have the same type.

>>> squares = [1, 4, 9, 16, 25]


>>> squares
[1, 4, 9, 16, 25]

Like strings (and all other built-in sequence types), lists can be indexed and sliced:

>>> squares[0] # indexing returns the item


1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]

All slice operations return a new list containing the requested elements. This means that the following slice returns a
shallow copy of the list:

>>> squares[:]
[1, 4, 9, 16, 25]

Lists also support operations like concatenation:

>>> squares + [36, 49, 64, 81, 100]


[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Unlike strings, which are immutable, lists are a mutable type, i.e. it is possible to change their content:

>>> cubes = [1, 8, 27, 65, 125] # something's wrong here


>>> 4 ** 3 # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]

You can also add new items at the end of the list, by using the list.append() method (we will see more about
methods later):

>>> cubes.append(216) # add the cube of 6


>>> cubes.append(7 ** 3) # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]

Assignment to slices is also possible, and this can even change the size of the list or clear it entirely:

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']


>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
(continues on next page)

3.1. Using Python as a Calculator 15


Python Tutorial, Release 3.12.2

(continued from previous page)


>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]

The built-in function len() also applies to lists:

>>> letters = ['a', 'b', 'c', 'd']


>>> len(letters)
4

It is possible to nest lists (create lists containing other lists), for example:

>>> a = ['a', 'b', 'c']


>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'

3.2 First Steps Towards Programming

Of course, we can use Python for more complicated tasks than adding two and two together. For instance, we can write
an initial sub-sequence of the Fibonacci series as follows:

>>> # Fibonacci series:


... # the sum of two elements defines the next
... a, b = 0, 1
>>> while a < 10:
... print(a)
... a, b = b, a+b
...
0
1
1
2
3
5
8

This example introduces several new features.


• The first line contains a multiple assignment: the variables a and b simultaneously get the new values 0 and 1. On
the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated first before
any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.
• The while loop executes as long as the condition (here: a < 10) remains true. In Python, like in C, any non-zero
integer value is true; zero is false. The condition may also be a string or list value, in fact any sequence; anything
with a non-zero length is true, empty sequences are false. The test used in the example is a simple comparison.

16 Chapter 3. An Informal Introduction to Python


Python Tutorial, Release 3.12.2

The standard comparison operators are written the same as in C: < (less than), > (greater than), == (equal to), <=
(less than or equal to), >= (greater than or equal to) and != (not equal to).
• The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt,
you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input
for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is
entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when
you have typed the last line). Note that each line within a basic block must be indented by the same amount.
• The print() function writes the value of the argument(s) it is given. It differs from just writing the expression
you want to write (as we did earlier in the calculator examples) in the way it handles multiple arguments, floating
point quantities, and strings. Strings are printed without quotes, and a space is inserted between items, so you can
format things nicely, like this:

>>> i = 256*256
>>> print('The value of i is', i)
The value of i is 65536

The keyword argument end can be used to avoid the newline after the output, or end the output with a different
string:

>>> a, b = 0, 1
>>> while a < 1000:
... print(a, end=',')
... a, b = b, a+b
...
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,

3.2. First Steps Towards Programming 17


Python Tutorial, Release 3.12.2

18 Chapter 3. An Informal Introduction to Python


CHAPTER

FOUR

MORE CONTROL FLOW TOOLS

As well as the while statement just introduced, Python uses a few more that we will encounter in this chapter.

4.1 if Statements

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))
...
(continues on next page)

19
Python Tutorial, Release 3.12.2

(continued from previous page)


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
2
3
4

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])
(continues on next page)

20 Chapter 4. More Control Flow Tools


Python Tutorial, Release 3.12.2

(continued from previous page)


...
0 Mary
1 had
2 a
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
6

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 breaks out of the innermost enclosing for or while loop.
A for or while loop can include an else clause.
In a for loop, the else clause is executed after the loop reaches its final iteration.
In a while loop, it’s executed after the loop’s condition becomes false.
In either kind of loop, the else clause is not executed if the loop was terminated by a break.
This is exemplified in the following for 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
(continues on next page)

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


Python Tutorial, Release 3.12.2

(continued from previous page)


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.)
When used with a loop, the else clause has more in common with the else clause of a try statement than it does
with that of if statements: a try statement’s else clause runs when no exception occurs, and a loop’s else clause
runs when no break occurs. For more on the try statement and exceptions, see Handling Exceptions.
The continue statement, also borrowed from C, continues with the next iteration of the loop:

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


... if num % 2 == 0:
... print("Found an even number", num)
... continue
... print("Found an odd number", num)
...
Found an even number 2
Found an odd number 3
Found an even number 4
Found an odd number 5
Found an even number 6
Found an odd number 7
Found an even number 8
Found an odd number 9

4.5 pass Statements

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no
action. For example:

>>> while True:


... pass # Busy-wait for keyboard interrupt (Ctrl+C)
...

This is commonly used for creating minimal classes:

>>> class MyEmptyClass:


... pass
...

Another place pass can be used is as a place-holder for a function or conditional body when you are working on new
code, allowing you to keep thinking at a more abstract level. The pass is silently ignored:

>>> def initlog(*args):


... pass # Remember to implement this!
...

22 Chapter 4. More Control Flow Tools


Python Tutorial, Release 3.12.2

4.6 match Statements

A match statement takes an expression and compares its value to successive patterns given as one or more case blocks.
This is superficially similar to a switch statement in C, Java or JavaScript (and many other languages), but it’s more similar
to pattern matching in languages like Rust or Haskell. Only the first pattern that matches gets executed and it can also
extract components (sequence elements or object attributes) from the value into variables.
The simplest form compares a subject value against one or more literals:

def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
case _:
return "Something's wrong with the internet"

Note the last block: the “variable name” _ acts as a wildcard and never fails to match. If no case matches, none of the
branches is executed.
You can combine several literals in a single pattern using | (“or”):

case 401 | 403 | 404:


return "Not allowed"

Patterns can look like unpacking assignments, and can be used to bind variables:

# point is an (x, y) tuple


match point:
case (0, 0):
print("Origin")
case (0, y):
print(f"Y={y}")
case (x, 0):
print(f"X={x}")
case (x, y):
print(f"X={x}, Y={y}")
case _:
raise ValueError("Not a point")

Study that one carefully! The first pattern has two literals, and can be thought of as an extension of the literal pattern
shown above. But the next two patterns combine a literal and a variable, and the variable binds a value from the subject
(point). The fourth pattern captures two values, which makes it conceptually similar to the unpacking assignment (x,
y) = point.
If you are using classes to structure your data you can use the class name followed by an argument list resembling a
constructor, but with the ability to capture attributes into variables:

class Point:
def __init__(self, x, y):
self.x = x
self.y = y

def where_is(point):
(continues on next page)

4.6. match Statements 23


Python Tutorial, Release 3.12.2

(continued from previous page)


match point:
case Point(x=0, y=0):
print("Origin")
case Point(x=0, y=y):
print(f"Y={y}")
case Point(x=x, y=0):
print(f"X={x}")
case Point():
print("Somewhere else")
case _:
print("Not a point")

You can use positional parameters with some builtin classes that provide an ordering for their attributes (e.g. dataclasses).
You can also define a specific position for attributes in patterns by setting the __match_args__ special attribute in
your classes. If it’s set to (“x”, “y”), the following patterns are all equivalent (and all bind the y attribute to the var
variable):

Point(1, var)
Point(1, y=var)
Point(x=1, y=var)
Point(y=var, x=1)

A recommended way to read patterns is to look at them as an extended form of what you would put on the left of
an assignment, to understand which variables would be set to what. Only the standalone names (like var above) are
assigned to by a match statement. Dotted names (like foo.bar), attribute names (the x= and y= above) or class names
(recognized by the “(…)” next to them like Point above) are never assigned to.
Patterns can be arbitrarily nested. For example, if we have a short list of Points, with __match_args__ added, we
could match it like this:

class Point:
__match_args__ = ('x', 'y')
def __init__(self, x, y):
self.x = x
self.y = y

match points:
case []:
print("No points")
case [Point(0, 0)]:
print("The origin")
case [Point(x, y)]:
print(f"Single point {x}, {y}")
case [Point(0, y1), Point(0, y2)]:
print(f"Two on the Y axis at {y1}, {y2}")
case _:
print("Something else")

We can add an if clause to a pattern, known as a “guard”. If the guard is false, match goes on to try the next case block.
Note that value capture happens before the guard is evaluated:

match point:
case Point(x, y) if x == y:
print(f"Y=X at {x}")
case Point(x, y):
print(f"Not on the diagonal")

24 Chapter 4. More Control Flow Tools

You might also like