Python for Data Science: Chapter 2: Functions and Files

Python Functions and Files: Two Marks Important Questions and Answers

Python for Data Science

Python for Data Science: Chapter 2: Functions and Files: Anna University Part A Two Marks Important Questions and Answers

Python for Data Science

Chapter 2: Functions and Files

 

Two Marks Important Questions and Answers

 

1. How to define a function ?

Answer: The function can be defined using the def. The syntax of function definition is as follows‒ def function_name(parameters):

 statements

 

2. What is the syntax of passing an argument to a function ?

Answer: The argument is a value that is passed to the function when it is called. The syntax is as follows ‒

Function_name(variable1, variable2,...)

On the calling side, it is an argument and on the function side it is a parameter.

 

3. What are the uses of return statement ?

Answer: 1) The return statement is used to return a value from a function to the caller.

2) The return statement is sometimes used to return a control back to the caller.

 

4. Explain the terms ‒ scope and lifetime.

Answer: Scope of the variable means ‒ The part of the program in which a variable is accessible.

Lifetime of variable means ‒ Duration for which the variable exists.

 

5. What is file ?

Answer: File is a named location on the disk to store information. It is a mechanism for persistent storage.

 

6. What is the syntax for file open method?

Answer: In Python there is a built in function open() to open a file.

Syntax

File_object=open(file_name,mode)

Where File_object is used to as a handle to the file.

Example

F=open("test.txt")

Here file named test.txt is opened and the file object is in variable F

 

7. What are different modes of file operation?

Answer:

Mode               Purpose

'r     ‒   Open file for reading.

'w'   ‒   Open file for writing. If the file is not created, then create new file and then write. If file is already existing then truncate it.

'x'    ‒  Open a file for creation only. If the file already exists, the operation fails.

'a'    ‒  Open the file for appending mode. Append mode is a mode in which the data

is inserted at the end of existing text. A new file is created if it does not exist.

 't'     ‒  Opens the file in text mode.

'b'    ‒   Opens the file in binary mode.

"+"   ‒   Opens a file for updation i.e. reading and writing.

 

8. Explain the seek and tell methods used in Python file handling.

Answer: The seek method is used to change the file position. Similarly the tell method returns the current position.

Fobj.seek(0) #will set the position at the beginning of the file

Fobj.seek(1) #will set the position at current location

Fobj.seek(2) # will set the position at the end of the file

 

9. What is the difference between readline and readlines methods?

Answer: The readline method reads a single line from the file while the readlines read all the lines written in the file at a time.

 

10. What are command line arguments in Python ?

Answer: The sys.argv is a list in Python, which contains the command‒line arguments passed to the script. With the len(sys.argv) function you can count the number of arguments.

 

11. What is package?

Answer:

Package is a specialized directory containing subpackages, modules and _init__.py file.

 

12. Write a Python script to display current date and time.

Answer:

import datetime

now = datetime.datetime.now()

print ("Current date and time:")

print (now.strftime("%Y‒%m‒%d %H:%M:%S"))

 

13. What is command line argument?

Answer: The command line argument in Python are the input parameters passed to the script when executing them.

In Python the sys module is used to use the command line arguments. We can use sys.argy for getting the list of command line arguments. The len(sys.argv) gives total number of command line arguments.

 

Python for Data Science: Chapter 2: Functions and Files : Tag: Computer Programming, Python, Data Science : Python for Data Science - Python Functions and Files: Two Marks Important Questions and Answers


Python for Data Science: Chapter 2: Functions and Files



Under Subject


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science - Laboratory

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation