Skip to content

Loops changes #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Loops/Break keyword/task.md
Original file line number Diff line number Diff line change
@@ -5,7 +5,9 @@ always be `True`, such a loop becomes infinite. The most straightforward way to
an infinite loop is to use `while True`. The `break` keyword is used to
escape the loop.

For more structured and detailed information, you can refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/6302#break).

### Task
Write a condition to exit the loop on its third iteration after evaluating if `animal`
is equal to `elephant`. Use the `break` statement. The `zoo` list in the end should contain
only `lion` and `tiger` (because you `pop` all the other ones in the loop).
3 changes: 3 additions & 0 deletions Loops/Continue keyword/task.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,9 @@ the current iteration only and proceed right to the next one. The loop returns t
`while` statement and evaluates the condition again. If there is no next item,
the `else` clause is executed if it is present.

For more structured and detailed information, you can refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/6302#continue).

### Task
Print only the odd numbers `1, 3, 5, 7, 9`.

<div class='hint'>Use the <code>%</code> operator to check if <code>x</code> is even.</div>
3 changes: 1 addition & 2 deletions Loops/Else with loops 2/task.md
Original file line number Diff line number Diff line change
@@ -18,8 +18,7 @@ The `else` statement will only be executed if `n` is a prime number, i.e. the `i
`for` loop.

### Task

Inside the [function](course://Functions/Definition) `contains_even_number()`, write a `for` loop that would iterate over a list `lst` and, if an even element is found,
Inside the [function](course://Loops/Else with loops 2) `contains_even_number()`, write a `for` loop that would iterate over a list `lst` and, if an even element is found,
print `f"List {lst} contains an even number."` and exit the loop, if no such element is found, print `f"List {lst} does not contain an even number."`.

<div class="hint">
3 changes: 3 additions & 0 deletions Loops/Else with loops/task.md
Original file line number Diff line number Diff line change
@@ -34,6 +34,9 @@ Remember, an `else` after an `if` statement is skipped and NOT executed if the e
`if` is `True`, while in the case of loops, an `else` clause is executed after the loop itself
is completed (unless there was a `break` in there somewhere).

For more structured and detailed information, you can refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/6302).

### Task
In the code editor, add two lines of code to the second loop to make sure the loop only prints
numbers 1 and 2 and never prints the phrase `"for loop is done"`.

3 changes: 3 additions & 0 deletions Loops/For loop/task.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@ for each item. When the items are exhausted, the loop terminates.

You can read more about the `for` statement on <a href="https://docs.python.org/3/reference/compound_stmts.html#the-for-statement">this page</a> of Python Documentation.

For more structured and detailed information, you can also refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/6065).

### Task
Print each prime number from the `primes` list using the `for` loop. A prime
number is a natural number greater than `1` that has no positive divisors
other than `1` and itself.
3 changes: 3 additions & 0 deletions Loops/List Comprehension/task.md
Original file line number Diff line number Diff line change
@@ -28,6 +28,9 @@ Output:
```
List comprehensions are also more efficient computationally than a `for` loop.

For more structured and detailed information, you can refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/6315).

### Task
In the code editor, use list comprehension to build `my_efficient_list` from the elements of `my_inefficient_list`
by adding $10$ to each of them. For example, the first element of `my_inefficient_list` is $1 + 10 = 11$,
so the first element of `my_efficient_list` should be $11 + 10 = 21$, and so on.
1 change: 1 addition & 0 deletions Loops/Loop over a string/task.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
As we discussed earlier, strings in Python are in many ways similar to lists.
You can iterate over a string just like you would iterate over a list.

### Task
Use a loop to count how many characters `hello_world` contains. Store
this number in the variable `length`.

2 changes: 2 additions & 0 deletions Loops/Nested List Comprehension/task.md
Original file line number Diff line number Diff line change
@@ -33,6 +33,8 @@ Output:
[[0, 2, 4, 6, 8], [0, 2, 4, 6, 8], [0, 2, 4, 6, 8]]
```

For more structured and detailed information, you can refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/6938#nested-list-comprehension).

### Task

Create a $10×10$ `matrix` such that each row (sublist) contains **characters** 0–9 from
2 changes: 2 additions & 0 deletions Loops/Nested for Loop/task.md
Original file line number Diff line number Diff line change
@@ -29,7 +29,9 @@ Note that any type of loop can be nested inside another loop.
For example, a [`while` loop](course://Loops/While loop) (see further) can be nested inside a `for` loop, or vice versa.
</details>

For more structured and detailed information, you can refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/6065#nested-loop).

### Task
You are given a tic-tac-toe board of 3x3, your task is to print every position. Coordinates along each side
are stored in the list `coordinates`. The output should be:
```text
3 changes: 3 additions & 0 deletions Loops/While loop/task.md
Original file line number Diff line number Diff line change
@@ -3,7 +3,10 @@
A `while` loop is somewhat similar to an `if` statement: it executes some code if some
condition is `True`. The key difference is that it will continue to execute indented
code for as long as the condition is `True`. If the expression is `False`, the loop terminates.

For more structured and detailed information, you can refer to [this Hyperskill knowledge base page](https://hyperskill.org/learn/step/5940).

### Task
Print all squares from `1` to `9` `(1, 4, ... , 81)`. Use the `number` variable in a
`while` loop.