Python 3.2.5 (default, May 15 2013, 23:07:10) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ #Checking Collatz conjecture for a given number >>> Please enter a positive integer to apply Collatz algorithm: 7 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 7 is OK ( 16 iters) #Setting the "end" parameter of print() function to " " >>> ================================ RESTART ================================ >>> Please enter a positive integer to apply Collatz algorithm: 7 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 7 is OK ( 16 iters) >>> ================================ RESTART ================================ #Checking Collatz conjecture for a range of numbers (from 1 to ...) >>> Check Collatz conj. from 1 to ... 10 1 1 is OK ( 0 iters) 2 1 2 is OK ( 1 iters) 3 10 5 16 8 4 2 1 3 is OK ( 7 iters) 4 2 1 4 is OK ( 2 iters) 5 16 8 4 2 1 5 is OK ( 5 iters) 6 3 10 5 16 8 4 2 1 6 is OK ( 8 iters) 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 7 is OK ( 16 iters) 8 4 2 1 8 is OK ( 3 iters) 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 9 is OK ( 19 iters) 10 5 16 8 4 2 1 10 is OK ( 6 iters) #Testing range() >>> for i in range(1, 11): print(i) 1 2 3 4 5 6 7 8 9 10 >>> for i in range(11): print(i) 0 1 2 3 4 5 6 7 8 9 10 >>> for i in range(2, 11, 2): print(i) 2 4 6 8 10 #We replaced the outer while loop with a for loop >>> ================================ RESTART ================================ >>> Check Collatz conj. from 1 to ... 5 1 1 is OK ( 0 iters) 2 1 2 is OK ( 1 iters) 3 10 5 16 8 4 2 1 3 is OK ( 7 iters) 4 2 1 4 is OK ( 2 iters) 5 16 8 4 2 1 5 is OK ( 5 iters) >>> >>> #lists - an ordered data structure allowing random access (= accessing items in constant time, no matter in which index they are located) #the empty list >>> >>> lst = [] >>> type(lst) >>> len(lst) 0 >>> list(50) Traceback (most recent call last): File "", line 1, in list(50) TypeError: 'int' object is not iterable # lists in Python can contain items of different types >>> lst = [1, 4.5, True, "course", [70, 71], 800] >>> lst[0] 1 >>> len(lst) 6 >>> lst[5] 800 >>> lst[6] Traceback (most recent call last): File "", line 1, in lst[6] IndexError: list index out of range >>> lst[-1] 800 >>> lst [1, 4.5, True, 'course', [70, 71], 800] >>> lst[-1] = "michal" #list slicing - creates a new list! Does not change the original one >>> lst [1, 4.5, True, 'course', [70, 71], 'michal'] >>> lst[3:] ['course', [70, 71], 'michal'] >>> lst [1, 4.5, True, 'course', [70, 71], 'michal'] >>> lst1 = lst[3:] #operator + for lists concatenation - creates a new list! #Notice that lst1 , lst2 did not change >>> lst1 ['course', [70, 71], 'michal'] >>> lst2 = [4, 5, 6] >>> lst3 = lst1 + lst2 >>> lst1 ['course', [70, 71], 'michal'] >>> lst2 [4, 5, 6] >>> lst3 ['course', [70, 71], 'michal', 4, 5, 6] #Here lst3 changes because of the assignment #However the original items in lst3 are coppied to a new location in memory. #If you would like to append items to an existing list then operator + should not be used. (use instead, append(), extend() or operator +=) >>> lst3 = lst3 + ["michalkl"] >>> lst3 ['course', [70, 71], 'michal', 4, 5, 6, 'michalkl'] >>> lst3 ['course', [70, 71], 'michal', 4, 5, 6, 'michalkl'] #the method append for lists appends an item to the end of an existing list. It changes the original list. >>> lst2 [4, 5, 6] >>> lst2.append(1000) >>> lst2 [4, 5, 6, 1000] #creating a nested list >>> lst2.append([500]) >>> lst2 [4, 5, 6, 1000, [500]] >>> lst2[-1] [500] >>> lst2[-1][0] 500 #the method extend for lists concatenates a given list to the end of an existing list. It changes the original list. >>> lst2.extend([9,8,7]) >>> lst2 [4, 5, 6, 1000, [500], 9, 8, 7] >>> lst3 ['course', [70, 71], 'michal', 4, 5, 6, 'michalkl'] #sum ,len >>> numeric = [30, -1.5, 0, -90] >>> sum(numeric) -61.5 #sorting a list: #sorted() creates a new list with the same items in a sorted order. It does not change the original list. #sort method for lists sorts the list items in-place (changing the original list) >>> numeric [30, -1.5, 0, -90] >>> numeric.sort() >>> numeric [-90, -1.5, 0, 30] >>> numeric = [30, -1.5, 0, -90] >>> sorted(numeric) [-90, -1.5, 0, 30] >>> numeric [30, -1.5, 0, -90] #running our count_grades.py code >>> ================================ RESTART ================================ >>> How many grades?4 enter a grade:7 enter a grade:21.5 enter a grade:80 enter a grade:97 2 grades are above the average 51.375 #examples for list comprehension >>> lst = [i for i in range(10)] >>> lst [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> lst = [0 for i in range(10)] >>> lst [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] >>> lst = [i for i in range(10)] >>> [2*val for val in lst] [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] >>> [2*val for val in lst if val*6 < 30] [0, 2, 4, 6, 8] >>> lst [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #calling the function max2 >>> >>> max2 >>> type(max2) >>> x = 10 >>> y = 15 >>> z = max2(x, y) >>> z 15 >>> z = max2(x, 500) >>> z 500 >>> help(max2) Help on function max2 in module __main__: max2(a, b) max(float,float) ---> float return the maximum od a and b #short circuit evaluation >>> 3/0 Traceback (most recent call last): File "", line 1, in 3/0 ZeroDivisionError: division by zero >>> True or 3/0 True >>> 3/0 or True Traceback (most recent call last): File "", line 1, in 3/0 or True ZeroDivisionError: division by zero >>>