11

SPECIAL KEYWORDS

by

The Education Machine

[support@theeducationmachine.com]

BREAK

Many times, we want to stop the execution of the loop if some condition is fulfilled but loops doesn't stop automatically. They need a command from user to stop the execution. In such scenarios, 'Break' keyword is used.

Break terminates the execution of the loop(NOTE IT, just stops execution of LOOP) but process remaining statements in the program.

Break statement can be used in both 'while' and 'for' loops.

In case there are nested loops in the program. In such cases, break terminates the execution of the inner most loop and continue to process remaining statements of the program.

To print the numbers of a certain range.

  for i in range(0,10):
      print(i)

Output:
0
1
2
3
4
5
6
7
8
9

Nested loop: When there is a loop inside a loop. As shown below:

  for i in range(0,5):
      print(f"i: {i}")
      for j in range(10,15):
          print(f"j: {j}")

Output:
i: 0
j: 10
j: 11
j: 12
j: 13
j: 14
i: 1
j: 10
j: 11
j: 12
j: 13
j: 14
i: 2
j: 10
j: 11
j: 12
j: 13
j: 14
i: 3
j: 10
j: 11
j: 12
j: 13
j: 14
i: 4
j: 10
j: 11
j: 12
j: 13
j: 14

Basic usage of break statement. Break keyword is used when the user wants to stop the loop at certain point.
Like in below cell, the user wants to stop the loop at letter "h".So, with the use of if condition and reak keyword the loop has stopped at "h".

  name = 'kashish'
  
  for i in name:
      if i == 'h':
          break
      print(i)

Output:
k
a
s

How would you check vowels in a string, the moment you see a vowel, break the loop.

  for i in "kashish":
      if i in ['a','e','i','o','u']:
          break
      print(i)
  
  print("the letter is matching with vowel",i)

Output:
k
the letter is matching with vowel a

Print the numbers till the time the number from 0-50 are not magic and break the loop if number is a magic number.

  magic=7
  for i in range(51):
      if i == magic:
          print(i, "is the magic number")
          break
      else:
          print(i)

Output:
0
1
2
3
4
5
6
7 is the magic number

Count e in the string “python is efficient and easy”, if the count of e is gt 2 then stop the execution.
Note: There are 3 ‘e’ in the string.

  count=0
  for i in "python is efficient and easy":
      if i=="e":
          count+=1
          if count > 2:
              break
          print(i,count)
  print("ending loop as count is above 2")

Output:
e 1
e 2
ending loop as count is above 2

Check in how many years my investment of 1L will be doubled to 2L or higher.

  p=100000
  year=0
  for i in range(0,50):
      p+=p*.09
      year+=1
      print(f"Year: {year}, Principal: {p}")
      if p>=200000:
          break

Output:
Year: 1, Principal: 109000.0
Year: 2, Principal: 118810.0
Year: 3, Principal: 129502.9
Year: 4, Principal: 141158.161
Year: 5, Principal: 153862.39549
Year: 6, Principal: 167710.0110841
Year: 7, Principal: 182803.912081669
Year: 8, Principal: 199256.2641690192
Year: 9, Principal: 217189.32794423093

CONTINUE

In cases where we want to just skip the execution of the remaining statements of the loop based on a condition and move the pointer to the beginning of loop to process next iteration, 'continue' statement can be used.

Continue returns the control to the beginning of the loop.

The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

The continue statement can be used in both while and for loop.

Check if vehicle number is available to be taken by someone.

  numbers_taken = [4013, 4016, 4020, 4023, 4024]
  
  for i in range(4012, 4026):
      if i in numbers_taken:
          continue
      print(i,"Car number is available, pay amount to take this number")

Output:
4012 Car number is available, pay amount to take this number
4014 Car number is available, pay amount to take this number
4015 Car number is available, pay amount to take this number
4017 Car number is available, pay amount to take this number
4018 Car number is available, pay amount to take this number
4019 Car number is available, pay amount to take this number
4021 Car number is available, pay amount to take this number
4022 Car number is available, pay amount to take this number
4025 Car number is available, pay amount to take this number

Print all odd numbers between 1-10 using continue statement.

  for i in range(1,11):
      if i%2 == 1:
          print(i," is an odd number")
      continue
      print(i," is an even number")

Output:
1 is an odd number
3 is an odd number
5 is an odd number
7 is an odd number
9 is an odd number

PASS

When we want to skip the execution of the loop or if-else conditions, pass keyword can be used.

In Python programming, pass is a null statement. The difference between a comment and pass statement in Python is that, while the interpreter ignores a comment entirely, pass is not ignored.

However, nothing happens when pass is executed. It results into no operation.

  a = 10
  if a == 10:
  pass



Check if vehicle number is available to be taken by someone.

  numbers_taken = [4013, 4016, 4020, 4023, 4024]
  
  for i in range(4012, 4026):
      if i in numbers_taken:
          pass
      print(i,"Car number is available, pay amount to take this number")

Output:
4012 Car number is available, pay amount to take this number
4013 Car number is available, pay amount to take this number
4014 Car number is available, pay amount to take this number
4015 Car number is available, pay amount to take this number
4016 Car number is available, pay amount to take this number
4017 Car number is available, pay amount to take this number
4018 Car number is available, pay amount to take this number
4019 Car number is available, pay amount to take this number
4020 Car number is available, pay amount to take this number
4021 Car number is available, pay amount to take this number
4022 Car number is available, pay amount to take this number
4023 Car number is available, pay amount to take this number
4024 Car number is available, pay amount to take this number
4025 Car number is available, pay amount to take this number

Using pass in for loop.

  l=[1,2,3,44]
  for i in l:
      pass
  
  print(f"The last value of list is {i}")

Output:
The last value of list is 44