Section 1.3 - IT 140_ Introduction to Scripting _ zyBooks

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
print(string1, string2) adds a single space between string1 and string2. Ex: print('Flo', 'wer') outputs Flo wer , followed by a newline. 553398.3976864.qx3zqy7 Basic input Many useful programs allow a user to enter values, such as typing a number, a name, etc. Reading input is achieved using the input() function. The statement best_friend = input() will read text entered by the user and assign the entered text to the best_friend variable. The function input() causes the interpreter to wait until the user has entered some text and has pushed the return key. The input obtained by input() is any text that a user typed, including numbers, letters, or special characters like # or @. Such text in a computer program is called a string and is always surrounded by single or double quotes, for example 'Hello' or "#Goodbye# Amigo!". A string simply represents a sequence of characters. For example, the string 'Hello' consists of the characters 'H', 'e', 'l', 'l', and 'o'. Similarly, the string '123' consists of the characters '1', '2', and '3'. PARTICIPATION ACTIVITY 1.3.7: A program can get an input value from the keyboard. PARTICIPATION ACTIVITY 1.3.8: Reading user input. 1) Which statement reads a user-entered string into variable num_cars? num_cars = input input() = num_cars num_cars = input() PARTICIPATION ACTIVITY 1.3.9: Reading user input. 1) Complete a statement that reads a user-entered string into variable my_var. Converting input types The string '123' (with quotes) is fundamentally different from the integer 123 (without quotes). The '123' string is a sequence of the characters '1', '2', and '3' arranged in a certain order, whereas 123 represents the integer value one- hundred twenty-three. Strings and integers are each an example of a type ; a type determines how a value can behave. For example, integers can be divided by 2, but not strings (what sense would "Hello" / 2 make?). Types are discussed in detail later on. Reading from input always results in a string type. However, often a programmer wants to read in an integer, and then use that number in a calculation. If a string contains only numbers, like '123', then the int() function can be used to convert that string to the integer 123. Figure 1.3.6: Using int() to convert strings to integers. Start Type the program's output print ( 'Joe is great.' ) Joe is great. 2 3 4 Check Next Feedback? print ( 'Enter name of best friend:' , end = ' ' ) best_friend = input () print ( 'My best friend is' , best_friend ) best_friend Input Marty McFly Output Marty McFly Enter name of best friend: Marty McFly My best friend is Marty McFly 1. The input() statement gets an input value from the keyboard and puts that value into the best_friend variable. 2. best_friend's value can then be used in subsequent processing and outputs. Captions Feedback? Feedback? Check Show answer Feedback? 1 Start 2x speed
my_string = '123' my_int = int ( '123' ) print ( my_string ) print ( my_int ) 123 123 A programmer can combine input() and int() to read in a string from the user and then convert that string to an integer for use in a calculation. Figure 1.3.7: Converting user input to integers. print ( 'Enter wage:' , end = ' ' ) wage = int ( input ()) new_wage = wage + 10 print ( 'New wage:' , new_wage ) Enter wage: 8 New wage: 18 PARTICIPATION ACTIVITY 1.3.10: Converting user input to integers. 1) Type a statement that converts the string '15' to an integer and assigns the result to my_var. 2) Complete the code so that new_var is equal to the entered number plus 5. my_var = int ( input ()) new_var = Input prompt Adding a string inside the parentheses of input() displays a prompt to the user before waiting for input and is a useful shortcut to adding an additional print statement line. Figure 1.3.8: Basic input example. hours = 40 weeks = 50 hourly_wage = int ( input ( 'Enter hourly wage: ' )) print ( 'Salary is' , hourly_wage * hours * weeks ) Enter hourly wage: 12 Salary is 24000 ... Enter hourly wage: 20 Salary is 40000 zyDE 1.3.1: Basic input. Run the program and observe the output. Change the input box value from 3 to another number, and run again. 3 Feedback? Feedback? Check Show answer Check Show answer Feedback? Feedback? Load default template... human_years = int ( input ( 'Enter age of dog (in human years): ' )) print () dog_years = 7 * human_years print ( human_years , 'human years is about' , end = ' ' ) print ( dog_years , 'dog years.' ) Run Feedback? 1 2 3 4 5 6 7 8 9 10
Activity summary for assignment: 1-3 zyBooks Participation Activities 98 % 98 % submitted to desire2learn CHALLENGE ACTIVITY 1.3.4: Read user input numbers and perform a calculation. The following program reads in 2 numbers from input, assigns them to num1 and num2 respectively, and then outputs the sum of those numbers. Copy the code provided to see how this code is executed in an autograded system. num1 = int(input()) num2 = int(input()) print(num1 + num2) See How to Use zyBooks for info on how our automated program grader works. 553398.3976864.qx3zqy7 CHALLENGE ACTIVITY 1.3.5: Read user input and print to output. Read three integers from input (without an input prompt). Then, multiply all three integers and output the product. Ex: If the input is: 2 3 5 then the output is: 30 2 * 3 * 5 = 30 Note: Our system will run your program several times, automatically providing different input values each time, to ensure your program works for any input values. See How to Use zyBooks for info on how our automated program grader works. 553398.3976864.qx3zqy7 How was this section? | ''' Your solution goes here ''' Run View your last submission Feedback? ''' Your solution goes here ''' Run View your last submission Feedback? Provide section feedback Completion details 1 2 3 1 2 3
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