Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

break pass continue python

# ---------------------------
# -- Continue, Break, Pass --
# ---------------------------

myNumbers = [1, 2, 3, 5, 7, 10, 13, 14, 15, 19]

# Continue ==> Skip The ( Specific Element ) And Continue 

for numbers in myNumbers :

  if numbers == 13 :

      continue

  print( numbers )

print( "_" * 100 ) # -- Separator --

# Pass ==> Skip Any Program If I Completed Or Not

for numbers in myNumbers :
  pass

print( "Here We Have 'pass'." )

print( "_" * 100 ) # -- Separator --

# Break ==> Stop The Function

for numbers in myNumbers:

  if numbers == 13:

    break

  print( numbers )
  
Comment

continue and break in python

while True:
    line = input('Write something: ')
    if not line == '':    # if the line variable is not empty, run the code block
        print(line)
        continue    # Continues back to the beginning of the while loop
    else:  
        break # if the line variable is empty come out the loop and run the next code
print('End of the program')
Comment

Example of break, continue and pass statements in python

def main():
    check = input("Are you a new student(yes/no): ")
    if check == "yes":
        pass
    elif check == "no":
        totalMarks = 0
        numOfSub = 0
        while True:
            marks = input( 'Marks for subject ' + str(numOfSub +1) + ": ")
            if marks == '':
                break
            marks = float(marks)
            if marks < 0 or marks >100:
                print("Invalid marks")
                continue
            numOfSub = numOfSub+1
            totalMarks = totalMarks + marks
        percentage = totalMarks/numOfSub
        print("Total marks: ", totalMarks)
        print("Number of Subject is: ", numOfSub)
        print("Percentage: ", round(percentage,2))
    else:
        print("enter a valid response")
main()
Comment

break continue pass in python

# break - used to terminate the loop or statement
# continue - skip the current iteration and continue from the next
# pass - continue the statement, often used as a placeholder to execute
# 		 empty loops/statements
Comment

PREVIOUS NEXT
Code Example
Python :: change value in dataframe 
Python :: sort 2d list python 
Python :: pyaudio 
Python :: python sort array by lambda 
Python :: whatsapp bot python code 
Python :: panda 
Python :: find distance between two points in python 
Python :: bounding box in matplotlib 
Python :: remove dict python 
Python :: list from dataframe python 
Python :: how to add element to list value in a dict python 
Python :: python libraries 
Python :: how to download chatterbot 
Python :: python check for exception 
Python :: python string: .replace() 
Python :: python bot 
Python :: python conditional statement 
Python :: if we use list in the dictionary 
Python :: function python 
Python :: joining two lists in python using for loop 
Python :: count python 
Python :: what does tuple mean in python 
Python :: python socket get client ip address 
Python :: discord.py 
Python :: jquery datepicker disable 
Python :: read header of csv file python 
Python :: python sort list case insensitive 
Python :: python how to get rid of spaces in print 
Python :: django connexion session time 
Python :: flask_jinja structure 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =