PYW.png 3b: Loops I

Table of Contents

🎉Table of contents with working links🎉

1. Reflection on test

1.1. Solution q1

import math # May be used in your answers

### Question 1 ###
#
# In mathematics, a parabola is described by a function of the form
# f(x) = ax^2 + bx + c, where a, b, and c are real numbers and a is not
# equal to zero.
#
# The discriminant of a parabola can be used to determine if the parabola
# has 0, 1 or 2 roots. In this first step, you're asked to calculate the
# discriminant.
#
# The formula for the discriminant is D = b^2 - 4ac.
#
# Edit the function below such that it takes a, b, and c as inputs and 
# returns the value of the discriminant. The inputs a, b, and c
# have the type float.

def discriminant(a, b, c):
    return b*b - 4*a*c


print(discriminant(4, 2, -1))
print(discriminant(4, 2, 1))
print(discriminant(1, 4, 4))

1.2. Solution q2

import math # May be used in your answers

def discriminant(a, b, c):
    return b*b - 4*a*c


# - If the discriminant D<0, then the function has no roots.
# - If D = 0, then the parabola has one root, namely -b/2a.
# - If D > 0, then the parabola has two roots, namely -b-math.sqrt(D)/2a and
#             -b+math.sqrt(D)/2a.

def roots(a, b, c):
    D = discriminant(a, b, c)
    if D < 0:
        print("No roots")
    elif D == 0:
        root = -b / (2*a)
        print("One root:", round(root, 3))
    else:
        root1 = (-b - math.sqrt(D))/(2*a)
        root2 = (-b + math.sqrt(D))/(2*a)
        print("Two roots:", round(root1, 3), ",", round(root2, 3))


roots(4, 2, -1)
roots(4, 2, 1)
roots(1, 4, 4)

1.3. Some common issues

  • Difference between print and return
    • Bottom line: use return, unless we explicitly otherwise.
  • Don't use input with Codegrade
    • Codegrade tests your functions with its own inputs.
    • (So also don't change the name of the functions!)
  • Don't re-assign values to parameters inside your function
a = 6

def adding(a,b):
    a = 5
    return a + b

print(adding(a,3))

print(a)
  • If you want to change the value of a variable, don't forget to use an assignment statement!
def exchange(deposit,rate):
    a = deposit/rate
    round(a,2)
    return a

print(exchange(6000,4200))
  • If you want to call a function, then you HAVE to write the function name, followed by parentheses, and list all the values for the function's inputs!
def discriminant(a, b, c):
    return b*b - 4*a*c

def roots(a, b, c):
    d = discriminant(a, b, c) # This is not how you call a function
def printing_hi():
    print("hi")

printing_hi()

2. For loop and range

Let's continue our quest towards automation.

Most languages offer the following two types of iteration commands:

  • for : Iterate a predefined number of times.
    • This is also known as a definite iteration.
  • while : Keep on iterating until the condition is false.
    • This is known as an indefinite iteration.
for <variable> in <iterable>:
    <statement>
    ...
    <statement>

The <variable> will successively pass along all values in <iterable>, and for each item all indented statements will be executed.

In Python, an <iterable> can be: Strings, Lists, Tuples, Dictionaries, Sets, Files, and more.

for i in "02468":
    print(i)


For now, we are gonna start using the for-loop with the function range().

range() generates an iterable that you can use in a for loop.

Below you can find several examples of syntax for using the range() function.

The basic logic is

range(start, end, step)

Where the iterable will contain integers starting with start, going up with step, up to - but not including end.

for i in range(4):
    print(i)

for i in range(1, 4):
    print(i)
for i in range(0,10,2):
    print(i)
for i in range(4,0,-1):
    print(i)

Bottom line

  • When do you use a for-loop in combination with the range() function?
    • When you are interested in going thorugh a series of Integers.
  • When do you use a for-loop in combination with a string?
    • When you are interested in visiting each character

2.1. Useful tips and tricks with iteration

When you are repeatingly redefining a variable, for example like this …

count = 0
for i in "programming your world":
    if i == "r":
      count = count + 1
print(count)

… you can also just write:

count = 0
for i in "programming your world":
    if i == "r":
      count += 1
print(count)

In the same way …

count = count + 1
count += 1

count = count - 1
count -= 1

count = count * 2
count *= 2

Created: 2025-03-03 ma 21:21