Section 1

.pdf

School

Southern New Hampshire University *

*We aren’t endorsed by this school

Course

140 - X625

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

6

Uploaded by DeaconCaterpillar4154

Report
Students: Section 1.3 is a part of 1 assignment: 1-3 zyBooks Participation Activities Includes: PA 1.3 Basic input and output Basic text output Printing of output to a screen is a common programming task. This section describes basic output; later sections have more details. The primary way to print output is to use the built-in function print() . Printing text is performed via: print('hello world') . Text enclosed in quotes is known as a string literal . Text in string literals may have letters, numbers, spaces, or symbols like @ or #. Each print statement will output on a new line. A new output line starts after each print statement, called a newline . A print statement's default behavior is to automatically end with a newline. However, using print('Hello', end=' ') , specifying end=' ' , keeps the next print's output on the same line separated by a single space. Any space, tab, or newline is called whitespace . A string literal can be surrounded by matching single or double quotes: 'Python rocks!' or "Python rocks!" . Good practice is to use single quotes for shorter strings and double quotes for more complicated text or text that contains single quotes (such as print("Don't eat that!") ). Figure 1.3.1: Printing text and new lines. # Each print statement starts on a new line print ( 'Hello there.' ) print ( 'My name is...' ) print ( 'Carl?' ) Hello there. My name is... Carl? # Including end=' ' keeps output on same line print ( 'Hello there.' , end = ' ' ) print ( 'My name is...' , end = ' ' ) print ( 'Carl?' ) Hello there. My name is... Carl? PARTICIPATION ACTIVITY 1.3.1: Basic text output. 1) Select the statement that prints the following: Welcome! print(Welcome!) print('Welcome!") print('Welcome!') 2) Which pair of statements print output on the same line? print('Halt!') print('No access!') print('Halt!', end=' ') print('No access!') print(Halt!, end=' ') print(No Access!, end=' ') PARTICIPATION ACTIVITY 1.3.2: Basic text output. 1) Type a statement that prints the following: Hello CHALLENGE ACTIVITY 1.3.1: Output simple text. Write the simplest statement that prints the following: 3 2 1 Go! Note: Whitespace (blank spaces / blank lines) matters; make sure your whitespace exactly matches the expected output. Learn how our autograder works 553398.3976864.qx3zqy7 Feedback? Feedback? Check Show answer Feedback? ''' Your solution goes here ''' 1 2 3
CHALLENGE ACTIVITY 1.3.2: Output an eight with asterisks. Output the following ±gure with asterisks. Do not add spaces after the last character in each line. ***** * * ***** * * ***** Note: Whitespace (blank spaces / blank lines) matters; make sure your whitespace exactly matches the expected output. Learn how our autograder works 553398.3976864.qx3zqy7 Outputting a variable's value The value of a variable can be printed out via: print(variable_name) (without quotes). Figure 1.3.2: Printing the value of a variable. wage = 20 print ( 'Wage is' , end = ' ' ) print ( wage ) # print variable's value print ( 'Goodbye.' ) Wage is 20 Goodbye. PARTICIPATION ACTIVITY 1.3.3: Basic variable output. 1) Given the variable num_cars = 9, which statement prints 9? print(num_cars) print("num_cars") PARTICIPATION ACTIVITY 1.3.4: Basic variable output. 1) Write a statement that prints the value of the variable num_people. Outputting multiple items with one statement Programmers commonly try to use a single print statement for each line of output by combining the printing of text, variable values, and new lines. The programmer simply separates the items with commas; each item in the output will be separated by a space. Such combining can improve program readability, because the program's code corresponds more closely to the program's printed output. Run View your last submission Feedback? ''' Your solution goes here ''' Run View your last submission Feedback? Feedback? Feedback? Check Show answer Feedback? 1 2 3
Figure 1.3.3: Printing multiple items using a single print statement. wage = 20 print ( 'Wage:' , wage ) # Comma separates multiple items print ( 'Goodbye.' ) Wage: 20 Goodbye. A common error is to forget the comma between items, as in print('Name' user_name) . Newline characters Output can be moved to the next line by printing \n , known as a newline character . Ex: print ('1\n2\n3') prints "1" on the ±rst line, "2" on the second line, and "3" on the third line of output. \n consists of two characters, \ and n, but together are considered by the Python interpreter as a single character. Figure 1.3.4: Printing using newline characters. print ( '1\n2\n3' ) 1 2 3 Using print() by itself without any text also prints a single newline. Figure 1.3.5: printing without text. print ( '123' ) print () print ( 'abc' ) 123 abc NOTE: In a normal programming environment, program input is provided interactively and completed by pressing the enter key. The enter key press would insert a newline. Since zyBooks input is pre-entered, no enter key press can be inferred. Thus, activities that require pre-entered input may need extra newline characters or blank print statements in zyBooks, compared to other environments. PARTICIPATION ACTIVITY 1.3.5: Output simulator. The tool below allows for experimenting with print statements. The variables country_population = 1344130000 and country_name = 'China' have been de±ned and can be used in the simulator. Try printing the following output: The population of China was 1344130000 in 2011. PARTICIPATION ACTIVITY 1.3.6: Single print statement. Assume variable age = 22, pet = "dog", and pet_name = "Gerald". 1) What is the output of print('You are', age, 'years old.') 2) What is the output of print(pet_name, 'the', pet, 'is', age) CHALLENGE ACTIVITY 1.3.3: Enter the output. Type the program's output. Note: Each print() outputs a new line, so create a newline after your output by pressing Enter or Return on your keyboard. Feedback? Feedback? Feedback? print( 'Change this string!' ) Change this string! Feedback? Check Show answer Check Show answer Feedback?
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help