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
Q

Fill in the blank: In Python, strings are enclosed in _______.

A

Single or double quotes.

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

What does the ‘import’ statement do in Python?

A

It allows you to include modules in your code.

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

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

a) for i in range(5):.

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

True or False: Python allows multiple inheritance.

A

True.

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

What is the purpose of the ‘with’ statement in Python?

A

To simplify exception handling for file operations.

30
Q

What will be the output of print(‘Hello’ * 3)?

A

‘HelloHelloHello’.

31
Q

Fill in the blank: The _______ function is used to convert a string to an integer.

32
Q

What does the ‘continue’ statement do in a loop?

A

It skips the current iteration and continues with the next one.

33
Q

Which of the following is used to define a generator in Python? a) def, b) yield, c) return

34
Q

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

35
Q

True or False: Python supports both single-line and multi-line comments.

36
Q

What is the output of ‘str(100)’ in Python?

A

‘100’.

37
Q

Which keyword is used to define a module in Python?

A

There is no specific keyword; modules are created by saving a file with a .py extension.

38
Q

What does the ‘assert’ statement do in Python?

A

It tests a condition and raises an error if the condition is false.

39
Q

Fill in the blank: To create a tuple, use _______ parentheses.

40
Q

What is the output of ‘list(range(3))’ in Python?

A

[0, 1, 2].

41
Q

True or False: Python lists can contain elements of different data types.

42
Q

What is the purpose of the ‘self’ parameter in class methods?

A

It refers to the instance of the class.

43
Q

What is the correct way to handle exceptions in Python?

A

Using try and except blocks.

44
Q

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

a) def func(x=1).

45
Q

What does the ‘break’ statement do in a loop?

A

It exits the loop.

46
Q

Fill in the blank: The _______ function returns the largest of the input values.

47
Q

What is the output of ‘type(3.14)’ in Python?

A

<class ‘float’>.

48
Q

True or False: The ‘elif’ statement is optional in Python conditional statements.

49
Q

What is the correct way to create a set in Python?

A

Using curly braces {}.

50
Q

Which of the following is not a valid Python keyword? a) class, b) def, c) method

A

c) method.

51
Q

What is the output of ‘3 + 4 == 7’ in Python?

52
Q

Fill in the blank: In Python, the keyword _______ is used to define a subclass.

53
Q

What is the output of ‘5 > 3 and 3 > 2’ in Python?

54
Q

True or False: In Python, dictionaries are ordered collections.

A

True (as of Python 3.7).

55
Q

What is the correct syntax for a while loop in Python?

A

while condition:.

56
Q

Which function is used to find the length of a string in Python?

57
Q

What is the output of ‘5 != 5’ in Python?

58
Q

Fill in the blank: To concatenate two lists, use the _______ operator.

59
Q

What does the ‘sorted()’ function do in Python?

A

It returns a sorted list from the specified iterable.

60
Q

True or False: Python supports list comprehension.

61
Q

What is the correct way to import the math module in Python?

A

import math.

62
Q

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

63
Q

Fill in the blank: The _______ method is used to add an item to a list in Python.

64
Q

What is the output of ‘print(‘Hello, World!’.split())’ in Python?

A

[‘Hello,’, ‘World!’].

65
Q

Which of the following is an immutable data type in Python? a) List, b) Tuple, c) Dictionary

66
Q

True or False: The ‘in’ keyword can be used to check for membership in a list.

67
Q

What is the correct way to define a lambda function in Python?

A

Using the ‘lambda’ keyword.

68
Q

What is the output of ‘3.0 == 3’ in Python?

69
Q

Fill in the blank: The _______ function is used to remove an item from a list in Python.

70
Q

What does the ‘join()’ method do in Python?

A

It concatenates the elements of an iterable into a single string.

71
Q

True or False: Python supports function overloading.

72
Q

What is the output of ‘list(‘abc’)’ in Python?

A

[‘a’, ‘b’, ‘c’].