Download presentation

Presentation is loading. Please wait.

1
Week 3 Lab1 Review Dina A. Said dasaid@ucalgary.ca

2
Mathematical operations Suppose that x= 7, y =2 Z= x + y =9 Z= x-y = 5 Z= x*y = 14 Z= x / y = 3 (because both of them are integers) Z= 7.0 / 2.0 = 7 / 2.0 = 7.0 / 2 = 3.5 Z= x % y = 1 (the reminder of the division) 3/23/2009CPSC203- Wee3 Lab1

3
Mod operation The result in the reminder of the division 10%2= 0 11%2 =1 15 % 3 =0 17 % 3 = 2 16 % 3 = 1 To know if y is divisible by x, check if y % x =0 X is even if x % 2=0 X is odd if x% 2 =1 3/23/2009CPSC203- Wee3 Lab1

4
Notes x =2 z=1 z = z+ x = 3 z+= x = 3 z=z+x is the same as z+=x 3/23/2009CPSC203- Wee3 Lab1

5
Parameters passing def myFunc(name, ID): Print name, Id When u execute myFunc in the black window: myFunc(“John”, 26378) def printList(myList): for i in myList: print i When you execute printList in the black window: printList([1,2,3]) printList([7]) 3/23/2009CPSC203- Wee3 Lab1

6
Note To know the length of a list, use command len(myList) def printList(myList): Print “You passed”, len(myList), “ parameters” for i in myList: print i 3/23/2009CPSC203- Wee3 Lab1

7
If-else condition if test1: statements1 elif test2: statements2 else: statements3 IMP: Don’t forget : after the condition For equal test, you must use “== “e.g. if x==y There is no condition after else 3/11/2009CPSC203- Week2- Lab27

8
Example Write a function that accepts a number x as a parameter and checks if this number if positive, negative, or zero Think before looking to answer 3/23/2009CPSC203- Wee3 Lab1

9
3/23/2009CPSC203- Wee3 Lab1 def check(x): if (x>0): print "Positive" elif (x<0): print "Negative" else: print "Zero"

10
For loop for target in object: statements Example: modify the previous program to accept a list of numbers. Do a for loop to check whether each number in the list is positive, negative, or zero Test your program with check([-4,7,0,-1,9]) 3/23/2009CPSC203- Wee3 Lab1

11
3/23/2009CPSC203- Wee3 Lab1 def check(list): for x in list: if (x>0): print "Positive" elif (x<0): print "Negative" else: print "Zero"

12
Example Modify the previous program to count the number of even numbers in the list 3/23/2009CPSC203- Wee3 Lab1

13
3/23/2009CPSC203- Wee3 Lab1 def check(list): count=0 for x in list: if (x>0): count=count+1 print "No. of positive numbers is ", count

Similar presentations

OK

SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION

SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on introduction to object-oriented programming vs procedural programming Ppt on leadership development training Ppt on service oriented architecture diagram Ppt on history of australia prisoners Ppt on centring prayer Ppt on pricing strategy in retail Ppt on fdi in retail sector 2011 Ppt on if clauses in english grammar Ppt on advanced power electronics Ppt on different types of forests images