print without Going to a New Lineprint Item Separatorformat Functionformat with Scientific Notationformat to Put Commas in Big Numbersformat FunctionYou should read Chapter 3, Decision Structures and Boolean Logic, from our textbook, Starting Out with Python, before next weeks's class.
I have posted homework 3 here.
It is due this coming Sunday at 11:59 PM.
Let's take a look at it.
int conversion functionint converts a decimal to an integer by throwing away the decimal part>>> int(5.3) 5
>>> int(5.9) 5
round
>>> round(5.3) 5 >>> round(5.9) 6
| Character | Operation | Description |
|---|---|---|
| + | Addition | Adds two numbers |
| − | Subtraction | Subtracts one number from another |
| * | Multiplication | Multiplies one number by another |
| / | Division | Divides one number by another and gives the result as a decimal number |
| // | Integer division | Divides one number by another and gives the result as an integer |
| % | Remainder | Divides one number by another and gives the remainder |
| ** | Exponent | Raises a number to a power |
>>> a = 2 >>> b = 4 >>> a + b 6 >>> a - b -2 >>> a * b 8 >>> a / b 0.5
>>> 4 / 2 2.0 >>> 4 / 5 0.8
>>> 4 // 2 2 >>> 5 // 2 2
>>> -4 // 2 -2 >>> -5 // 2 -3
>>> 3 ** 2 9 >>> 3 ** 3 27 >>> 3 ** 4 81
>>> 17 // 5 3
>>> 17 % 5 2
| ** | Exponentiation |
| * / // % | Multiplication, division and remainder |
| + - | Addition and subtraction |
2 + 3 * 5
>>> 2 + 3 * 5 17 >>> (2 + 3) * 5 25
>>> 3 * 5 15
>>> 3.0 * 5.0 15.0
>>> 3 * 5.0 15.0
int values, the result will be an intfloat values, the result will be a floatint and a float, float
>>> print("To be or not to be. \
... That is the question.")
To be or not to be. That is the question.
number = int(input("Please enter an integer: "))
rate * balance
EXPRESSSION OPERATOR EXPRESSION
5 + round(value)
value_1 + round(value_2)
round(value_1) + round(value_2)
print("Hello")
prints 5 characters
print("Hello world")
prints 11 characters, one of them a Spaceprint statement?print statement?
>>> print("Line 1
File "<stdin>", line 1
print("Line 1
^
SyntaxError: EOL while scanning string literal)
\n
print("Line 1\nLine 2\nLine 3")
Line 1
Line 2
Line 3
| Escape Character | Effect |
|---|---|
| \n | Causes output to be advanced to the next line |
| \t | Causes output to skip over to the next horizontal tab position |
| \' | Causes a single quote mark to be printed |
| \" | Causes a double quote mark to be printed |
| \\ | Causes a backslash character to be printed |
print('The path is C:\\temp\\data.')
The path is C:\temp\data.
>>> print("Monday\tTuesday\tWednesday\tThursday\tFriday")
Monday Tuesday Wednesday Thursday Friday
$ cat print_06.py
# uses tabs to align values
print("percent\t", 10)
print("rate\t", 5)
$ python3 print_06.py
percent 10
rate 5
>>> print("He said \"It\'s me!\"")
He said "It's me!"
print without Going to a New Lineprint function normally prints its output on one line and moves down to the next
$ cat print_04.py
# Prints three lines
print("Line 1")
print("Line 2")
print("Line 3")
$ python3 print_04.py
Line 1
Line 2
Line 3
print function normally adds the newline character to the string you give it as an argumentprint not to add this newline character at the endprint a special kind of argument5 a a * 6
print to supply another character instead of newline
you write something like this
end=" "
$ cat print_02.py
# Prints a single line
print("Line 1", end=" ")
print("Line 2", end=" ")
print("Line 3")
we get
$ python3 print_02.py Line 1 Line 2 Line 3
print to add nothing at the end
$ cat print_03.py
# Prints a single line
print("Line 1", end="")
print("Line 2", end="")
print("Line 3")
$ python3 print_03.py
Line 1Line 2Line 3
$ cat print_04.py
# Prints a single line
print("Line 1", end="---")
print("Line 2", end="---")
print("Line 3")
$ python3 print_04.py
Line 1---Line 2---Line 3
print Item Separatorprint more that one argument to print it will add a Space between each value
>>> print(1, 2, 3, 4, 5) 1 2 3 4 5
>>> print(1, 2, 3, 4, 5, sep=",") 1,2,3,4,5
>>> print(1, 2, 3, sep=", ") 1, 2, 3
print statement
$ cat print_05.py # Prints a single line print(1, 2, sep=", ", end="---") print(3, 4, sep=", ", end="---") print(5) $ python3 print_05.py 1, 2---3, 4---5
>>> team = "Red Sox"
>>> print("Let's go", team)
Let's go Red Sox
>>> cheer = "Let's go " + team
>>> print(cheer)
Let's go Red Sox
print statement are a string literal and a
variable
print statement has one argumentprint function a number it has to convert it into a string in order to print it
>>> value = 5
>>> print("The value is", value)
The value is 5
print("The value is" + value)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
>>> balance = "1000"
>>> print("The balance is " + balance)
The balance is 1000
because balance is a string
>>> balance = 1000
>>> print("The balance is " + balance)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
str
print("The balance is " + str(balance))
The balance is 1000
>>> print(1/3) 0.3333333333333333
float value can have>>> print(bill, "split three ways is", bill/3) 54.35 split three ways is 18.116666666666667
round function
>>> print(bill, "split three ways is", round(bill/3, 2)) 54.35 split three ways is 18.12
>>> print("The age of the Universe is 13800000000 years")
The age of the Universe is 13800000000 years
>>> print("The size of an electron is .0000000000000282 meters")
The size of an electron is .0000000000000282 meters
printf
format Functionprintf stands for "print formated"format function format function takes two arguments
format function we are using it with a float valuefloat is fformat on a float to specify the number of digits after the decimal pointfloat is .2f
>>> format(12345.67890, ".2f")
'12345.68'
>>> format(12345.67890, ".3f")
'12345.679'
format function automatically rounds the last digitround functionformat with Scientific Notationformat function to print a number in scientific notation
>>> print("The age of the universe is" , format(13800000000, "e"), "years")
The age of the universe is 1.380000e+10 years
>>> print("The size of an electron is", format(.0000000000000282, "e"), "meters")
The size of an electron is 2.820000e-14 meters
>>> print("The age of the universe is" , format(13800000000, ".2e"), "years")
The age of the universe is 1.38e+10 years
>>> print("The size of an electron is", format(.0000000000000282, ".2e"), "meters")
The size of an electron is 2.82e-14 meters
format to Put Commas in Big Numbersformat to group numbers with commas we add a comma before the period in the format specifier
format(12345.67890, ",.2f")
'12,345.68'
>>> format(12345.67890, ",f")
'12,345.678900
print("The age of the Universe is", format(13800000000, ",d"), "years")
The age of the Universe is 13,700,000,000 years
$ cat print_07.py
# uses tabs to print a table
print("Mon\tTue\tWed\tThr\tFri")
print("15\t24\t89\t31\t86")
print("21\t79\t74\t23\t79")
[528] glenn - ~/workspace-neon/it116/resources_it116/code_it116/examples_it116/2_chapter_examples
$ python3 print_07.py
Mon Tue Wed Thr Fri
15 24 89 31 86
21 79 74 23 79
$ cat print_08.py
# uses tabs to print a table
print("Mon\tTue\tWed\tThr\tFri")
print("13452.12\t24\t234523.4589\t31\t86")
print("2324.1\t79\t74\t22343.453\t79")
$ python3 print_08.py
Mon Tue Wed Thr Fri
13452.12 24 234523.4589 31 86
2324.1 79 74 22343.453 79
$ cat print_09.py # prints a table of values using format print(format(13452.12, "10.2f") + format(24, "10.2f") + format(234523.4589, "10.2f") \ + format(31, "10.2f") + format(86, "10.2f")) print(format(2324.1, "10.2f") + format(79, "10.2f") + format(74, "10.2f") \ + format(22343.453, "10.2f") + format(79, "10.2f")) $ python3 print_09.py 13452.12 24.00 234523.46 31.00 86.00 2324.10 79.00 74.00 22343.45 79.00
format Functionformat function to set the minimum width of strings as well as numbersformat with strings we use the type specifier s
$ cat print_10.py
# prints a table of values using format
print(format("Mon", "10s") + format("Tue", "10s") + format("Wed", "10s") \
+ format("Thr", "10s") + format("Fri", "10s"))
print(format(13452.12, "10.2f") + format(24, "10.2f") + format(234523.4589, "10.2f") \
+ format(31, "10.2f") + format(86, "10.2f"))
print(format(2324.1, "10.2f") + format(79, "10.2f") + format(74, "10.2f") \
+ format(22343.453, "10.2f") + format(79, "10.2f"))
$ python3 print_10.py
Mon Tue Wed Thr Fri
13452.12 24.00 234523.46 31.00 86.00
2324.10 79.00 74.00 22343.45 79.00
$ cat print_11.py
# prints a table of values using format
print(format("Mon", ">10s") + format("Tue", ">10s") + format("Wed", ">10s") \
+ format("Thr", ">10s") + format("Fri", ">10s"))
print(format(13452.12, "10.2f") + format(24, "10.2f") + format(234523.4589, "10.2f") \
+ format(31, "10.2f") + format(86, "10.2f"))
print(format(2324.1, "10.2f") + format(79, "10.2f") + format(74, "10.2f") \
+ format(22343.453, "10.2f") + format(79, "10.2f"))
$ python3 print_11.py
Mon Tue Wed Thr Fri
13452.12 24.00 234523.46 31.00 86.00
2324.10 79.00 74.00 22343.45 79.00
format function can turn decimals into percentage
>>> print("3 out of 10 is", format(3/10, ".0%"))
3 out of 10 is 30%
format FunctionI will not ask you to use format on a quiz or exam.
I do not expect you to memorize all the different components of a format string.
These are the sorts of things you look up when you need them.