Challenge your Python fundamentals on functions, scope, and beginner-friendly real-world projects. Refresh your understanding with these logic-driven questions ideal for early learners.
Which of the following correctly defines a function in Python that takes no arguments and prints 'Hello, World!'?
Explanation: The correct syntax uses 'def' followed by the function name, parentheses, and a colon. Option B uses incorrect keywords and syntax. Option C is missing parentheses needed for function definition. Option D mixes function call and definition in an invalid way.
If a variable is defined inside a function in Python, where can it be accessed?
Explanation: A variable defined inside a function is local to that function and cannot be accessed outside. The other options are incorrect because local variables do not have global or module-wide visibility.
Which standard library module would you import to generate random numbers for a guessing game?
Explanation: The 'random' module in Python is specifically designed for generating random numbers. 'math' provides mathematical functions but no randomization. 'datetime' manages dates and times, and 'os' handles operating system tasks.
What will be the output of this code?ndef add(a, b):n return a + bnprint(add(3, 5))
Explanation: The 'add' function returns the sum of its arguments, so print(add(3, 5)) outputs 8. '35' would be correct if concatenation or string returns were involved. 'Error' is incorrect because the code is valid. '3 + 5' would appear if the function returned a string.
Which logic would best catch invalid numeric input when building a unit converter in Python?
Explanation: Using try-except handles invalid conversions gracefully by catching ValueError. Checking if input is not None does not assure a numeric value. Using assert alone is not user-friendly for handling input errors. Ignoring errors leads to program crashes, which is never recommended.