# CSH 2.4 MrG 2012.0928 Coding

## 3149 days ago by CSH2012

#1) def first(s): '''give you the first element of a list or the first char of a string''' return s #first char first('Hello, World!!!')
 \newcommand{\Bold}{\mathbf{#1}}\hbox{H} \newcommand{\Bold}{\mathbf{#1}}\hbox{H}
first.__doc__
 \newcommand{\Bold}{\mathbf{#1}}\hbox{give you the first element of a list or the first char of a string} \newcommand{\Bold}{\mathbf{#1}}\hbox{give you the first element of a list or the first char of a string}
#2) def, while, return def sum1ToN(n): "returns 1+2+...+n" s=0 k=1 while k<=n: s=s+k # add k to s k=k+1 # increment k by 1 return s sum1ToN(100)
 \newcommand{\Bold}{\mathbf{#1}}5050 \newcommand{\Bold}{\mathbf{#1}}5050
 \newcommand{\Bold}{\mathbf{#1}}8 \newcommand{\Bold}{\mathbf{#1}}8
#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(2)
 \newcommand{\Bold}{\mathbf{#1}}8 \newcommand{\Bold}{\mathbf{#1}}8
#7) def firstLast(s): 'return new string with 1st and last letters only' return s+s[-1] firstLast('A. Jorge Garcia-Fernandez')
 \newcommand{\Bold}{\mathbf{#1}}\hbox{Az} \newcommand{\Bold}{\mathbf{#1}}\hbox{Az}
#8) def triangle(s): print 5*s+"\n "+3*s+"\n "+s triangle('*')
 ***** *** * ***** *** *
show(triangle('a'))
 aaaaa aaa a \newcommand{\Bold}{\mathbf{#1}}\hbox{None} aaaaa aaa a \newcommand{\Bold}{\mathbf{#1}}\hbox{None}
show(3==3)
 \newcommand{\Bold}{\mathbf{#1}}{\rm True} \newcommand{\Bold}{\mathbf{#1}}{\rm True}
show(3==2)
 \newcommand{\Bold}{\mathbf{#1}}{\rm False} \newcommand{\Bold}{\mathbf{#1}}{\rm False}