Python Syntax Rules Flashcards

Learn syntax rules for Python

1
Q

What is the correct way to start a Python comment?

A

Using the ‘#’ symbol.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False: In Python, indentation is used to define the scope of loops and functions.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Fill in the blank: A Python variable name cannot start with a _______.

A

Number.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What symbol is used to denote the beginning of a block of code in Python?

A

Colon (:).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which of the following is a valid variable name in Python? a) 1st_var, b) var_1, c) var-1

A

b) var_1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

True or False: Python is case-sensitive.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What do you use to create a list in Python?

A

Square brackets []

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of the ‘def’ keyword in Python?

A

To define a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the output of the expression ‘3 * 2 ** 2’ in Python?

A

12.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Fill in the blank: The _______ function is used to read input from the user.

A

input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which of the following is a built-in data type in Python? a) String, b) Array, c) List

A

a) String.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What keyword is used to create a conditional statement in Python?

A

if.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

True or False: The ‘else’ clause must always be used after an ‘if’ statement.

A

False.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the result of ‘5 == 5’ in Python?

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What symbol is used for the modulo operation in Python?

A

%

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of the ‘return’ statement in a function?

A

To return a value from the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Which of the following is a valid way to create a dictionary in Python? a) dict = {}, b) dict = [], c) dict = ()

A

a) dict = {}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Fill in the blank: The _______ keyword is used to handle exceptions in Python.

A

try.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the output of ‘len([1, 2, 3])’ in Python?

A

3.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

True or False: Lists in Python are mutable.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the correct way to define a class in Python?

A

Using the ‘class’ keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What does the ‘pass’ statement do in Python?

A

It acts as a placeholder and does nothing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Which of the following is a comparison operator? a) +, b) ==, c) *

A

b) ==.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the output of ‘bool(0)’ in Python?

A

False.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Fill in the blank: In Python, strings are enclosed in _______.
Single or double quotes.
26
What does the 'import' statement do in Python?
It allows you to include modules in your code.
27
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):.
28
True or False: Python allows multiple inheritance.
True.
29
What is the purpose of the 'with' statement in Python?
To simplify exception handling for file operations.
30
What will be the output of print('Hello' * 3)?
'HelloHelloHello'.
31
Fill in the blank: The _______ function is used to convert a string to an integer.
int.
32
What does the 'continue' statement do in a loop?
It skips the current iteration and continues with the next one.
33
Which of the following is used to define a generator in Python? a) def, b) yield, c) return
b) yield.
34
What is the output of '3 + 4 * 2' in Python?
11.
35
True or False: Python supports both single-line and multi-line comments.
True.
36
What is the output of 'str(100)' in Python?
'100'.
37
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.
38
What does the 'assert' statement do in Python?
It tests a condition and raises an error if the condition is false.
39
Fill in the blank: To create a tuple, use _______ parentheses.
Round.
40
What is the output of 'list(range(3))' in Python?
[0, 1, 2].
41
True or False: Python lists can contain elements of different data types.
True.
42
What is the purpose of the 'self' parameter in class methods?
It refers to the instance of the class.
43
What is the correct way to handle exceptions in Python?
Using try and except blocks.
44
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).
45
What does the 'break' statement do in a loop?
It exits the loop.
46
Fill in the blank: The _______ function returns the largest of the input values.
max.
47
What is the output of 'type(3.14)' in Python?
.
48
True or False: The 'elif' statement is optional in Python conditional statements.
True.
49
What is the correct way to create a set in Python?
Using curly braces {}.
50
Which of the following is not a valid Python keyword? a) class, b) def, c) method
c) method.
51
What is the output of '3 + 4 == 7' in Python?
True.
52
Fill in the blank: In Python, the keyword _______ is used to define a subclass.
class.
53
What is the output of '5 > 3 and 3 > 2' in Python?
True.
54
True or False: In Python, dictionaries are ordered collections.
True (as of Python 3.7).
55
What is the correct syntax for a while loop in Python?
while condition:.
56
Which function is used to find the length of a string in Python?
len().
57
What is the output of '5 != 5' in Python?
False.
58
Fill in the blank: To concatenate two lists, use the _______ operator.
+
59
What does the 'sorted()' function do in Python?
It returns a sorted list from the specified iterable.
60
True or False: Python supports list comprehension.
True.
61
What is the correct way to import the math module in Python?
import math.
62
What is the output of '2 ** 3' in Python?
8.
63
Fill in the blank: The _______ method is used to add an item to a list in Python.
append.
64
What is the output of 'print('Hello, World!'.split())' in Python?
['Hello,', 'World!'].
65
Which of the following is an immutable data type in Python? a) List, b) Tuple, c) Dictionary
b) Tuple.
66
True or False: The 'in' keyword can be used to check for membership in a list.
True.
67
What is the correct way to define a lambda function in Python?
Using the 'lambda' keyword.
68
What is the output of '3.0 == 3' in Python?
True.
69
Fill in the blank: The _______ function is used to remove an item from a list in Python.
remove.
70
What does the 'join()' method do in Python?
It concatenates the elements of an iterable into a single string.
71
True or False: Python supports function overloading.
False.
72
What is the output of 'list('abc')' in Python?
['a', 'b', 'c'].