Python3 and testing for loops

Hello,

I have a for loop in a function:

def off_on():
    pos = 0
    for (pos == 0, pos <= 180, pos += 1):
        pwm_controller.set_pwm_frequency(66)
        pwm_controller.set_servo_angle(15, pos)
        sleep(2)

    for (pos == 180, pos >= 0, pos -= 1):
        pwm_controller.set_pwm_frequency(66)
        pwm_controller.set_servo_angle(15, pos)
        sleep(2)

In the for loop, I have tried alterations of the source so far. I have tried to handle it this way:

def off_on():
    pos = 0
    for (pos = 0, pos <= 180, pos += 1):
        pwm_controller.set_pwm_frequency(66)
        pwm_controller.set_servo_angle(15, pos)
        sleep(2)

    for (pos = 180, pos >= 0, pos -= 1):
        pwm_controller.set_pwm_frequency(66)
        pwm_controller.set_servo_angle(15, pos)
        sleep(2)

Neither are seemingly working. In my function called off_on, I cannot seem to find the correct way Python3 handles the -= or += association to the variable in the for loop.

Seth

P.S. If anyone sees my error, please let me know.

Python for-loops are not the same as for-loops in other languages. I believe you want something like this:

for pos in range(181):
    ...

for pos in reversed(range(181)):
    ...

See also the Python tutorial’s section on for-loops.

2 Likes

Hello Sir,

Thank you. I realized this a long time ago, i.e. still a bit of a mystery to me.

Seth

P.S. @sweeneyde , you probably save me tons of time researching small tutorials in Python3 to handle for loops. Thank you.

Hello Sir,

To @sweeneyde or anyone else watching and using this forum, I have another issue w/ positional argument’ing.

"The second function pos_only_arg is restricted to only use positional parameters as there is a / in the function definition: " Taken from here, https://docs.python.org/3/tutorial/controlflow.html#function-examples .


>>>
>>> pos_only_arg(1)
1

What is a positional parameter in the above listed quotation from the for loop section of the Python Docs?

I see the course of action it takes in source but that phrase is a bit offloading to me.

Seth

P.S. If I was to use this positional usage in source, like w/ for loops, am I stuck to using integers instead of characters and integers?

Suppose we have a function

def divide(dividend, divisor):
    return dividend / divisor

Then I would be able to call the function in the standard way:

z = divide(1, 2)
print(z) # prints 0.5

I could also call the function using “keyword arguments”:

z = divide(dividend=1, divisor=2) # helps to tell which argument is which
print(z) # prints 0.5

For more complicated functions, this can be extremely useful in making the code clearer, since you don’t have to remember which argument comes first. You could put divide(divisor=2, dividend=1), and it would do the same thing.

Sometimes, you might see that this keyword-argument style is so much better looking for your function that you can forbid using standard positional arguments. You could write this

# dividend and divisor are keyword-only parameters
def divide2(*, dividend, divisor):
    return dividend / divisor

That way, every call to divide2 must use keyword parameters like divide2(dividend=1, divisor=2). Writing divide2(1, 2) won’t work.

In the other direction, you might decide that your function wouldn’t make sense with keyword arguments. For example, you could write this:

# The '/' makes x and y positional-only.
def maximum(x, y, /):
    if x > y:
        return x
    else:
        return y

You probably don’t want people to call your function like maximum(x=2, y=3), since “x” and “y” aren’t meaningful names. So putting the slash there ensures that you can only call this function like maximum(2, 3), so maximum(x=2, y=3) will not work.

1 Like

Yes they are! Many languages have Python-style loops, with minor differences in syntax.

  • Python: for value in array
  • Ruby: for value in array
  • Rust: for value in &array
  • Javascript: for (value in array)
  • Java: for (int value : array)
  • Go: for value := range array
  • Ada: for Value of Array loop
  • C++ 3.11: for (int value : array)
  • ColdFusion: for (value in array)
  • PHP: foreach ($array as $value)
  • Dart: for (final value in array)
  • Groovy: for (value in array)
  • Swift: for value in array
  • Windows Powershell: foreach ($value in $array)

Some of those languages are very well-known and popular.

Sorry, I get grumpy when people make out that Python is weird and unusual, when in truth Python-like features are found in dozens, maybe hundreds, of other languages.

3 Likes

Even in usual shells it is the same:

  • POSIX shell: for value in $whitespace_delimited_list
  • Bash / KornShell: for value in "${array[@]}"