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
Recap session

2 Agenda Variables Lists ‘if-elif-else’ Loops Modules and functions
‘while’ loop ‘for’ loop Modules and functions Putting it all together

3 Variables Chunks of the computer’s memory Each has a name
Starting with alphabetic character Then can be alphabetic or numeric character Cannot use keywords Each has a type int: integer bool: Boolean value str: string

4 Lists Lists are an extremely versatile Python data type
They are used frequently so it is important to get to know how to use them The elements of a list can be anything: myList = ["Abc", 1, 3.8, True] A list can even have no items: emptyList = []

5 Lists The elements of a list may be modified: Some other operations:
myList = ["Abc", 1, 3.8, True] myList[0] = "ABCD" Some other operations: myList.append("DEF") myList.extend(["GHI",99]) myList.clear() myList.pop(i) del myList[i]

6 ‘if-elif-else’ Use to pick different execution paths if score < 10:
print("Pathetic") elif score < 20: print("My gran could do better") elif score < 50: print("OK") else: print("Legendary!!!")

7 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

8 The ‘for’ loop Another kind of loop is a ‘for’ loop which iterates over the elements of a list 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 L = ["Abc", 1, 3.8, True, "XYZ", 21] for e in L: print(e)

9 Functions Functions are a way of packaging useful chunks of code
We give the code a name so it can be re-used We can tailor how the function behaves by the use of arguments The function may or may not return a value

10 Modules Modules are a convenient way to package groups of related functions Defining a module is very easy Create a new file – ‘shapes.py’ for instance Add function definitions to the file Example – ‘shapes.py’ # Utility functions for dealing with shapes def circleArea(radius=1): ... def rectangleInfo(width,height):

11 Putting it all together
Generating lottery numbers Generate random numbers in a range Need 6 different numbers 11

12 Exercises An exercise in using random numbers
Write a program to generate lottery numbers


Download ppt "Computer Programming Fundamentals"

Similar presentations


Ads by Google