Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming Fundamentals

Similar presentations


Presentation on theme: "Computer Programming Fundamentals"— Presentation transcript:

1 Computer Programming Fundamentals
Loops

2 Last Time The ‘if’ statement Lists ‘else’ ‘elif’ Conditions
individual items L[2] slices L[1:5] 2

3 ‘if’and‘else’ We can use‘if’ and ‘else’to express alternatives paths:
if x > y : bigger = x print("chose x") else: bigger = y print("chose y") print("Continue from here...") 3

4 The ‘elif’ statement When there are more than two cases we use the ‘elif’ statement if score < 10: print("Pathetic") elif score < 20: print("My gran could do better") elif score < 50: print("OK") else: print("Legendary!!!") 4

5 Today’s Agenda The ‘while’ loop More about lists The ‘for’ loop
Formatting strings 5

6 The ‘while’ loop We can make the computer repeat statements
One way to do this is to use a ‘while’ loop For example The body of the loop is executed while the condition x > 0 is true a = 1 x = 10 while x > 0 : a = a*x x = x - 1 6

7 Flow chart for ‘while’ loop
Start Loop action while ... : Finish 7

8 More about lists Suppose our list is:
L = ["Abc", 1, 3.8, True, "XYZ", 21] We can use negative index numbers Then L[-2] L[-3] L[-3:-1] is "XYZ" is True is [True, "XYZ"] 8

9 More about lists L = ["Abc", 1, 3.8, True, "XYZ", 21]
Slices have sensible default values: L[-2:] is ["XYZ", 21] L[:3] is ["Abc", 1, 3.8] L[:] is the whole list L[:i]+L[i:] is always equal to L Slices are more forgiving than indices: L[99] would raise an error L[-1:99] is [21] 9

10 More about lists We can construct lists in various ways: From a string
Q = list("Abc") ['A','b','c'] Using the built-in function ‘range’ N = list(range(4)) [0,1,2,3] 10

11 The ‘for’ loop Another way to write a loop is to use a ‘for’ loop
This kind of loop iterates over the elements of a list L = ["Abc", 1, 3.8, True, "XYZ", 21] for e in L: print(e) 11

12 More about ‘range’ We can use the ‘range’ built-in function to make a ‘for’ loop iterate over integers: for x in range(4): print(x) This will print the numbers 0,1,2,3 We can use other parameters: range(4,0,-1) Would give 4,3,2,1 12

13 More about loops You can get out of your loop early using the keyword: ‘break’ for x in range(len(L)): if L[x] == "value": print(x) break The keyword ‘continue’ finishes the current iteration 13

14 String formatting Often we will want to make sure the output sent to the console formats nicely Use these string functions myString.ljust(12) myString.center(12) myString.rjust(12) Another useful one is: print("".join(L)) 14

15 String formatting example
myString = "<string>" print("< >"*3) print(myString.ljust(12),end="") print(myString.center(12),end="") print(myString.rjust(12)) < >< >< > <string> <string> <string> 15

16 Exercises Some things to try on the console Writing a for loop
A multiplication table Writing a ‘Hangman’ game 16


Download ppt "Computer Programming Fundamentals"

Similar presentations


Ads by Google