Question

#Practical Quiz 3 #Add a comment above each line of code describing what it does. #Be...

#Practical Quiz 3

#Add a comment above each line of code describing what it does.
#Be specific. Use multiple comment lines if you need to
#OR write code below the comments requesting code.
#Make sure your file runs and generates the correct results.

def main():
#define variable str1 with string "Hello"
str1 = "Hello"
#print out the 2nd letter
print(str1[1])

#START QUIZ HERE
print(str1[-2])
print(str1[:3])
print(str1[2:len(str1)-1])
str2 = "World"
print(str1+str2)

#write code below to iterate over the str1 and print each character
#separated by a space in a single line

#write code below. Create 2 lists. Print each. Print concatenation of them.

#write code that demonstrates you can append to list1 created above

#write code that demonstrates parsing of an input phrase into words in a list

#write code that gets pi from the math library and outputs as "3.14"
#using string formatting.

#in the code below, what does line[:-1] evaluate to?
#assume infile has been opened for reading
#assume infile is a text file with multiple lines each line ending with
#a carriage return/linefeed character
'''
for line in infile:
print(line[:-1])
'''
  

main()

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def main():
    # define variable str1 with string "Hello"
    str1 = "Hello"
    # print out the 2nd letter
    print(str1[1])

    # START QUIZ HERE

    # Print out the 2nd last letter "l" of the string "Hello"
    print(str1[-2])

    # Print out the first three letters "Hel" of the string "Hello"
    # str1[:3] means letters from position 0 to 2
    print(str1[:3])

    # Print out the letters "ll" of the string "Hello"
    # Here, length of string is 5
    # str1[2:len(str1) - 1] means letters from position 2 to 3
    print(str1[2:len(str1) - 1])
    str2 = "World"

    # Print out the concatenation of str1 and str2
    print(str1 + str2)

    # write code below to iterate over the str1 and print each character
    # separated by a space in a single line
    for c in str1:
        print(c,end=" ")

    print()
    # write code below. Create 2 lists. Print each. Print concatenation of them.
    # Create 2 lists
    list1=['H','e','l','l','o']
    list2=['W','o','r','l','d']

    # Print both lists
    for e in list1:
        print(e)
    for e in list2:
        print(e)

    # Print concatenation of list
    print(list1+list2)

    # write code that demonstrates you can append to list1 created above
    list1.append('!') # This will add '!' at the end of the list
    print(list1)

    # write code that demonstrates parsing of an input phrase into words in a list
    str3="How are you"
    list3=[]
    # split function will split the string into words based on spaces
    for word in str3.split():
        list3.append(word)  # append words in list
    print(list3)

    # write code that gets pi from the math library and outputs as "3.14"
    # using string formatting.
    import math
    print("%.2f" % math.pi)

    # in the code below, what does line[:-1] evaluate to?
    # assume infile has been opened for reading
    # assume infile is a text file with multiple lines each line ending with
    # a carriage return/linefeed character
    infile = open("text.txt", "r")

    # line[:-1] prints the whole line except the last character which is the carriage return
    # So this loop will print lines without a line between each line
    for line in infile:
        print(line[:-1])

main()

SCRRENSHOT

q.py def main): # define variable stri vith string Hello strl Hello # print out the 2nd letter print (strl[l]) # START QU

34 35 38 37 38 39 40 41 42 43 list2 [W,or,l,d] # Print both lists for e in listl: print (e) for e in list2: print (e) # Pri

61 62 63 # in the code below , what does line [:-1] evaluate to? # assume infile has been opened for reading # assume infile

OUTPUT

↑ C:\Users\mahes\AppData\Local\Programs\Python\Python36 Helloworld Hell o areyou] How 3.14 Hello hi how are you are you th

Add a comment
Know the answer?
Add Answer to:
#Practical Quiz 3 #Add a comment above each line of code describing what it does. #Be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone help me out with this? You are given a program that receives four lines...

    Can someone help me out with this? You are given a program that receives four lines in the below format, and stores them in str1, str2, str3, and num1. This is not a very long sentence. is long 4 Expand this program to: Write an if-elseif-else statement to print this line only if num1 is higher than 0: "Num1 is higher than 0!" print this line only if num1 is 0: "Num1 equals to 0!" And otherwise, print: ""Num1 is...

  • Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for...

    Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for writing and reading from console #include <fstream> //this one to deal with files, read and write to text files using namespace std; int main() { //first thing we need to read the data in the text file //let's assume we have the reading in a text file called data.txt //so, we need a stream input variable to hold this data ifstream infile; //now we...

  • Please help me. package a1; public class Methods {    /*    * Instructions for students:...

    Please help me. package a1; public class Methods {    /*    * Instructions for students: Use the main method only to make calls to the    * other methods and to print some testing results. The correct operation of    * your methods should not depend in any way on the code in main.    *    * Do not do any printing within the method bodies, except the main method.    *    * Leave your testing code...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Add high level comments throughout the program to explain what each line of code does. Finally,...

    Add high level comments throughout the program to explain what each line of code does. Finally, use a diagramming tool (Visio, Lucidchart, MSPaint, paper & pen) to create a flowchart of the code below. path = "/Users/user1/data/baby_names/" myyear = input("Please provide the year to be searched:") babyname = input("Please provide the baby name to be searched:") babyname = babyname.capitalize() baby_counts = count_babies(myyear, babyname) print("There were {0} boys and {1} girls with that name in {2}".format( baby_counts[0], baby_counts[1], myyear)) def count_babies(year,...

  • The last 3 cases are tests .cpp files to test the code. The language of this...

    The last 3 cases are tests .cpp files to test the code. The language of this code must be C++ because that is the only I am familiar with. Soreland, a software company, is planning on releasing its first C++ compiler with the hopes of putting MiniSoft's Visual C++ out of business (that reminds me of another story). Consequently, Soreland has contracted with the Fruugle Corporation to design and build part of their compiler. Of course, since Fruugle gets all...

  • Instructions: Each line of code is to be explained in a comment. I have provided an...

    Instructions: Each line of code is to be explained in a comment. I have provided an example for the first line of code. I want to know what the statement is doing, i.e. creating a function, using module, assigning a value and specifically what is the variable, function, parameters etc… Answer, What will be printed and What is the output in a comment The use of Python Library 2.7 may necessary to complete the assignment. 1. # port variable is...

  • Lab 4.1 Utilizing the code from Lab 3.2, add an overloaded operator == which will be...

    Lab 4.1 Utilizing the code from Lab 3.2, add an overloaded operator == which will be used to compare two objects to see if they are equal. For our purposes, two objects are equal if their abbreviation and uldid are the same. You’ll need to make your overloaded operator a friend of the Cargo class. Create a unit1 object and load it with this data: uld – Pallet abbreviation – PAG uldid – PAG32597IB aircraft - 737 weight – 3321...

  • Below is my code please help me edit it so in the beginning it ask for Press any key to start Tas...

    below is my code please help me edit it so in the beginning it ask for Press any key to start Task n, where n is the task number (1, 2, or 3,4). I already wrote the code for the 4 task. Also please write it so when 1 code is finish running it return to the prompt where it ask to run another task #task 1 list1 = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','.',',','?'] morse = ['.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','-----','.----','..---','...--','....-','.....','-....','--...','---..','----.','.-.-.-','--..--','..--..'] inp = input("Enter original text : ")...

  • c++ only. Please follow directions. What does the following program print and why? Comment each line...

    c++ only. Please follow directions. What does the following program print and why? Comment each line of code to explain what it is doing.   #include <iostream> using namespace std; int main() { int track[ ] = { 10, 20, 30, 40 }; int * ptr; ptr = track; track[1] += 30; cout << * ptr << " "; *ptr -= 10; ptr++; cout << * ptr << " "; ptr += 2; cout << * ptr << " "; cout...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT