Friday, August 9, 2024

Questions on: If __name__ == '__main__' ( A Python Lesson )

To See All Python Articles: Python Course (Index)



Question 1: Which of these is not a module?

A. libraries
B. functions
C. classes
D. files
E. None of the above.


Answer:

All of these are modules: libraries, functions, classes and files.
Correct answer: E

~~~

Question 2: 

What will this print:


import pandas
df = pandas.DataFrame([1, 2])
print(__name__)


~~~

Q3:

What will this print:


import pandas
df = pandas.DataFrame([1, 2])
print(pandas.__name__)


~~~

Q4:

What will this print:


import pandas as pd
df = pandas.DataFrame([1, 2])
print(pd.__name__)


Q5:

What will this print:


from pandas import DataFrame
print(DataFrame.__name__)


Q6: What will this print:

from pandas import DataFrame
print(pandas.__name__)


Answer: NameError: name 'pandas' is not defined


Rules of Thumb

For libraries:
Remove the .py filename extension to get the __name__ of any library.

For Classes and Functions:

The __name__ is the same as the name of the class.

~~~

Q7: What will this print:


++++ File: import_me.py ++++

def call_me():
    print("Hello!!!")

call_me()

++++ File: run_me.py ++++

from import_me import call_me

call_me()
print(5+5) 

Output:


Hello!!!
Hello!!!
10

~~~


Ratings

Harshitha4
Tags: Python,Technology,

No comments:

Post a Comment