Thinking Like a Programmer•Interactive notebook lesson•Free
break continue pass
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
Break Continue Pass
Codepython
counter = 10
while counter > 5:
# IF the counter is 7, break out of the loop
if counter == 7: break
print(f"the value of counter: {counter}")
counter -= 1 # Same as saying counter = counter - 1
Output
the value of counter: 10
the value of counter: 9
the value of counter: 8
Codepython
counter = 10
while counter > 5:
counter -= 1 # Same as saying counter = counter - 1
# IF the counter is 7, dont do anything for that value or loop
# when ccounter is 7, skip to next loop
if counter == 7: continue
print(f"the value of counter: {counter}")
Output
the value of counter: 9
the value of counter: 8
the value of counter: 6
the value of counter: 5
Codepython
for i in [1,2,7,9]:
if i == 7: pass
Codepython
def func1(array1):
print(f"this is a custom function: {array1}")Codepython
func1(8)Output
this is a custom function: 8
Codepython
func1(["high", "low"])Output
this is a custom function: ['high', 'low']
Codepython
func2 = lambda vary: print(f"the value of vary is : {vary}")
func2(["a" , "b", "c"])Output
the value of vary is : ['a', 'b', 'c']
Codepython
func3 = lambda x,y: x+y
func3(5,6)Output
11Codepython
(lambda x,y: x+y)(7,8)Output
15Codepython
def function_1(a,b):
""""This function returns the summation of two numbers if the result is even. If result is odd we dont know what to do."""
if (a+b)%2 == 0:
return a+b
else: pass
#I havent' thought of the else block
# so to avoid error i'll say to python
# pass
# meaning that i'll write my code here later onCodepython
function_1(2,9)Codepython
function_1(3,10)Codepython
Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →