# 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 first("Hello, world")
 \newcommand{\Bold}{\mathbf{#1}}\hbox{H} \newcommand{\Bold}{\mathbf{#1}}\hbox{H}
first.__doc__
 \newcommand{\Bold}{\mathbf{#1}}\hbox{return first character in a given string s} \newcommand{\Bold}{\mathbf{#1}}\hbox{return first character in a given string s}
#2) def, while, return
 \newcommand{\Bold}{\mathbf{#1}}99 \newcommand{\Bold}{\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}{\mathbf{#1}}8 \newcommand{\Bold}{\mathbf{#1}}8
#7) def firstAndLast(s): 'output new string with first and last characters of s' return s+s[-1] firstAndLast("Baldwin")
 \newcommand{\Bold}{\mathbf{#1}}\hbox{Bn} \newcommand{\Bold}{\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