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 is OK >>> ================================ 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 #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 >>> ================================ RESTART ================================ #Checking Collatz conjecture for a range of numbers (from 1 to ...) >>> Check Collatz conj. from 1 to ...10 1 1 is OK 2 1 2 is OK 3 10 5 16 8 4 2 1 3 is OK 4 2 1 4 is OK 5 16 8 4 2 1 5 is OK 6 3 10 5 16 8 4 2 1 6 is OK 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 7 is OK 8 4 2 1 8 is OK 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 9 is OK 10 5 16 8 4 2 1 10 is OK #Testing range() >>> for m in range(10): print(m) 0 1 2 3 4 5 6 7 8 9 >>> for m in range(1,11): print(m) 1 2 3 4 5 6 7 8 9 10 >>> for m in range(1,11,2): print(m) 1 3 5 7 9 >>> for m in range(10, -1, -3): print(m) 10 7 4 1 #Replacing the outer while loop with a for loop >>> ================================ RESTART ================================ >>> Check Collatz conj. from 1 to ...10 1 1 is OK 2 1 2 is OK 3 10 5 16 8 4 2 1 3 is OK 4 2 1 4 is OK 5 16 8 4 2 1 5 is OK 6 3 10 5 16 8 4 2 1 6 is OK 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 7 is OK 8 4 2 1 8 is OK 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 9 is OK 10 5 16 8 4 2 1 10 is OK #lists # lists in Python can contain items of different types >>> lst = [1,2,3.14,"hi", True] >>> lst[0] 1 >>> lst[4] True >>> lst[5] Traceback (most recent call last): File "", line 1, in lst[5] IndexError: list index out of range >>> lst[-1] True #the empty list >>> empty_lst = [] >>> empty_lst[1] Traceback (most recent call last): File "", line 1, in empty_lst[1] IndexError: list index out of range >>> empty_lst[0] Traceback (most recent call last): File "", line 1, in empty_lst[0] IndexError: list index out of range #list slicing - creates a new list! Does not change the original one >>> lst [1, 2, 3.14, 'hi', True] >>> lst[1:3] [2, 3.14] >>> lst[1:5:2] [2, 'hi'] >>> lst [1, 2, 3.14, 'hi', True] >>> lst2 = lst[1:5:2] >>> lst2 [2, 'hi'] #operator + for lists concatenation - creates a new list! >>> lst [1, 2, 3.14, 'hi', True] >>> lst2 = [-2,5] >>> lst + lst2 [1, 2, 3.14, 'hi', True, -2, 5] >>> lst2 [-2, 5] >>> lst [1, 2, 3.14, 'hi', True] >>> lst2 [-2, 5] #can be used to extend an existing list but is less efficient than list.extend() >>> lst2 = lst2 + [10,100] >>> lst2 [-2, 5, 10, 100] #creating a nested-list >>> lst2 = lst2 + [[3,4,5], 6] >>> lst2 [-2, 5, 10, 100, [3, 4, 5], 6] >>> lst2[4][0] 3 >>> lst2[4] [3, 4, 5] >>> lst2[4][0] 3 #the method append for lists appends an item to the end of an existing list. It changes the original list. >>> lst [1, 2, 3.14, 'hi', True] >>> lst.append(100) >>> lst [1, 2, 3.14, 'hi', True, 100] #the method extend for lists concatenates a given list to the end of an existing list. It changes the original list. >>> lst.extend(10) Traceback (most recent call last): File "", line 1, in lst.extend(10) TypeError: 'int' object is not iterable >>> lst.extend([90,90]) >>> lst [1, 2, 3.14, 'hi', True, 100, 90, 90] >>> lst.append([90,90]) >>> lst [1, 2, 3.14, 'hi', True, 100, 90, 90, [90, 90]#len] #len >>> len(lst) 9 #sum - only for numeric lists >>> sum([10,2.5, 45, -2.4]) 55.1 #sorting a list: #sorted() creates a new list with the same items in a sorted order. It does not change the oiriginal list. #sort method for lists sorts the list items in-place (changing the original list) >>> lst = [10,2.5, 45, -2.4] >>> lst [10, 2.5, 45, -2.4] >>> sorted(lst) [-2.4, 2.5, 10, 45] >>> lst [10, 2.5, 45, -2.4] >>> lst.sort() >>> lst [-2.4, 2.5, 10, 45] >>> lst = ["abc", "az", "mic", "123"] >>> lst.sort() >>> lst ['123', 'abc', 'az', 'mic'] #going over all list items (we saw 3 different methods) 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 ================================ >>> 1 name 5 7 8 1 name 5 7 8 1 name 5 7 8 >>> #running our count_grades.py code >>> ================================ RESTART ================================ >>> How many grades? 4 Enter a grade 78.6 Enter a grade 94 Enter a grade 100 Enter a grade 40.6 3 grades are above the average 78.30000000000001 #examples for list comprehension >>> l = [1,2,3,4,5] >>> l [1, 2, 3, 4, 5] >>> l2 = [x for x in l if x<4] >>> l2 [1, 2, 3] >>> l3 = [2*x for x in l if x<4] >>> l3 [2, 4, 6] >>> l4 = [x**3 for x in l if x**2 >10] >>> l4 [64, 125] >>> l5 = [1 for x in l if x**2 >10] >>> l5 [1, 1] >>> ================================ RESTART ================================ >>> How many grades? 3 Enter a grade 10 Enter a grade 90 Enter a grade 100 2 grades are above the average 66.66666666666667 >>> #calling the function max2 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 ================================ >>> >>> max2(10,14) 14 >>> max2(14,10) 14 >>> x = 10 >>> y = 14 >>> max2(x,y) 14 >>> help(max2) Help on function max2 in module __main__: max2(a, b) max(float,float) ---> float return the maximum od a and b >>> type(max2) >>> c = max2(4, 7) >>> c 7 #Short circuit evaluation >>> lst = [] >>> True or lst[10] True >>>