2020a = 42
2121b = a * 2 - 3
2222
23- # In addition to numbers, Python can also represent values that are text,
24- # called strings. They are given between either single or double quotes.
25- # You can take your pick which type of quote you prefer. Using one type
26- # allows the use of other type inside the string.
23+ # Python can also represent and handle text strings. They are given
24+ # between either single or double quotes. You can take your pick
25+ # which type of quote you prefer. Using one kind of quote allows
26+ # the use of other type inside the string.
2727
2828c = 'Hello world'
2929d = "Another 'string' given between double quotes"
8484print (math .pow (x , math .pi ))
8585
8686# Python even supports complex numbers right out of the box, and its
87- # arithmetic operations just do the correct complex number arithmetic.
87+ # arithmetic operations just do the complex number arithmetic.
8888
8989z1 = complex (4 , - 2 ) # 4 - 2j
9090z2 = complex (- 3 , 1 ) # -3 + j
9191z3 = z1 * z2 # -10+10j
9292print (f"The real part is { z3 .real } and the imaginary part is { z3 .imag } ." )
9393
94+ # However, Python uses floating point arithmetic for complex numbers.
9495
9596f1 = Fraction (- 2 , 7 ) # a fraction from two integers
9697f2 = Fraction ('5/9' ) # a fraction from a string
100101# Joe and Moe are peeling potatoes. Working by himself, Joe could peel
101102# the entire pile in three hours, whereas Moe could peel the same pile
102103# in five hours. How long will it take for these two men to peel the
103- # potatoes if they work together? (No, the answer is not four hours,
104- # the average of three and five.)
104+ # potatoes if they work together? (No, the answer is * not* four hours,
105+ # the simple average of three and five.)
105106
106- joe = Fraction (1 , 3 )
107- moe = Fraction (1 , 5 )
108- together = joe + moe
107+ joe_speed = Fraction (1 , 3 )
108+ moe_speed = Fraction (1 , 5 )
109+ together = joe_speed + moe_speed
109110time = 1 / together
110111print (f"Together, Joe and Moe finish in { time } hours." )
111112
112- # Remember that strings and integers are not the same thing, even as they
113- # can be trivially converted to one another.
113+ # Remember that strings and integers are not the same thing, even as
114+ # they can be trivially converted to one another.
114115
115116a = 22 + 22
116117print (a ) # 44
136137print (f"After swap, x equals { x } , and y equals { y } ." ) # 42 17
137138
138139# In basic arithmetic, division is handled with a couple of different
139- # operators depending on what kind of division you want. Couple of things
140- # about it can first be a bit surprising.
140+ # operators depending on what kind of division you want. Couple of
141+ # things about them can first be surprising.
141142
142143print (11 / 4 ) # 2.75, the usual everyday division
143144print (11 / - 4 ) # -2.75
155156idontknow = None
156157print (idontknow )
157158del idontknow
158- print (idontknow ) # crash with NameError
159+ print (idontknow ) # crash with NameError
0 commit comments