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. #for loops >>> s = "michal" >>> for c in s: print (c*3) mmm iii ccc hhh aaa lll >>> for klm in s: print (klm*3) mmm iii ccc hhh aaa lll #Checking Collatz conjecture for a given number >>> ================================ 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 #Checking Collatz conjecture for a range of numbers (from 1 to ...) >>> ================================ RESTART ================================ >>> Checking 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 x in range(1, 11): print(x) 1 2 3 4 5 6 7 8 9 10 >>> for x in range(11): print(x) 0 1 2 3 4 5 6 7 8 9 10 >>> for x in range(1,11,3): print(x) 1 4 7 10 #Replacing the outer while loop with a for loop >>> ================================ RESTART ================================ >>> Checking 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 #the empty list >>> lst = [] >>> type(lst) >>> len(lst) 0 # lists in Python can contain items of different types >>> lst = [2, 4.5, print, "michal", True, [100,200]] >>> len(lst) 6 #accessing list items >>> lst[0] 2 >>> lst[5] [100, 200] >>> lst[-1] [100, 200] >>> lst[-2] True >>> type(lst[2]) >>> lst[2] = 3.14 >>> lst [2, 4.5, 3.14, 'michal', True, [100, 200]] >>> lst[8] Traceback (most recent call last): File "", line 1, in lst[8] IndexError: list index out of range >>> lst[-10] Traceback (most recent call last): File "", line 1, in lst[-10] IndexError: list index out of range #list slicing - creates a new list! Does not change the original one >>> lst[1:4] [4.5, 3.14, 'michal'] >>> lst [2, 4.5, 3.14, 'michal', True, [100, 200]] >>> lst1 = lst[1:4] >>> lst [2, 4.5, 3.14, 'michal', True, [100, 200]] >>> lst1 [4.5, 3.14, 'michal'] >>> lst[1:] [4.5, 3.14, 'michal', True, [100, 200]] >>> lst[:4] [2, 4.5, 3.14, 'michal'] >>> lst[0: 6: 2] [2, 3.14, True] >>> lst [2, 4.5, 3.14, 'michal', True, [100, 200]] #operator + for lists concatenation - creates a new list! #can be used to extend an existing list but is less efficient than list.extend() >>> lst [2, 4.5, 3.14, 'michal', True, [100, 200]] >>> lst2 = [900, 1000] >>> lst+ lst2 [2, 4.5, 3.14, 'michal', True, [100, 200], 900, 1000] #the method append for lists appends an item to the end of an existing list. It changes the original list. >>> lst.append(9999) >>> lst [2, 4.5, 3.14, 'michal', True, [100, 200], 9999] #the method extend for lists concatenates a given list to the end of an existing list. It changes the original list. >>> lst.extend([1,2,3]) >>> lst [2, 4.5, 3.14, 'michal', True, [100, 200], 9999, 1, 2, 3] >>> lst.append(["ff"]) >>> lst [2, 4.5, 3.14, 'michal', True, [100, 200], 9999, 1, 2, 3, ['ff']] #accessing an item of a nested list >>> lst[5] [100, 200] >>> lst[5][1] 200 #sum - only for numeric lists >>> numeric_lst = [100, 50.5, -90] >>> sum(numeric_lst) 60.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) >>> sorted_num_lst = sorted(numeric_lst) >>> sorted_num_lst [-90, 50.5, 100] >>> numeric_lst [100, 50.5, -90] >>> numeric_lst.sort() >>> numeric_lst [-90, 50.5, 100] #going over all list items (we saw 3 different methods) >>> ================================ 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: 79 enter a grade: 88 enter a grade: 94 enter a grade: 3 3 grades are above the average 66.0 #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 #print function returns None >>> x = print("r") r >>> x >>>