Python If, Elif, while, for, break, continue, pass)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Iteration

When you iterate, you "go around again". Iteration is performed in Python by loops of statements. Ex. a = 1 while a < 10: print(a) a += 2

The Elif Keyword

A program can use the elif keyword to combine the functions of else and if in a single statement. The elif keyword simplifies the code and removes the need for nesting each successive command. Ex. if command==1: print("Command 1") elif command==2: print("Command 2") elif command==3: print("Command 3") else: print("Invalid command")

Nested conditions

A statement controlled by if conditions can contain if conditions. Ex. If command == 1: print("Command 1") else: if command ==2: print("Command 2") else: if command == 3: print("Command 3") else: print("Invalid command")

While

Python provides two iterating constructions, while and for. The while loop will perform statements while a condition is true. Ex. while True: print("Hello") while False: print("Hello") print("This is printed")

Break

The code controlled by the iteration can be of any size, and can contain nested loops (iterations) and if conditions. Ex. for in in range(1,21,2): if i==9: break print(i)

Continue

The continue statement causes an iteration to restart at the top with the next value. Ex. for i in range(1,11): if i==9: continue print(i)

For

The for construction allows a program to iterate (loop) through a collection of values. Ex. for ch in "Hello": print(ch) **ch is each letter in "Hello"**

The If Construction

The if construction allows the path through a program to vary depending on the values held in variables Ex. if age < 10: print("You are too young") else: print("Welcome aboard")

Pass

The pass statement can be regarded as a placeholder for code. Ex. if age < 10: pass else: print("Welcome Abroad")

Range

The range function provides a range that a for loop can iterate through. Ex. for i in range(10): print(i) for i in range(1,21,2): print(I)


Ensembles d'études connexes

Chapter 5: Regulations and Standards

View Set

Perguntas iguais! Chapter 4 and 3 end of chapter review & lecture notes

View Set

CH 4: Completing the Accounting Cycle

View Set

Foundations Chapter 9 Cultural Awareness

View Set

Chapter 7: Optimal Risky Portfolios (Review Questions)

View Set

Research Methods Test 3 Study Guide

View Set