Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 2 - Elementary Programming - Programming Exercises - Page 58: 2.12

Answer

Program: #initialize a and $\mathrm{b}$ $\mathrm{a}=1$ $b=2$ #print table header and first row $\operatorname{print}\left(\right.$ "a $\left.\mathrm{b} \mathrm{a}^{* *} \mathrm{~b}^{\prime \prime}\right)$ $\operatorname{print}\left(a, "^{\prime \prime}, b, " ", a^{* x} b\right)$ #increment a and $\underline{b}$ $a+=1$ $b+=1$ #print table contents $\operatorname{print}\left(a, "^{\prime \prime}, b_{n}^{\prime \prime \prime}, a^{* \star} b\right)$ #repeat three more times $a+=1$ $b+=1$ $\operatorname{print}\left(a, " ", b, " ", a^{* \star} b\right)$ $a+=1$ $b+=1$ $\operatorname{print}\left(a, "^{\prime \prime}, b, " ", a^{* \star} b\right)$ $a+=1$ $b+=1$ $\operatorname{print}\left(a, "^{\prime \prime}, b_{n}^{\prime \prime \prime}, a^{* *} b\right)$

Work Step by Step

Step 1 of 3: Program Plan: Create a python file with name 2_12PE.PY. In the file, - Initialize variable $a$ and $b$ to 1 and 2 respectively. - Display table header and first row. - Increment variable a and $b$ by 1 - Display second row of table. - Repeat the above 2 steps 3 more times. - Note that this code would have been shorter with for or if loops, but they have not been introduced till chapter 2 , so done in this crude way. Step 2 of 3: Program: #initialize a and $\mathrm{b}$ $\mathrm{a}=1$ $b=2$ #print table header and first row $\operatorname{print}\left(\right.$ "a $\left.\mathrm{b} \mathrm{a}^{* *} \mathrm{~b}^{\prime \prime}\right)$ $\operatorname{print}\left(a, "^{\prime \prime}, b, " ", a^{* x} b\right)$ #increment a and $\underline{b}$ $a+=1$ $b+=1$ #print table contents $\operatorname{print}\left(a, "^{\prime \prime}, b_{n}^{\prime \prime \prime}, a^{* \star} b\right)$ #repeat three more times $a+=1$ $b+=1$ $\operatorname{print}\left(a, " ", b, " ", a^{* \star} b\right)$ $a+=1$ $b+=1$ $\operatorname{print}\left(a, "^{\prime \prime}, b, " ", a^{* \star} b\right)$ $a+=1$ $b+=1$ $\operatorname{print}\left(a, "^{\prime \prime}, b_{n}^{\prime \prime \prime}, a^{* *} b\right)$ Step 3 of 3: Sample Output: $a b a^{* *} b$ 121 238 3481 451024 5615625
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.