CS 1101 Learning Journal Unit 5 PDF

Title CS 1101 Learning Journal Unit 5
Course Programming Fundamentals
Institution University of the People
Pages 3
File Size 117.7 KB
File Type PDF
Total Downloads 256
Total Views 392

Summary

Consider the loop from Section 8 of your textbook. prefixes = 'JKLMNOPQ' suffix = 'ack'for letter in prefixes: print(letter + suffix)Put this code into a Python script and run it. Notice that it prints the names "Oack" and "Qack".Modify the program so that it prints "Ouack" and "Quack" but leaves th...


Description

1. Consider the loop from Section 8.3 of your textbook. prefixes = 'JKLMNOPQ' suffix = 'ack' for letter in prefixes: print(letter + suffix) Put this code into a Python script and run it. Notice that it prints the names "Oack" and "Qack". Modify the program so that it prints "Ouack" and "Quack" but leaves the other names the same. Include the modified Python code and the output in your submission. 2. Give at least three examples that show different features of string slices. Describe the feature illustrated by each example. Invent your own examples. Do not copy them for the textbook or any other source. 1. Consider the loop from Section 8.3 of your textbook Input: prefixes = 'JKLMNOPQ' suffix = 'ack' for letter in prefixes: if letter in ('O','Q'): #will execute if prefixes are O or Q print(letter + 'u' + suffix) #will print result with u after prefixes else: #will execute if prefixes are not O or Q print(letter + suffix) #will print result with no u after prefixes Output: Jack Kack Lack Mack Nack Ouack Pack Quack >>>

2. Give at least three examples that show different features of string slices

Input Example 1: phrase = "Change the world by being yourself." #in programming, start counting from 0 #pythonanywhere doesn’t print without the print command print(phrase[11:16]) Output Example 1: world >>> Explanation Example 1: The number 11 stands for the 11th letter of the phrase, which is w. In programming, counting starts from 0. In other words, C is the 0th character. The print command prints the 11th character and everything that is between it and the 16th character. The 16th character doesn’t print.

Input Example 2: phrase = "Change the world by being yourself." #in programming, start counting from 0 print(phrase[:6]) #will print the word ‘Change’ print(phrase[7:10]) #will print the word ‘the’ print(phrase[11:16]) #will print the word ‘world’ print(phrase[17:19]) #will print the word ‘by’ print(phrase[20:25]) #will print the word ‘being’ print(phrase[26:34]) #will print the word ‘yourself’ print(phrase[34:]) #will print the period sign Output Example 2: Change the world by being yourself . >>> Explanation Example 2: The multiple print statements show the range of characters that must print for each line. For example, the first print statement prints the first 5 characters because in programming, the count starts from zero. The no number before :6 means character 0. The last print statements also has no number after 34:, which means that the print statement should print the 34th character and everything after it.

Input Example 3: phrase = "Change the world by being yourself." #in programming, start counting from 0 print(phrase[-9:-1]) print(phrase[26:34]) Output Example 3: yourself yourself >>> Explanation Example 3: This is a long string, so if one wants to take something from the end of the phrase, he or she can count backwards. The count starts from -1. Even though -1 is the period sign, it won’t print. If the statement would have been phrase[-9:], then the output would have been yourself with a period sign....


Similar Free PDFs