Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 3 Lets learn about the print() built-in function (output statement) and escape sequences (or characters). Create a Python program using the Python Code Fragment

Exercise 3

Lets learn about the print() built-in function (output statement) and escape sequences (or characters). Create a Python program using the Python Code Fragment 1 below and see what it produces.

Python Code Fragment 1:

name = "Louise MacLeavy" income = 37500.2567 SIN = 123456789 age = 20 print("The employee record for %s contains: \tSIN %i \tage %d\tsalary of $%0.2f" %(name, SIN, age, income))

Make sure you understand what is happening in the above code fragment. Specifically, make sure we know what the following items do and why we would use them:

  • the %s, %i, %d, %f
  • the \t and the , which are called escape sequences or escape characters (what other escape sequences are there?)
  • old string formatting mechanism: "$%0.2f" %(name, SIN, age, income). This string formatting mechanism is sometimes referred to as printf string It also has other names. Note that the 0 in "$%0.2f" is the number zero 0, not the capital letter O.

While investigating the above, feel free to browse the Internet. Just remember to add Python 3 to our query as Python 2 does printing differently than Python 3. Here is a table listing some escape sequences:

Source: https://docs.python.org/3.1/reference/lexical_analysis.html (Links to an external site.)

In terms of the old string formatting, we may find the information described at this link (Links to an external site.) very useful.

Is the Python code fragment below equivalent, i.e., outputs the same result, to the Python code fragment above?

Python Code Fragment 2:

name = "Louise MacLeavy" income = 37500.2567 SIN = 123456789 age = 20 record = "\tSIN %i\tage %d\tsalary of $%0.2f" %(SIN, age, income) print("The employee record for %s contains: %s" %(name,record))

Once again, make sure you understand what is happening in the above code fragment.

excersie 5

What is the result of the following Python Code Fragments?

Python Code Fragment 1:

# With a string word = 'Greetings' print("len(word): ", len(word)) print("word[0]) : ", word[0]) word2 = word + '!' print("word2 : ", word2) print("'e' in word? ", 'e' in word) # What is this in operator? print("'a' in word? ", 'a' in word) print("word*3: ", word*3)

Python Code Fragment 2:

# With a list aList = [1,'b',300] print("len(aList): ", len(aList)) print("aList[0]) : ", aList[0]) bList = aList + ['D',5.5] print("bList) : ", bList) print("300 in aList? ", 300 in aList) print("1 in aList? ", 1 in aList) print("'a' in aList? ", 'a' in aList) print("'b' in aList? ", 'b' in aList) print("aList*3: ", aList*3)

Lets add the following Python statements to our program:

  • a statement that would create an index out of range error using a string,
  • a statement that would create an index out of range error using a list,
  • a statement that would successfully create and print a slice from a string,
  • a statement that would successfully create and print a slice from a list,
  • a statement that would successfully step through a string, printing every 3rd character starting from the second character at index 1,
  • a statement that would successfully step through a list, printing every 4th element starting from the first element at index 0.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions