2010.1018 ch2.4 Python Code

4056 days ago by calcpage123

#1) def first(s): 'return first character in a given string s' return s[0] first("Hello, world") 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{H}
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{H}
first.__doc__ 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{return first character in a given string s}
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{return first character in a given string s}
#2) def, while, return 
       
#3) def badCode(x): return x**2-1 badCode(-10) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}99
\newcommand{\Bold}[1]{\mathbf{#1}}99
#4) print('One is better that \none' +\ '; two is better than one') 
       
One is better that 
one; two is better than one
One is better that 
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(2) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}8
\newcommand{\Bold}[1]{\mathbf{#1}}8
#7) def firstAndLast(s): 'output new string with first and last characters of s' return s[0]+s[-1] firstAndLast("Baldwin") 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{Bn}
\newcommand{\Bold}[1]{\mathbf{#1}}\hbox{Bn}
#8) def triangle(s): 'print out triangle made up of s' print(s+s+s+s+s+'\n '+s+s+s+'\n '+s) triangle("b") 
       
bbbbb
 bbb
  b
bbbbb
 bbb
  b