II (If, Elif, while, for, break, continue, pass)

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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")

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"**

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)


संबंधित स्टडी सेट्स

Nursing: Unlimited Possibilities and Unlimited Potential

View Set

1. Decontamination is needed in order to:

View Set

Dynamics of Personality Chapter 11 Engler

View Set

Markdown & Sale Price (Ch. 6 Review Part 2), Discount, Sale Price

View Set