Multiple questions

Assignment

 Question 1

Consider the following Python script.

str = “John”

str1 = “Good morning,” + str

str2 = str1 + “How are you?”

print(str2)

  1. a) What is the output?
  2. b) Write a Python statement using str, str1 or str2 only to make a list [‘Good’, ‘morning,John’]?
For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Content Editor

Use arrow keys to select functions

Bold Italic Underline Font family — Font family — Font size — Font size —
Text Color

 

Bullet List Number List Toggle Spell Checker Insert/Edit Link Remove Link
Bold Italic Underline Strikethrough — Format — Heading Sub Heading 1 Sub Heading 2 Paragraph Formatted Code Format Font family — Font family — Andale Mono Arial Arial Black Book Antiqua Comic Sans MS Courier New Georgia Helvetica Impact Symbol Tahoma Terminal Times New Roman Trebuchet MS Verdana Webdings Wingdings Font size — Font size — 1 (8pt) 2 (10pt) 3 (12pt) 4 (14pt) 5 (18pt) 6 (24pt) 7 (36pt)
Bullet List
Number List
Text Color

 

Highlight

 

Remove Formatting

 

Cut Copy Paste Find Undo Redo Align Left Align Center Align Right Align Full Indent Outdent Superscript Subscript Insert/Edit Link Remove Link Direction Left to Right Direction Right to Left Line Horizontal Rule Insert Nonbreaking Space Character
Toggle Spell Checker

 

Browse My YouTube Videos Launch Math Editor
Insert Mashup
Show Nonprinting Characters Blockquote Symbol Emotions Anchor Insert/Edit Table Table Row Properties Table Cell Properties Insert Row Before Insert Row After Delete Row Insert Column Before Insert Column After Delete Column Merge Table Cells Split Merged Table Cells HTML Code View Edit CSS Style

 

Preview Help Full Screen Show Less
Path:

Words:0

Question 2

 Consider the following prompt statement to ask salary:

salary = input(“Enter your salary in Integer”)

Assume that the salary will be increased by 1000.

  1. a) Write a print statement to display the outcome of the increment. For example, if you enter 5000, then your program returns 6000. Give one line print statement.
  1. b) Assume that a salary can be a real number. For example, 5019.99. Modify your print statement that can handle both integer and real numbers.
For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Content Editor

Use arrow keys to select functions

Bold Italic Underline Font family — Font family — Font size — Font size —
Text Color

 

Bullet List Number List Toggle Spell Checker Insert/Edit Link Remove Link
Bold Italic Underline Strikethrough — Format — Heading Sub Heading 1 Sub Heading 2 Paragraph Formatted Code Format Font family — Font family — Andale Mono Arial Arial Black Book Antiqua Comic Sans MS Courier New Georgia Helvetica Impact Symbol Tahoma Terminal Times New Roman Trebuchet MS Verdana Webdings Wingdings Font size — Font size — 1 (8pt) 2 (10pt) 3 (12pt) 4 (14pt) 5 (18pt) 6 (24pt) 7 (36pt)
Bullet List
Number List
Text Color

 

Highlight

 

Remove Formatting

 

Cut Copy Paste Find Undo Redo Align Left Align Center Align Right Align Full Indent Outdent Superscript Subscript Insert/Edit Link Remove Link Direction Left to Right Direction Right to Left Line Horizontal Rule Insert Nonbreaking Space Character
Toggle Spell Checker

 

Browse My YouTube Videos Launch Math Editor
Insert Mashup
Show Nonprinting Characters Blockquote Symbol Emotions Anchor Insert/Edit Table Table Row Properties Table Cell Properties Insert Row Before Insert Row After Delete Row Insert Column Before Insert Column After Delete Column Merge Table Cells Split Merged Table Cells HTML Code View Edit CSS Style

 

Preview Help Full Screen Show Less
Path:

Words:0

Question 3 

Consider the following code segment:

principal = input(“Enter your principal”)

month = input(“Enter the number of installment”)

installment = principal/month

  1. a) If the above Python segment runs, what is the result. Otherwise, explain the error (not copying) and fix it.
  1. b) In addition to the above Python segment, write one line of a print statement using the format function. An example of the output is

If you pay 217.39 for 23 times, then you can pay off the principle 5000

For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Content Editor

Use arrow keys to select functions

Bold Italic Underline Font family — Font family — Font size — Font size —
Text Color

 

Bullet List Number List Toggle Spell Checker Insert/Edit Link Remove Link
Bold Italic Underline Strikethrough — Format — Heading Sub Heading 1 Sub Heading 2 Paragraph Formatted Code Format Font family — Font family — Andale Mono Arial Arial Black Book Antiqua Comic Sans MS Courier New Georgia Helvetica Impact Symbol Tahoma Terminal Times New Roman Trebuchet MS Verdana Webdings Wingdings Font size — Font size — 1 (8pt) 2 (10pt) 3 (12pt) 4 (14pt) 5 (18pt) 6 (24pt) 7 (36pt)
Bullet List
Number List
Text Color

 

Highlight

 

Remove Formatting

 

Cut Copy Paste Find Undo Redo Align Left Align Center Align Right Align Full Indent Outdent Superscript Subscript Insert/Edit Link Remove Link Direction Left to Right Direction Right to Left Line Horizontal Rule Insert Nonbreaking Space Character
Toggle Spell Checker

 

Browse My YouTube Videos Launch Math Editor
Insert Mashup
Show Nonprinting Characters Blockquote Symbol Emotions Anchor Insert/Edit Table Table Row Properties Table Cell Properties Insert Row Before Insert Row After Delete Row Insert Column Before Insert Column After Delete Column Merge Table Cells Split Merged Table Cells HTML Code View Edit CSS Style

 

Preview Help Full Screen Show Less
Path:

Words:0

Question 4

Consider the output of a Python GUI program. The questions below are asking for you to write a Python statement. Assume that the object of Tk() is top and the object of Canvas() is cc.

(a) Write a Python sentence to label “Welcome to John Yoon’s Home” on the window’s title.

(b) On the canvas, draw a rectangle of width 100 and height 70 from the upper left corner (10, 10). Fill the rectangle with black.

(c) On the rectangle, write a text “Mercy Cybersecurity” to be centerized from the point (50,20)

(d) On the canvas again, draw a circle of diameter 50 from the upper left corner (30,30). Fill the rectangle with blue.

For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Content Editor

Use arrow keys to select functions

Bold Italic Underline Font family — Font family — Font size — Font size —
Text Color

 

Bullet List Number List Toggle Spell Checker Insert/Edit Link Remove Link
Bold Italic Underline Strikethrough — Format — Heading Sub Heading 1 Sub Heading 2 Paragraph Formatted Code Format Font family — Font family — Andale Mono Arial Arial Black Book Antiqua Comic Sans MS Courier New Georgia Helvetica Impact Symbol Tahoma Terminal Times New Roman Trebuchet MS Verdana Webdings Wingdings Font size — Font size — 1 (8pt) 2 (10pt) 3 (12pt) 4 (14pt) 5 (18pt) 6 (24pt) 7 (36pt)
Bullet List
Number List
Text Color

 

Highlight

 

Remove Formatting

 

Cut Copy Paste Find Undo Redo Align Left Align Center Align Right Align Full Indent Outdent Superscript Subscript Insert/Edit Link Remove Link Direction Left to Right Direction Right to Left Line Horizontal Rule Insert Nonbreaking Space Character
Toggle Spell Checker

 

Browse My YouTube Videos Launch Math Editor
Insert Mashup
Show Nonprinting Characters Blockquote Symbol Emotions Anchor Insert/Edit Table Table Row Properties Table Cell Properties Insert Row Before Insert Row After Delete Row Insert Column Before Insert Column After Delete Column Merge Table Cells Split Merged Table Cells HTML Code View Edit CSS Style

 

Question 5 

(a) Write a, for loop to print odd integers from 1 to 11.

(b) Assume that a variable myLst holds a list of integers. Write a, for loop to print the number multiplied with the ordinal number. For example, if myLst = [ 3, 5, 2, 1], the program displays

0

5

4

3

Question 6

The requirement of this Python program is to read a file, which contains a number of lines. Each line in the file has 4 integers. Assume that the file is open as follows:

myFile = open(“number.data”, “r”)

For example, the file contains

2 3 4 8

1 77 23 12

(a) Write a, for loop to print each line in a line.

For example, it prints out as contained in the file above.

(b) Write a, for loop to print the sum of the number and the average in each line and displays. The sum and average in each line are delimited by a tab. Assume that the file is rewinded.

For example, it prints out as follows:

17       4.25

113      28.25

(c) Assume that a file “arF.dat” contains an arbitrary number of integers in each line. For example, the file contains

2 3 4 “a” 8 9 1

1 77 23 “mercy”

3 “security” 8 22 19 17 21 4

Write a, for loop to do the same as (b) above for the numbers only. Ignore non-digit values. An example run for the above small example data is

27       4.5

101      33.666666666666664

94       13.428571428571429

For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Content Editor

Use arrow keys to select functions

Bold Italic Underline Font family — Font family — Font size — Font size —
Text Color

 

Bullet List Number List Toggle Spell Checker Insert/Edit Link Remove Link
Bold Italic Underline Strikethrough — Format — Heading Sub Heading 1 Sub Heading 2 Paragraph Formatted Code Format Font family — Font family — Andale Mono Arial Arial Black Book Antiqua Comic Sans MS Courier New Georgia Helvetica Impact Symbol Tahoma Terminal Times New Roman Trebuchet MS Verdana Webdings Wingdings Font size — Font size — 1 (8pt) 2 (10pt) 3 (12pt) 4 (14pt) 5 (18pt) 6 (24pt) 7 (36pt)
Bullet List
Number List
Text Color

 

Highlight

 

Remove Formatting

 

Cut Copy Paste Find Undo Redo Align Left Align Center Align Right Align Full Indent Outdent Superscript Subscript Insert/Edit Link Remove Link Direction Left to Right Direction Right to Left Line Horizontal Rule Insert Nonbreaking Space Character
Toggle Spell Checker

 

Browse My YouTube Videos Launch Math Editor
Insert Mashup
Show Nonprinting Characters Blockquote Symbol Emotions Anchor Insert/Edit Table Table Row Properties Table Cell Properties Insert Row Before Insert Row After Delete Row Insert Column Before Insert Column After Delete Column Merge Table Cells Split Merged Table Cells HTML Code View Edit CSS Style

 

Solution
 

Question 1

 Consider the following Python script.

str = “John”

str1 = “Good morning,” + str

str2 = str1 + “How are you?”

print(str2)

a) What is the output?

Good morning,JohnHow are you?

b) Write a Python statement using str, str1 or str2 only to make a list [‘Good’, ‘morning,John’]?

str1.split(maxsplit=1)

Question 2

 Consider the following prompt statement to ask salary:

salary = input(“Enter your salary in Integer”)

Assume that the salary will be increased by 1000.

a) Write a print statement to display the outcome of the increment. For example, if you enter 5000, then your program returns 6000. Give one line print statement.

print (int(salary) + 1000)

b) Assume that a salary can be a real number. For example, 5019.99. Modify your print statement that can handle both integer and real numbers.

print (“%.2f” % (float(salary) + 1000))

Question 3

 Consider the following code segment:

principal = input(“Enter your principal”)

month = input(“Enter the number of installment”)

installment = principal/month

a) If the above Python segment runs, what is the result. Otherwise, explain the error (not copying) and fix it.

The segment does not run because principal and month are strings, they cannot be divided.

The correct one is as follows:

principal = float(input(“Enter your principal”))

month = float(input(“Enter the number of installment”))

if month > 0:

installment = principal/month

else:

installment = 0  # unknown installment

b) In addition to the above Python segment, write one line of a print statement using the format function. An example of the output is

print (“If you pay %.2f for %d times, then you can pay off the principle %d” % (installment, month, principal))

If you pay 217.39 for 23 times, then you can pay off the principle 5000

Question 4

Consider the output of a Python GUI program. The questions below are asking for you to write a Python statement. Assume that the object of Tk() is top and the object of Canvas() is cc.

(a) Write a Python sentence to label “Welcome to John Yoon’s Home” on the window’s title.

top.title(“Welcome to John Yoon’s Home”)

(b) On the canvas, draw a rectangle of width 100 and height 70 from the upper left corner (10, 10). Fill the rectangle with black.

cc.create_rectangle(10, 10, 110, 80, fill=’black’)

(c) On the rectangle, write a text “Mercy Cybersecurity” to be centerized from the point (50,20)

cc.create_text(50, 20, text=’Mercy Cybersecurity’, fill=’red’)

(d) On the canvas again, draw a circle of diameter 50 from the upper left corner (30,30). Fill the rectangle with blue.

cc.create_oval(30, 30, 80, 80, fill=’blue’)

Question 5

 (a) Write a for loop to print odd integers from 1 to 11.

for i in range(1, 12, 2):

print (i)

(b) Assume that a variable myLst holds a list of integers. Write a for loop to print the number multiplied with the ordinal number. For example, if myLst = [ 3, 5, 2, 1], the program displays

0

5

4

3

for i in range(len(myLst)):

print (i * myLst[i])

Question 6

 The requirement of this Python program is to read a file, which contains a number of lines. Each line in the file has 4 integers. Assume that the file is open as follows:

myFile = open(“number.data”, “r”)

For example, the file contains

2 3 4 8

1 77 23 12

(a) Write a for loop to print each line in a line.

For example, it prints out as contained in the file above.

for line in myFile:

print (line, end=””)

(b) Write a, for loop to print the sum of the number and the average in each line and displays. The sum and average in each line are delimited by a tab. Assume that the file is rewinded.

For example, it prints out as follows:

17       4.25

113      28.25

for line in myFile:

values = line.split()

count = 0

total = 0

for value in values:

count += 1

total += int(value)

if count > 0:

average = total / count

else:

average = 0

print (total, ‘\t’, average)

(c) Assume that a file “arF.dat” contains an arbitrary number of integers in each line. For example, the file contains

2 3 4 “a” 8 9 1

1 77 23 “mercy”

3 “security” 8 22 19 17 21 4

Write a for loop to do the same as (b) above for the numbers only. Ignore non-digit values. An example run for the above small example data is

27       4.5

101      33.666666666666664

94       13.428571428571429

for line in myFile:

values = line.split()

count = 0

total = 0

for value in values:

try:

total += int(value)

count += 1

except: # not a integer, ignore

pass

if count > 0:

average = total / count

else:

average = 0

print (total, ‘\t’, average)