Python Programming: An Introduction to Computer Science
3.5.HANDLINGLARGENUMBERS:LONGINTS 33 Please enter a wholenumber. 15 The factorial of 15 is 1.307674368e+12 We donotgetanoverflow ...
34 CHAPTER3. COMPUTINGWITHNUMBERS Ifyouhave anolderversionofPython(priortoversion2.0),theseanswerswillbeprintedwithan“L” appende ...
3.7.EXERCISES 35 int(4.5) 4 int(3.9) 3 long(3.9) 3L float(int(3.3)) 3.0 int(float(3.3)) 3 int(float(3)) 3 Asyoucansee,conv ...
36 CHAPTER3. COMPUTINGWITHNUMBERS Show thelistofnumbersthatwouldbegeneratedbyeachofthefollowingrangeexpressions. (a)range(5) ( ...
3.7.EXERCISES 37 TheGregorianEpactis thenumberofdaysbetweenJan.1standtheprevious1stquartermoonphase. Thisvalueis usedtofigureou ...
38 CHAPTER3. COMPUTINGWITHNUMBERS ...
Chapter 4 Computing with Strings Sofar, wehave beendiscussingprogramsdesignedtomanipulatenumbers.Thesedaysweknow thatcom- puters ...
40 CHAPTER4. COMPUTINGWITHSTRINGS firstName = input("Pleaseenter your name: ") Please enter your name:"John" print "Hello",fi ...
4.2.SIMPLESTRINGPROCESSING 41 greet[0:3] ’Hel’ greet[5:9] ’ Bob’ greet[:5] ’Hello’ greet[5:] ’ Bob’ greet[:] ’Hello Bob’ T ...
42 CHAPTER4. COMPUTINGWITHSTRINGS tosevenlettersoftheuser’s lastname. Usingthismethod,theusernameforElmerThudpuckerwouldbe “ethu ...
4.3.STRINGSANDSECRETCODES 43 month number position Jan 1 0 Feb 2 3 Mar 3 6 Apr 4 9 Ofcourse,thepositionsallturnouttobemultipleso ...
44 CHAPTER4. COMPUTINGWITHSTRINGS imagination,youmightusethenumbers1-26torepresentthelettersa–z.Insteadoftheword“sourpuss,” you ...
4.3.STRINGSANDSECRETCODES 45 Finally, weneedto converteachcharacterto a number. Thesimplestapproachis to usetheASCIInumber (prov ...
46 CHAPTER4. COMPUTINGWITHSTRINGS add the characterto the end of message print the message Beforetheloop,theaccumulatorvariablem ...
4.3.STRINGSANDSECRETCODES 47 eval("345.67") 345.67 eval("3+4") 7 x = 3.5 y = 4.7 eval("x * y") 16.45 x = eval(raw_input("Ente ...
48 CHAPTER4. COMPUTINGWITHSTRINGS import numbers2text This program convertsa sequence of ASCII numbers into the string of tex ...
4.4.OUTPUTASSTRINGMANIPULATION 49 Evenif ourcipherwerenotbasedonthewell-knownASCIIencoding,it wouldstillbeeasytodiscover theorig ...
50 CHAPTER4. COMPUTINGWITHSTRINGS Function Meaning float(<expr>) Convertexprtoa floatingpointvalue. int(<expr>) Conv ...
4.4.OUTPUTASSTRINGMANIPULATION 51 Percentsignsinsidethetemplate-stringmark“slots”intowhichthevaluesareinserted.Theremust beexact ...
52 CHAPTER4. COMPUTINGWITHSTRINGS Supposeyouarewritinga computersystemfora bank.Yourcustomerswouldnotbetoohappy tolearn thata ch ...
«
1
2
3
4
5
6
7
8
9
10
»
Free download pdf