Keywords#
This page show basic scenarious of using different keywords in python.
Leave cycle (break)#
Lets you exit the cycle.
Nested loops#
What will happens if you put cycle incide two cycles? Will it leave both cycles or just the internal one?
It will leave only intertal loop.
So in the following example we will use the break
operator inside the internal cycle only if we loop to a variable of the internal cycle that is equal to or greater than 2. As a result, we go through all the operations provided by the outer loop, but each interation of the internal loop is ended when the loop variable enriches 2.
for i in range(4):
print("external loop", i)
for j in range(4):
if j >= 2:
break
print(" internal loop", j)
external loop 0
internal loop 0
internal loop 1
external loop 1
internal loop 0
internal loop 1
external loop 2
internal loop 0
internal loop 1
external loop 3
internal loop 0
internal loop 1
Solution the best solution for today is to organise a flag that will cause a break
operator in the external cycle.
So in the following example I have added a flag
that gets a True
value before exiting the internal loop, and if after exiting the internal loop flag==True
it will cause exiting the external cycle as well.
No better solution has been found to date.
for i in range(4):
print("external loop", i)
for j in range(4):
if j >= 2:
flag = True
break
print(" internal loop", j)
if flag: break
external loop 0
internal loop 0
internal loop 1
Is in array (in)#
This keyword lets you check if an element exists in an array. Here’s a simple example of how it works with a list
. For more details, please refer to the specific page.
test_lst = [2, 7, 2, 5]
print(f"5 in {test_lst} >>", 5 in test_lst)
print(f"8 in {test_lst} >>", 8 in test_lst)
5 in [2, 7, 2, 5] >> True
8 in [2, 7, 2, 5] >> False
Indexing ([])#
Negative index#
In Python, all indices have an alternate index which is negative. Suppose we have a list of n
elements, so elements will have indexes from 0
to n-1
and from -n
to -1
.
The following table puts positive and negative indexing in python into a visual correlation.
Positive indexing | 0 | 1 | ... | n-1 |
---|---|---|---|---|
Negative indexing | -n | -n+1 | ... | -1 |
It can be very useful to access items from the end of the collection. So in the last example, I easily accessed the penultimate item in the list.
list(range(50))[-2]
48
Assert#
assert <statement>
is a keyword used to raise an error if <statement>
evaluates to False
. The main purpose of this keyword is using it in the unittests.
If you pass a statement that evaluates to True
, everything will proceed without any issues.
assert True
But False
lead to the AssertionError
.
assert False
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Cell In[7], line 1
----> 1 assert False
AssertionError:
yield#
The yield
keyword is an alternative to the return
statement for specifying the output of a function. A function that uses yield
will return a generator, which can be iterated over to produce values one at a time.
Consider the following example: a function defined with yield
returns a generator. By iterating over this generator, you will retrieve the result of each subsequent yield
statement.
def func(lst):
for i in lst:
yield i
gen = func([1,2,3])
print(type(gen))
print(next(gen), next(gen))
<class 'generator'>
1 2