Concepts of Computing
Concepts of Computing ECS 010
Popular in Course
Popular in Engineering Computer Science
This 2 page Class Notes was uploaded by Ashleigh Dare on Tuesday September 8, 2015. The Class Notes belongs to ECS 010 at University of California - Davis taught by Staff in Fall. Since its upload, it has received 13 views. For similar materials see /class/191718/ecs-010-university-of-california-davis in Engineering Computer Science at University of California - Davis.
Reviews for Concepts of Computing
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/08/15
ECS 10 Basic Concepts of Computing Spring Quarter 2009 Printing Python offers several ways to print information This handout presents two using the print statement Unform atted Printing Unformatted printing prints numbers and strings in a natural format integers as ordinary numbers oating point numbers with two decimal digits and strings as they are typed Thus num l flnum 26 print quotThe integer isquot num quotand the float isquot flnum prints The integer is l and the float is 26 A comma between two arguments prints a space a comma at the end of the line suppresses the skip to the next line print quotHere is one print statementquot print quotAnd here is anotherquot prints Here is one print statement And here is another One problem with unformatted printing is that you cannot avoid adding spaces before numbers So if you try to print 139 with this print quotquot 139 you get 1 39 notice the unwanted space To print this you need a formatted print statement Formatted Print A formatted print statement allows you to control how the number or string is printed You can do lots of things with it that you cannot do with an unformatted print statement Let s start with the last unformatted print problem We want to print 139 Here s how we do it print quotfquot 139 This prints 139000O The string immediately following the printf is the format string It is printed as typed except when a is seen The sequence of characters following it control how the next arguments are to be printed f means to print a oating point number d would mean to print an integer Version of Aprl 8 2009 at 1021 AM Page 1 of2 ECS 10 Basic Concepts of Computing Spring Quarter 2009 as a decimal number d stands for decimal The after the string says that the arguments for the printing follow The arguments are enclosed in parentheses1 One problem with the aboveitoo many decimal places Let s restrict it to 2 decimal places by saying instead print quot2fquot 139 which prints 139 The f says to print the number as a oating point number The 2 between the and f means to print 2 digits after the decimal point As another example if you want to print the value OfJ39IS to 7 decimal places say import math print quotThe value of pi is 7fquot mathpi You get The value of pi is 31415927 Now let s say we want to print several arguments You do it this way print quotd d dquot 2 2 2 2 gives 2 2 4 You can get very fancy print 391anguages has O3d quote types quotquot 2 391anguage39 quotPythonquot gives Python has 002 quote types What Is Actually Going On Actually the arguments to print are simply a way to write a string Ignore the word print Then the string in quotes is called the format string and the parenthesized list following the are the values The values are substituted into the format string resulting in a new string To see this type the following to the IDLE interpreter x quot2 f fquot mathpi 2mathpi print x and you will see 2 3141593 6283185 1 Actually if there is only one argument to be printed you can omit the parentheses But it s easier to remember them if you always put them in Version of Aprl 8 2009 at 1021 AM Page 2 of2
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'