Python Syntax Rules Flashcards
Learn syntax rules for Python
What is the correct way to start a Python comment?
Using the ‘#’ symbol.
True or False: In Python, indentation is used to define the scope of loops and functions.
True.
Fill in the blank: A Python variable name cannot start with a _______.
Number.
What symbol is used to denote the beginning of a block of code in Python?
Colon (:).
Which of the following is a valid variable name in Python? a) 1st_var, b) var_1, c) var-1
b) var_1.
True or False: Python is case-sensitive.
True.
What do you use to create a list in Python?
Square brackets []
What is the purpose of the ‘def’ keyword in Python?
To define a function.
What is the output of the expression ‘3 * 2 ** 2’ in Python?
12.
Fill in the blank: The _______ function is used to read input from the user.
input.
Which of the following is a built-in data type in Python? a) String, b) Array, c) List
a) String.
What keyword is used to create a conditional statement in Python?
if.
True or False: The ‘else’ clause must always be used after an ‘if’ statement.
False.
What is the result of ‘5 == 5’ in Python?
True.
What symbol is used for the modulo operation in Python?
%
What is the purpose of the ‘return’ statement in a function?
To return a value from the function.
Which of the following is a valid way to create a dictionary in Python? a) dict = {}, b) dict = [], c) dict = ()
a) dict = {}.
Fill in the blank: The _______ keyword is used to handle exceptions in Python.
try.
What is the output of ‘len([1, 2, 3])’ in Python?
3.
True or False: Lists in Python are mutable.
True.
What is the correct way to define a class in Python?
Using the ‘class’ keyword.
What does the ‘pass’ statement do in Python?
It acts as a placeholder and does nothing.
Which of the following is a comparison operator? a) +, b) ==, c) *
b) ==.
What is the output of ‘bool(0)’ in Python?
False.
Fill in the blank: In Python, strings are enclosed in _______.
Single or double quotes.
What does the ‘import’ statement do in Python?
It allows you to include modules in your code.
Which of the following is the correct way to create a for loop in Python? a) for i in range(5):, b) for i = 0; i < 5; i++, c) for (i in range(5))
a) for i in range(5):.
True or False: Python allows multiple inheritance.
True.
What is the purpose of the ‘with’ statement in Python?
To simplify exception handling for file operations.
What will be the output of print(‘Hello’ * 3)?
‘HelloHelloHello’.
Fill in the blank: The _______ function is used to convert a string to an integer.
int.
What does the ‘continue’ statement do in a loop?
It skips the current iteration and continues with the next one.
Which of the following is used to define a generator in Python? a) def, b) yield, c) return
b) yield.
What is the output of ‘3 + 4 * 2’ in Python?
11.
True or False: Python supports both single-line and multi-line comments.
True.
What is the output of ‘str(100)’ in Python?
‘100’.
Which keyword is used to define a module in Python?
There is no specific keyword; modules are created by saving a file with a .py extension.
What does the ‘assert’ statement do in Python?
It tests a condition and raises an error if the condition is false.
Fill in the blank: To create a tuple, use _______ parentheses.
Round.
What is the output of ‘list(range(3))’ in Python?
[0, 1, 2].
True or False: Python lists can contain elements of different data types.
True.
What is the purpose of the ‘self’ parameter in class methods?
It refers to the instance of the class.
What is the correct way to handle exceptions in Python?
Using try and except blocks.
Which of the following is a valid way to define a function with default parameters? a) def func(x=1), b) def func(x:1), c) def func(x: int)
a) def func(x=1).
What does the ‘break’ statement do in a loop?
It exits the loop.
Fill in the blank: The _______ function returns the largest of the input values.
max.
What is the output of ‘type(3.14)’ in Python?
<class ‘float’>.
True or False: The ‘elif’ statement is optional in Python conditional statements.
True.
What is the correct way to create a set in Python?
Using curly braces {}.
Which of the following is not a valid Python keyword? a) class, b) def, c) method
c) method.
What is the output of ‘3 + 4 == 7’ in Python?
True.
Fill in the blank: In Python, the keyword _______ is used to define a subclass.
class.
What is the output of ‘5 > 3 and 3 > 2’ in Python?
True.
True or False: In Python, dictionaries are ordered collections.
True (as of Python 3.7).
What is the correct syntax for a while loop in Python?
while condition:.
Which function is used to find the length of a string in Python?
len().
What is the output of ‘5 != 5’ in Python?
False.
Fill in the blank: To concatenate two lists, use the _______ operator.
+
What does the ‘sorted()’ function do in Python?
It returns a sorted list from the specified iterable.
True or False: Python supports list comprehension.
True.
What is the correct way to import the math module in Python?
import math.
What is the output of ‘2 ** 3’ in Python?
8.
Fill in the blank: The _______ method is used to add an item to a list in Python.
append.
What is the output of ‘print(‘Hello, World!’.split())’ in Python?
[‘Hello,’, ‘World!’].
Which of the following is an immutable data type in Python? a) List, b) Tuple, c) Dictionary
b) Tuple.
True or False: The ‘in’ keyword can be used to check for membership in a list.
True.
What is the correct way to define a lambda function in Python?
Using the ‘lambda’ keyword.
What is the output of ‘3.0 == 3’ in Python?
True.
Fill in the blank: The _______ function is used to remove an item from a list in Python.
remove.
What does the ‘join()’ method do in Python?
It concatenates the elements of an iterable into a single string.
True or False: Python supports function overloading.
False.
What is the output of ‘list(‘abc’)’ in Python?
[‘a’, ‘b’, ‘c’].