CM 2.4 Code MrG 2011.1012

3700 days ago by calcpage123

#1) def first(word): 'gives first char of a string or first element of list' return word[0] first('garcia') 
       
'g'
'g'
first(range(2,5)) 
       
2
2
first.__doc__ 
       
'gives first char of a string or first element of list'
'gives first char of a string or first element of list'
#2) def, while, return 
       
#3) def badCode(x): return x**2-1 badCode(2) 
       
3
3
#4) print('one is better than \none' +\ '; two is better than one') 
       
one is better than 
one; two is better than one
one is better than 
one; two is better than one
#5) print('Python is #1') 
       
Python is #1
Python is #1
#6) def mystery(n): 'Returns n cubed' return n**3 mystery(3) 
       
27
27
#7) def firstLast(word): 'returns first and last letter of a string' return str(word[0])+str(word[-1]) firstLast("school") 
       
'sl'
'sl'
firstLast(range(12,30)) 
       
'1229'
'1229'
#8) def triangle(s): 'do half ascii graphics' print(s*5 + '\n ' + s*3 + '\n ' + s ) triangle('x') 
       
xxxxx
 xxx
  x
xxxxx
 xxx
  x