Python Flashcards
23 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 20 Python flashcards as text
What is Python?
Answer: General-purpose interpreted, interactive language, object-oriented and high-level programming language
Python is accurately described as a general-purpose, high-level programming language. It is interpreted, meaning code is executed line by line rather than compiled, and interactive, allowing for direct execution of commands. Furthermore, Python supports multiple programming paradigms, including object-oriented programming, making it versatile for various applications.
Which license does the Python source code fall under?
Answer: GPL
The Python source code is licensed under the Python Software Foundation License (PSF License), which is an OSI-approved, free software license. While not strictly the GNU General Public License (GPL) itself, the PSF License is explicitly designed to be compatible with the GPL. This compatibility allows Python to be freely used, modified, and distributed, including in projects that are licensed under the GPL.
Who created Phyton?
Answer: Guido van Rossum
Python was created by Guido van Rossum in the late 1980s, with its first public release in 1991. He served as Python's 'Benevolent Dictator For Life' (BDFL) until 2018, overseeing its development and direction. His vision and leadership shaped Python into the popular and versatile language it is today.
Which of the following is a Python environment variable that isn't valid?
Answer: PYTHONLIBRARY
Python uses several environment variables to configure its behavior, such as `PYTHONSTARTUP` (for an initialization script), `PYTHONPATH` (for module search paths), and `PYTHONCASEOK` (on Windows, for case-insensitive imports). `PYTHONLIBRARY` is not a standard or recognized environment variable used by the Python interpreter for its configuration or operation.
What specifically is PYTHONPATH?
Answer: It tells the Python interpreter where to locate the module files imported into a program
The `PYTHONPATH` environment variable is crucial for the Python interpreter. It specifies a list of directories that the interpreter should search for module files when an `import` statement is encountered. This allows users to extend Python's default search path and include custom modules or packages located in non-standard directories.
What is the meaning of PYTHONHOME?
Answer: It is an alternative module search path
The `PYTHONHOME` environment variable is used to specify an alternative Python installation root directory. When set, it tells the Python interpreter where to find its standard library modules and other core files, effectively acting as an alternative base for the module search path. This is particularly useful for embedding Python or managing multiple Python installations.
What is the purpose of PYTHONSTARTUP?
Answer: It is executed every time you start the interpreter
PYTHONSTARTUP is an environment variable that points to a Python script. When the Python interpreter starts in interactive mode, it automatically executes the commands in the script specified by this variable. This allows users to define custom functions, import modules, or set up a specific environment that is available every time they enter the interactive interpreter.
What does the -d command line option do?
Answer: It provides debug output
The `-d` command-line option for the Python interpreter enables debug output. Specifically, it turns on debugging output for the parser, which can be useful for understanding how Python processes its source code. This option helps developers diagnose issues related to parsing or compilation by providing more verbose information about the interpreter's internal operations.
What is PYTHONCASEOK, and how does it work?
Answer: It is used to find the first case-insensitive match in an import statement
PYTHONCASEOK is an environment variable that influences how Python handles module imports on case-insensitive file systems, like Windows. When set, Python will attempt to find a module even if its filename's casing doesn't exactly match the import statement. It allows for a case-insensitive search for the module file, importing the first match it finds.
Which of these naming conventions for Python identifiers is incorrect?
Answer: An identifier can start with a number
In Python, identifiers (like variable names, function names, class names) must start with a letter (a-z, A-Z) or an underscore (_). They cannot begin with a number. While numbers can be part of an identifier after the first character, starting with a digit is a syntax error.
What does the following code provide as an output? for x in range(0.5, 5.5, 0.5): print(x)
Answer: The Program executed with errors
The `range()` function in Python only accepts integer arguments for its start, stop, and step parameters. Providing floating-point numbers like 0.5 or 5.5 will raise a `TypeError`. Therefore, this code will result in an error during execution because `range()` cannot handle non-integer values.
What does the following code output? var = "James" * 2 * 3 print(var)
Answer: JamesJamesJamesJamesJamesJames
In Python, the `*` operator can be used to repeat a string. The expression `"James" * 2` evaluates to "JamesJames". This result is then multiplied by 3, meaning "JamesJames" is repeated three times, leading to the final string "JamesJamesJamesJamesJamesJames".
Is it possible to utilize the "else" clause in loops? as an example: for i in range(1, 5): print(i) else: print("this is else block statement" )
Answer: A) Yes
Python allows an `else` clause to be used with `for` and `while` loops. The code inside the `else` block is executed if the loop completes normally (i.e., without being terminated by a `break` statement). In this example, the loop finishes iterating through all numbers from 1 to 4, so the `else` block will execute and print its statement.
What is the output of the code below? listOne = [20, 40, 60, 80] listTwo = [20, 40, 60, 80] print(listOne == listTwo) print(listOne is listTwo)
Answer: True False
The `==` operator checks for value equality, meaning it compares the contents of the two lists. Since `listOne` and `listTwo` contain the same elements, `listOne == listTwo` is `True`. The `is` operator, however, checks for identity, meaning if two variables refer to the exact same object in memory. Since `listOne` and `listTwo` are distinct list objects, `listOne is listTwo` is `False`.
What does the following code output? sampleList = ["Jon", "Kelly", "Jessa"] sampleList.append(2, "Scott") print(sampleList)
Answer: The program executed with errors
The `append()` method for Python lists takes exactly one argument: the item to be added to the end of the list. In this code, `sampleList.append(2, "Scott")` attempts to pass two arguments. This will raise a `TypeError` because `append()` was called with an incorrect number of arguments.
What does following code output? str = "pynative" print (str[1:3])
Answer: yn
Python string slicing uses the syntax `[start:end]`, where `start` is the inclusive starting index and `end` is the exclusive ending index. In "pynative", 'p' is at index 0, 'y' at index 1, 'n' at index 2, and 'a' at index 3. Therefore, `str[1:3]` extracts characters from index 1 up to (but not including) index 3, resulting in 'y' and 'n'.
What does the following code output? def calculate (num1, num2=4): res = num1 * num2 print(res) calculate(5, 6)
Answer: 30
The `calculate` function is defined with a default argument `num2=4`. However, when `calculate(5, 6)` is called, the value `6` is explicitly passed for `num2`, overriding the default. So, `num1` becomes 5 and `num2` becomes 6. The function then calculates `res = 5 * 6`, which is 30, and prints this result.
What does the following code output? sampleSet = {"Jodi", "Eric", "Garry"} sampleSet.add(1, "Vicki") print(sampleSet)
Answer: The program executed with error
The `add()` method for Python sets takes exactly one argument: the element to be added to the set. Sets are unordered collections of unique elements. In this code, `sampleSet.add(1, "Vicki")` attempts to pass two arguments. This will raise a `TypeError` because the `add()` method was called with an incorrect number of arguments.
What does the following code output? for i in range(10, 15, 1): print( i, end=', ')
Answer: B) 10, 11, 12, 13, 14,
The `range(start, stop, step)` function generates a sequence of numbers starting from `start` (inclusive) up to `stop` (exclusive), incrementing by `step`. Here, `range(10, 15, 1)` will produce numbers 10, 11, 12, 13, and 14. The `print()` function's `end=', '` argument ensures that each number is followed by a comma and a space, rather than a newline.
What does the following code output? valueOne = 5 ** 2 valueTwo = 5 ** 3 print(valueOne) print(valueTwo)
Answer: 25 125
The `**` operator in Python denotes exponentiation. `5 ** 2` calculates 5 raised to the power of 2, which is 5 * 5 = 25. Similarly, `5 ** 3` calculates 5 raised to the power of 3, which is 5 * 5 * 5 = 125. The code then prints these two results on separate lines.