Playing as a guest? to earn XP and track your progress.
Python Fundamentals Quiz
Python Fundamentals
What is a character set in Python?
A group of string variables
The list of Python keywords
The set of valid characters that Python can recognise
The fonts used by IDLE
A character set is the set of valid characters a language recognises; a character is the smallest unit of a program.
Which of these is NOT part of Python's character set categories?
Digits
Whitespaces
Letters
Pixels
Python's character set has letters (A–Z, a–z), digits (0–9), special symbols and whitespaces.
Which of the following are whitespace characters?
A–Z and a–z
Blank space, tab, newline, carriage return
0–9
+, -, *, /
Whitespaces are characters that create space in code: blank space, tab, newline and carriage return.
What is a token in Python?
The smallest individual unit in a program
A complete line of code
A password for the interpreter
A type of loop
A token (lexical unit) is the smallest individual unit in a Python program; the interpreter breaks each line into tokens.
Which list correctly gives the five types of Python tokens?
Variables, Functions, Loops, Lists, Strings
Letters, Digits, Symbols, Spaces, Tabs
Keywords, Identifiers, Literals, Operators, Punctuators
int, float, str, bool, None
Python tokens are keywords, identifiers, literals, operators and punctuators.
What are keywords in Python?
Comments in the program
Values written directly in code
Names chosen by the programmer
Reserved words with a special meaning to the interpreter
Keywords are reserved words with special meaning; they cannot be used as identifiers.
How many keywords does Python 3 have (as per the notes)?
40
35
30
33
Python 3 has 35 keywords.
Which Python keywords begin with a capital letter?
True, False, None
Def, Class, Return
If, Else, For
And, Or, Not
All keywords are lowercase except True, False and None.
Which of the following is a keyword?
len
input
elif
elif is a keyword. print, input and len are built-in functions, not keywords.
A student says 'print is a keyword'. What is the correct understanding?
print is an operator
Correct — it cannot be used as a name
print is a literal
print is a built-in function, not a keyword
print, input, len and range are built-in functions, not keywords (though they should not be used as variable names).
What is an identifier?
A symbol like + or -
A name given to entities such as variables, functions, classes and modules
A reserved word
A numeric value
An identifier is the name given to program entities like variables, functions, classes and modules.
Which of the following is a valid identifier?
_fruit
for
fruit#1
2fruit
_fruit starts with an underscore and has only letters. 2fruit starts with a digit, for is a keyword, and fruit#1 contains a special symbol.
Why is 1name an invalid identifier?
It contains a keyword
It has uppercase letters
It starts with a digit
It is too short
An identifier cannot start with a digit.
Why is 'my name' an invalid identifier?
It starts with a digit
It is too long
It is a keyword
It contains a space
Identifiers cannot contain spaces.
Which identifier is invalid because it contains a special symbol?
_total
na@me
student1
Marks
na@me contains the @ symbol, which is not allowed in an identifier.
Why is 'class' not allowed as a variable name?
It is a keyword
It has five letters
It contains a digit
It starts with a lowercase letter
class is a keyword, and keywords cannot be used as identifiers.
Which statement about identifiers is correct?
They must be under 8 characters
They must start with a digit
They can be of any length and are case-sensitive
They are not case-sensitive
Identifiers can be of any length and are case-sensitive (age and Age are different).
Which set contains ONLY valid identifiers?
total marks, sum, x
age, 1st, _name
if, x1, _y
age, _name, student1
age, _name and student1 all start with a letter or underscore and contain only letters/digits/underscores. 1st starts with a digit, 'total marks' has a space, and if is a keyword.
What is a literal?
A variable name
A raw data value written directly in the source code
A reserved word
An error message
A literal is a raw data value written directly in the code, such as 10, 3.14 or 'Hello'.
Which of these is a complex number literal?
3+5j
3.14
'3+5'
0b101
Numeric literals include integers (10), floats (3.14) and complex numbers (3+5j).
Which are the Boolean literals in Python?
1 and 0 only
TRUE and FALSE
True and False
true and false
Boolean literals have a capital first letter: True and False.
Which special literal represents the absence of a value?
Empty
Null
0
None
None is Python's special literal representing the absence of a value.
Which quotes can be used to write a multi-line string literal?
Backticks
Triple quotes ''' '''
Angle brackets < >
Single quotes ' '
Triple quotes ('''...''') allow multi-line strings; single or double quotes are used for ordinary strings.
Which prefix marks a binary integer literal?
0b
0o
0x
0d
Binary literals use 0b or 0B, octal uses 0o or 0O, and hexadecimal uses 0x or 0X.
What is the decimal value of the literal 0o17?
23
17
15
7
0o17 is octal: 1×8 + 7 = 15.
What is the decimal value of the literal 0x1A?
1
110
16
26
0x1A is hexadecimal: 1×16 + 10 = 26.
What is the decimal value of the literal 0b1010?
1010
10
12
8
0b1010 is binary: 8 + 2 = 10.
How does Python treat a single character such as 'A'?
As a string of length 1
As a Boolean
As a separate char data type
As an integer
Python has no separate character type; 'A' is a string of length 1.
The values that an operator acts on are called:
Keywords
Punctuators
Operands
Literals only
Operators carry out computations on values called operands.
Which group of operators compares two values?
Arithmetic
Membership
Assignment
Relational
Relational operators (==, !=, >, <, >=, <=) compare two values.
The operators 'in' and 'not in' belong to which category?
Identity
Membership
Logical
Relational
in and not in are membership operators that test membership in a sequence.
The operators 'is' and 'is not' belong to which category?
Identity
Assignment
Membership
Arithmetic
is and is not are identity operators that compare memory identity.
Which of these is a logical operator?
//
==
and
+=
and, or and not are the logical operators.
What is the result of 7 // 2 in Python?
3.5
1
4
3
// is floor division and gives the floor (integer part): 3.
What is the main role of punctuators?
Storing values
Structuring the program — grouping, separating and terminating
Arithmetic calculation
Naming variables
Punctuators organise program structure (grouping, separating, terminating), e.g. ( ) [ ] { } , : .
How does Python define blocks of code?
With indentation (whitespace)
With semicolons
With BEGIN and END
With curly braces { }
Python uses indentation instead of { }; it is mandatory and must be consistent.
Mixing tabs and spaces inconsistently for indentation causes:
NameError
TypeError
IndentationError
No error
Inconsistent indentation causes an IndentationError; use 4 spaces per level consistently.
Which symbol starts a single-line comment in Python?
--
/*
//
#
Single-line comments start with #; multi-line comments/docstrings use ''' ... '''.
What is a variable in Python?
A type of operator
A named location in memory used to store a value that can change
A fixed value that never changes
A keyword
A variable is a named memory location whose value can change during program execution.
When is a variable created in Python?
The moment a value is assigned to it
When it is declared with its type
Only inside functions
When the program ends
Python has no separate declaration statement; a variable is created when you assign a value to it.
What does a = b = c = 10 do?
Raises an error
Assigns 10 to a, b and c
Assigns 10 to c only
Compares a, b and c with 10
Chained assignment gives the same value, 10, to all three variables.
After a, b, c = 10, 20, 30, what is the value of b?
30
10
(10, 20, 30)
20
Values are assigned in order: a = 10, b = 20, c = 30.
Which statement swaps a and b without a third variable?
a == b
a, b = b, a
a = b; b = a
swap(a, b)
a, b = b, a swaps the two values in one line.
What does the id() function show?
The unique memory address identifier of an object
The data type of a value
The name of the variable
The length of a string
id() gives the unique identifier (memory address) of the object; type() gives its type.
What does print(type(10)) display?
<class 'float'>
int(10)
<class 'int'>
10
type() shows the class of the value; 10 is an int.
What will type(True) return?
<class 'int'>
True
<class 'str'>
<class 'bool'>
True is a Boolean literal, so its type is bool.
Python is 'dynamically typed'. This means:
Types must be declared before use
A variable's type is decided at runtime from the value assigned and can change on reassignment
Variables have no data types
Only numbers can be stored
In a dynamically typed language, the type comes from the assigned value at runtime and can change when the variable is reassigned.
After running x = 10, then x = "Hello", what is the type of x?
str
float
Error — x cannot change type
int
Python is dynamically typed, so x now refers to the string "Hello".
Why does "5" + 5 cause an error even though Python is dynamically typed?
5 is not a valid literal
The + operator does not exist
Python is also strongly typed and will not add a str to an int
Dynamic typing means there are no types
Dynamic typing does not mean 'no types'; Python will not mix str and int in +.
What type of value does input() always return?
float
int
Depends on what the user types
str
input() always returns a string, even if the user types a number.
Which statement correctly reads an integer age from the user?
int age = input()
age = int(input("Enter age: "))
age = input("Enter age: ") + 0
age = input(int("Enter age: "))
Wrap input() in int() to convert the string to an integer.
What happens if you forget to type-cast input() before doing arithmetic with a number?
A TypeError (unsupported operand types) occurs
The program prints nothing
The result is rounded
Python converts it automatically
input() returns a str; adding it to an int gives TypeError: unsupported operand type(s).
What is the default value of the sep parameter in print()?
A comma
An empty string
A single space ' '
A newline
sep defaults to a single space; end defaults to a newline '\n'.
What is the output of print("Hello", "World", sep="-", end="!\n")?
Hello - World !
Hello-World-!
Hello World!
Hello-World!
sep='-' puts a dash between the items, and end='!\n' adds ! at the end: Hello-World!
What is the output of this code?
a, b, c = 5, 10, 15 print(a, b, c, sep="*", end="#")
510*15#
51015#
51015*#
5 10 15#
The values are joined by * and the line ends with # instead of a newline: 51015#.
What does this code print?
name = "Amit" marks = 95 print(f"{name} scored {marks} marks")
Amit scored 95 marks
name scored marks marks
Error
{name} scored {marks} marks
In an f-string, the values of the variables in braces are placed into the text.
In this code, what error does the first line contain?
1name = input(Enter name)
Only the variable name is invalid
The prompt is not in quotes, but the variable name is valid
The variable name starts with a digit, and the prompt is not in quotes
There is no error
1name is an invalid identifier because it starts with a digit, and the prompt text must be in quotes: name = input("Enter name").