Python 3.1.2 (r312:79360M, Mar 24 2010, 01:33:18) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "copyright", "credits" or "license()" for more information. >>> output = 7 >>> output = output + 5 >>> output 12 >>> output = 0 >>> for item in range(1,11): print(item) output = output + item 1 2 3 4 5 6 7 8 9 10 >>> output 55 >>> 5 / 4 1.25 >>> 8 //3 2 >>> 8.0 // 3 2.0 >>> output = 0 >>> for item in range(1, 10): print(item,item%3) output = output + (item % 3) 1 1 2 2 3 0 4 1 5 2 6 0 7 1 8 2 9 0 >>> 5 != 3 True >>> "fish" != 'chicken' True >>> "fish" != 'fish' False >>> 'a' in 'cat in the hat' True >>> 'at' in 'cat in the hat' True >>> 'cat in the hat'.count('a') 2 >>> 'cat' ['the','cat','in','the','hat'] Traceback (most recent call last): File "", line 1, in 'cat' ['the','cat','in','the','hat'] TypeError: string indices must be integers >>> 'cat' in ['the','cat','in','the','hat'] True >>> ['cat','in'] in ['the','cat','in','the','hat'] False >>> colors = {'apple':'red','chicken':'yellow'} >>> colors['apple'] 'red' >>> 'apple' in colors True >>> 'red' in colors False >>> ['apple','red'] in colors Traceback (most recent call last): File "", line 1, in ['apple','red'] in colors TypeError: unhashable type: 'list' >>> 'apple' in colors True >>> colors['apple'] 'red' >>> colors['banana'] Traceback (most recent call last): File "", line 1, in colors['banana'] KeyError: 'banana' >>> def look_up_value(key,dictionary): if key in dictionary: return(dictionary[key]) else: return(False) >>> look_up_value('apple') Traceback (most recent call last): File "", line 1, in look_up_value('apple') TypeError: look_up_value() takes exactly 2 positional arguments (1 given) >>> look_up_value('apple',colors) 'red' >>> look_up_value('banana',colors) False >>> def plus(item1,item2): return(item1+item2) >>> def many_pluses(list_of_numbers): output = 0 for number in list_of_numbers: output = output+number return(output) >>> many_pluses([1,2,57,100]) 160 >>> def many_pluses(list_of_numbers): output = 0 for number in list_of_numbers: output = plus(output,number) return(output) >>> many_pluses([1,2,57,100]) 160 >>> def my_modulus(number,modulus=5): return(number%modulus) >>> my_modulus(10) 0 >>> my_modulus(4) 4 >>> my_modulus(7) 2 >>> my_modulus(7,modulus=3) 1 >>> outstream = open('123.txt','w') >>> print(57,file=outstream) >>> print(57) 57 >>> outstream.close() >>> import os >>> os.cwd() Traceback (most recent call last): File "", line 1, in os.cwd() AttributeError: 'module' object has no attribute 'cwd' >>> os.getcwd() '/Users/adam/Documents' >>> output 9 >>> def sample_function(stuff): output = stuff print(stuff) >>> sample_function(57) 57 >>> output 9 >>> def sample_function(stuff): output = stuff print(output) >>> sample_function(57) 57 >>> output 9 >>> def sample_function(stuff): print(stuff,output) >>> sample_function(57) 57 9 >>> def sample_function2(stuff): global output output = stuff print(output) >>> output 9 >>> sample_function(57) 57 9 >>> sample_function2(57) 57 >>> output 57 >>> plus >>> def many_pluses(list_of_numbers): output = 0 for number in list_of_numbers: output = plus(output,number) return(output) >>> def testing_if(number_list): for item in number_list: print(item,end=': ') if (number%2)== 0: print('even') elif (number%3)== 0: print('three-ish') else: print('useless') >>> testing_if([1,2,3,4,5,6,100,47,59]) 1: Traceback (most recent call last): File "", line 1, in testing_if([1,2,3,4,5,6,100,47,59]) File "", line 4, in testing_if if (number%2)== 0: NameError: global name 'number' is not defined >>> def testing_if(number_list): for number in number_list: print(item,end=': ') if (number%2)== 0: print('even') elif (number%3)== 0: print('three-ish') else: print('useless') >>> testing_if([1,2,3,4,5,6,100,47,59]) 9: useless 9: even 9: three-ish 9: even 9: useless 9: even 9: even 9: useless 9: useless >>> def testing_if(number_list): for number in number_list: print(number,end=': ') if (number%2)== 0: print('even') elif (number%3)== 0: print('three-ish') else: print('useless') >>> testing_if([1,2,3,4,5,6,100,47,59]) 1: useless 2: even 3: three-ish 4: even 5: useless 6: even 100: even 47: useless 59: useless >>> def testing_if2(number_list): used = False for number in number_list: print(number,end=': ') used = False if (number%2)== 0: print('even',end=' ') used = True if (number%3)== 0: print('three-ish',end='') used = True if used: print() else: print('useless') >>> testing_if2([1,2,3,4,5,6,100,47,59]) 1: useless 2: even 3: three-ish 4: even 5: useless 6: even three-ish 100: even 47: useless 59: useless >>> for iteration in range(5): print('hello') hello hello hello hello hello >>> for number in range(5): output = output+number >>> number 4 >>> for number in range(5): output = output+number >>> number 4 >>> output 77 >>> output 77 >>> for number in range(5): output = output+number >>> output 87 >>> output = '' >>> for character in 'hello world': output = output+chr(ord(character)+50) >>> output '\x9a\x97\x9e\x9e¡R©¡¤\x9e\x96' >>> colors {'chicken': 'yellow', 'apple': 'red'} >>> for colored_item in colors: print('The',colored_item, 'is',colors[colored_item]) The chicken is yellow The apple is red >>> len('hello') 5 >>> len(range(5)) 5 >>> len(range(1,5)) 4 >>> len(colors) 2 >>> while(False): print('This is a secret') >>> import time >>> while(True): print('This is no longer a secret') time.sleep(1) This is no longer a secret This is no longer a secret This is no longer a secret This is no longer a secret This is no longer a secret This is no longer a secret This is no longer a secret Traceback (most recent call last): File "", line 3, in time.sleep(1) KeyboardInterrupt >>> def increment_and_find_a_3(): import random number = 1 while(number%3!=0): print(number) number = number+round(random.random()*100) >>> increment_and_find_a_3() 1 >>> def increment_and_find_a_3(): import random number = 1 while((number%3)!=0): print(number) number = number+round(random.random()*100) >>> increment_and_find_a_3() 1 28 58 155 158 >>> for num1 in range(1,6): for num2 in range(1,10): print(num1,num2) 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 >>> print >>> print(print) >>> print(5) 5 >>> print('hello') hello >>> self_containing_list = [1,2,3] >>> self_containing_list.append(self_containing_list) >>> print(self_containing_list) [1, 2, 3, [...]] >>> print('This string contains a newline \n and a weird character \u134') SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 56-60: end of string in escape sequence >>> print('This string contains a newline \n and a weird character \u184') SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 56-60: end of string in escape sequence >>> print('This string contains a newline \n and a weird character \u104') SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 56-60: end of string in escape sequence >>> print('This string contains a newline \n and a weird character \u094') SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 56-60: end of string in escape sequence >>> print('This string contains a newline \n and a weird character \'') This string contains a newline and a weird character ' >>> \u145 SyntaxError: unexpected character after line continuation character >>> '\u145' SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-4: end of string in escape sequence >>> print('hello','world') hello world >>> print('hello','world',sep='**') hello**world >>> print('hello','world',sep='**',end='%%') hello**world%% >>> import random >>> random.randint(1,5) 2 >>> for iteration in range(20): random.randint(1,5) 5 3 5 5 2 5 3 4 1 3 3 2 5 1 2 3 2 3 3 1 >>> output = input('What?') What?That's what >>> output "That's what" >>> multiplier = 1 >>> while (multiplier < 20): print(multiplier) multiplier = multiplier * 3 1 3 9 >>> multiplier 27 >>> def sails(width,columns, rows): for row in range(rows): for number in range(1,width+1): for column in range(columns): print('~'*number+' '*(width-number),end='') print() >>> sails(5,3,3) ~ ~ ~ ~~ ~~ ~~ ~~~ ~~~ ~~~ ~~~~ ~~~~ ~~~~ ~~~~~~~~~~~~~~~ ~ ~ ~ ~~ ~~ ~~ ~~~ ~~~ ~~~ ~~~~ ~~~~ ~~~~ ~~~~~~~~~~~~~~~ ~ ~ ~ ~~ ~~ ~~ ~~~ ~~~ ~~~ ~~~~ ~~~~ ~~~~ ~~~~~~~~~~~~~~~ >>>