Question

15. Write code to open a file named data.txt, which contains 3 numbers, and print the sum of those numbers to the console 16. Write code that opens a file named words.txt containing an essay, and the prints out the SECOND word in the file to the consoleUSING PYTHON PROGRAMMING LANGUAGE

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#Source code of the above program no. 16 is given below

#For Indentation part you have to see the image below

#Assuming words.txt file contains atleast one word per line

f = open("words.txt","r") #opens file with name of "word.txt"
firstLine=" ".join(f.readline().split()[:2])#reading two words of 1st line
firstArr=firstLine.split()# storing word by word in array
print("SECOND word in the file words.txt is given below:")
#checking whether the line contains the second word or not
if len(firstArr)>=2 :
print(firstArr[1])
#if 1st line does not have 2nd word
else:
secLine=" ".join(f.readline().split()[:2])#read 2nd line's first two words
secArr=secLine.split()# storing word by word in array
if len(secArr)>=1:
print(secArr[0])#display 1st word of 2nd line
f.close()#closing file

#For better understanding output screenshot with code image and input file is attached here

#Source code of the above program no. 15 is given below

#For Indentation part you have to see the image below

from decimal import *
f = open('data.txt','r')#openning file
arrayList = []
#reading numbers into a list
for line in f.readlines():
arrayList.extend(line.split())
#display result
print("Data present in file data.txt: ")
print(*arrayList)
print("Sum of those numbers:",end=' ')
#computing sum
print(sum(Decimal(i) for i in arrayList))
f.close()#closing file

# For better understanding output screenshot with code image and input file is attached here*/

data Notepad File Edit Format View Hel 37 46 28 readNumFromFile.py - D: PythonSourcelreadNumFromFile.py (3.6.3) 1回 File Edit

####If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!####

Add a comment
Know the answer?
Add Answer to:
USING PYTHON PROGRAMMING LANGUAGE 15. Write code to open a file named data.txt, which contains 3...
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
  • Create a python code named LetterCount with a text file named words.txt that contains at least...

    Create a python code named LetterCount with a text file named words.txt that contains at least 100 words in any format - some words on the same line, some alone (this program involves no writing so you should not be erasing this file). Then create a program in the main.py that prompts the user for a file name then iterates through all the letters in words.txt, counting the frequency of each letter, and then printing those numbers at the end...

  • PYTHON write a program that will open a file named data.txt and read each line of...

    PYTHON write a program that will open a file named data.txt and read each line of the file one at a time. Each line should be printed to the screen along with a line number

  • *PYTHON EXPERTS ONLY PLEASE* Please provide the answer coded in Python using comments to explain code...

    *PYTHON EXPERTS ONLY PLEASE* Please provide the answer coded in Python using comments to explain code function. Good answers will be rated with thumbs up! Please answer both parts. Thank you for your time. Question 3 - Suppose you have a file named numbers.csv which contains a bunch of integers, five per line of text, separated by commas. Write code below that will open the _le, read the numbers from it, and print the sum of all the even numbers...

  • File Encryption and Decryption chapter 9 programming exercise #3 Design and write a python program to...

    File Encryption and Decryption chapter 9 programming exercise #3 Design and write a python program to successfully complete chapter 9 programming exercise #3. File Encryption and Decryption Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example: codes = { ‘A’ : ‘%’, ‘a’ : ‘9’, ‘B’ : ‘@’, ‘b’ : ‘#’, etc . . .} Using this example, the letter A would be assigned the symbol %, the letter a would...

  • please help, language is python You’re creating a secret code to share with your friends such...

    please help, language is python You’re creating a secret code to share with your friends such that every word should be written with the second and last letters swapped. Write a function called encode that receives a string (input) and prints out another string with the second and last letters swapped. Example function calls: encode(secret) # should print “the code for secret is stcree” encode(city) # should print “the code for city is cyti”

  • PYTHON PROGRAMMING LANGUAGE Task 5 Open a csv file for writing create 3 rows that store...

    PYTHON PROGRAMMING LANGUAGE Task 5 Open a csv file for writing create 3 rows that store username, first name, last name and age data (four columns) write the data to the csv file under the correct column Task 6 Open the file in task 5) and read its contents Output the content in a tabular format of columns and values

  • Create a Word doc file (name of the file: numbers) using Python. Write the following numbers...

    Create a Word doc file (name of the file: numbers) using Python. Write the following numbers to the Word document as shown below and read these numbers from the file and calculate and print the sum and average of these numbers. 9 5 7 6 3 After that, read these values from the file and calculate the sum and average of these numbers.   I have wrote this so far, but doesnt work! def numbersfile(): f = open("numbers.docx, "w") for i...

  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. A. Write a...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. A. Write a function named smaller that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all numbers in the list that are smaller than the number n. Write a code that declares and initializes a list of numbers and a variable number and pass them to the function to test it. Please see the outcome below:...

  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. A. Write a...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. A. Write a function named smaller that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all numbers in the list that are smaller than the number n. Write a code that declares and initializes a list of numbers and a variable number and pass them to the function to test it. Please see the outcome below:...

  • Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. A. Write a...

    Python Language Only!!!! Python Language Only!!!! Python Language Only!!!! Python 3 is used. A. Write a function named smaller that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all numbers in the list that are smaller than the number n. Write a code that declares and initializes a list of numbers and a variable number and pass them to the function to test it. Please see the outcome below:...

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