9.11 first 9 exercises, 9.119 exercises
1. print
(1) print the first sentence ....
1 print " Hello World! "
Note the use of "" And''
1 print "I'm paul" # For 2 3 print 'I am paul' # the error is changed to 'I \'m paul'
And the use of "And '''
1 print """2 3 Hello World!4 5 Hello World!6 7 Hello World!8 9 """
(2) comma
print "hello world ",'abc'print "i am paul "
Result:
Hello world abc
I am paul
print "hello world " 'abc'print "i am paul "
Result:
Hello world abc
I am paul
print "hello world" ' abc',print "i am paul"
Result:
Hello world abci am paul
print "hello world" ' abc',"i am paul"
Syntax Error
(3) operator number
print "now i will count the eggs:"print 3+1+2-5+4%2-1/4print 3+2>4-2
Result:
Now I will count the eggs:
1
True
(4) variables and Printing
name = "paul"age = 20height = 70print "let's talk about %s"%nameprint "he's %d years old"%ageprint "he's %d inches tall"%heightprint "let's talk about %r"%nameprint "he's %r years old"%ageprint "he's %r inches tall"%height
print "if i add %d and %d,i get %d."%(age,height,age+height)
Let's talk about paul
He's 20 years old
He's 70 inches tall
Let's talk about "paul"
He's 20 years old
He's 70 inches tall
If I add 20 and 70, I get 90
x = "there are %d types of people."%10print x print "i said :%r "%xy = "isn't that joke so funny? %r"z = Falseprint y%za = "this is the left of"b = "a string with a right side"print a + b
Result:
There are 10 types of people.
I said: there are 10 types of people.
Isn' t that joke so funny? False
This is the left of a string with a right side
print "ab " *10
Result:
AB
formatter = "%r %r %r"print formatter %(1,2,3)print formatter %("one","two","three")print formatter %(True,False,True)print formatter %( "i had this string", "that you could ", "type up right" )
1 2 3
'One' two' 'three'
True False True
'I had this string ''that you could ''type up right'
1 a = "a\nb\nc\n"2 print a
Result:
A
B
C