Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Differences in Python

The document provides explanations of various Python programming concepts, including the differences between parameters, file modes, and loop structures. It covers the use of the 'with' statement for resource management, the distinction between built-in functions and user-defined functions, and the comparison of global and local scopes. Additionally, it includes examples of file handling and the syntax for opening different types of files.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Differences in Python

The document provides explanations of various Python programming concepts, including the differences between parameters, file modes, and loop structures. It covers the use of the 'with' statement for resource management, the distinction between built-in functions and user-defined functions, and the comparison of global and local scopes. Additionally, it includes examples of file handling and the syntax for opening different types of files.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Q. What is the difference between sep and end in print()?

The end parameter is used to specify a string that will be printed after the output. However, the sep parameter is
used as a separator between the items that you want to print.
Q. Difference between == and „is‟ operators in Python
We use the is operator when we want to compare two objects‟ identities (memory address). On the other hand,
we use the == operator when we want to compare the two objects‟ values.
Differentiation Table
Basis of Comparison For Loop While Loop
Keyword Uses for keyword Uses while keyword
Used For loop is used when the number of While loop is used when the number of
iterations is already known. iterations is already Unknown.

Q. What is the Python “with” statement designed for?


Ans: The `with` statement is used for exception handling to make code cleaner and simpler. It is generally used
for the management of common resources like creating, editing, and saving a file.
Text Files Binary Files CSV Files
1 It is capable to handle textual It is capable to handle large file. It is very common format and
data. platform independent.
2 It consists of series of lines of a It consists of data with a specific It consists of plain text with a list
set of letters, numbers or pattern without any delimiter. of data with a delimiter.
symbols (String)
3 Any text editors like notepad No specific programs can be used to It can be read using text editors
can be used to read them. read them, python provides like notepads and spreadsheet
functions to read data. software.
4 Every line ends with EOL. There is no specific EOL character. It terminates a line automatically
when the delimiter is not used
after data.
Example:
Instead of writing multiple lines of open, try, finally, and close, you can create and write a text file using the
`with` statement. It is simple.
# using with statement
with open('myfile.txt','w') as file:
file.write('DataCamp Black Friday Sale!!!')
Q.14 Differentiate between „w‟ and „a‟ modes
Ans.
“w” write mode “a” append mode
File overwrites an existing file or creates a non-existing File retains the old data and adds new data to the
file file
Opens a file for writing and reading Opens a file for both appending and reading
Q. 5 Differentiate between file modes r+ and w+ with respect to Python.
Ans.
r+ w+
File must exist otherwise error is raised File is created if it does not exist. If exists, the data is truncated
Cannot truncate a file Can truncate a file if it exists

Q.15 How is r+ file mode different from rb+ mode?


Ans. r+ is used to read or write a normal Ascii or Unicode file whereas rb+ mode is used to read or write a
binary file
Q. 16 Differentiate between read() and readlines().
Ans.
read() readlines()
This function is used to read the file This function is used to read all lines from the file and return a list
together in one string with each line as separate string.
It returns string It returns list of Strings.
e.g. F1.read() e.g. F1.readLines()
Q.17 Differentiate write() and writelines().
Ans.
write() writelines()
write() function writes a string into the writelines() function takes in a List of strings and writes that to the
file file.
Syntax=.write(str1) Syntax=.writelines(L)
Write string str1 to file referenced by Writes all strings in list L as lines to file referenced by
Q.18 Differentiate between Absolute Pathnames and Relative Pathnames
Ans.
Absolute Pathnames Relative Pathnames
Absolute paths are from the topmost level of The relative paths are relative to current working directory
the directory structure denoted as a dot(.) while its parent directory is denoted with two
dots(..)
Example: open(“C: Files/myfile.txt”, „r‟) Example: open(“myfile.txt”, „r‟)
Q.8 What is the difference between void and non-void functions?
Ans.
Void functions Non-void functions
Functions not returning any value. It returns None Functions returning some value
It is called non-fruitful functions It is called fruitful functions
Q.9 What is the difference between formal and actual parameters?
Ans.
 Actual Parameters: The values passed are called Actual Parameters,
 Formal Parameters: The values received in the function definition header is called Formal parameters
e.g.
def power(n,r): # n and r are formal parameters
return (n**r)
power(10,2) # 10 and 2 are actual parameters
Q.10 Difference between Global Scope and Local Scope.
Ans.
Global Scope Local Scope

Is a variable defined in the main Is a variable defined within a function


program(_main_) section

It is usable inside the whole program and all locks(functions, other It can be used only within this function and
blocks) contained within the program other blocks contained under it

e.g def power(p):


n = 3 # Global Variable x = p**2 # Localvariable
def power(p): return (x)
return (n**p) n = power (3)
x = power (3) print (n)
print (n, x)

Q. 15 Differentiate between Built-in functions and functions defined in modules.


Ans.
Built-in functions Functions
Built-in functions are pre-defined functions Functions defined in modules are predefined
That are already defined in python and can be used anytime e.g. len(), type(), int(), etc. It can be used only
when the corresponding module is imported
e.g. len(), type(), int(), etc. e.g to use predefined function sqrt() the math
module needs to be imported as import math
Q. 16 How are following two statements different import math and from math import?
Ans.
import math from math import
When we use import math statement a new namespace When we use from math import statement no new
is setup with the same name as that of the module and namespace is created.
all the code of the module is interpreted and executed
here.
All the defined functions and variables of the modules When we use from math import statement no new
are available to the program namespace is created.

Q. Write the file mode that will be used for opening the following files. Also, write the Python statements to
open the following files:
1. a text file “example.txt” in both read and write mode.
2. a binary file “bfile.dat” in write mode.
3. a text file “try.txt” in append and read mode.
4. a binary file “btry.dat” in read only mode.
Answer
1. File Mode: 'r+'
fh = open("example.txt", "r+")
2. File Mode: 'wb'
fh = open("bfile.dat", "wb")
3. File Mode: 'a+'
fh = open("try.txt", "a+")
4. File Mode: 'rb'
fh = open("btry.dat", "rb")

You might also like