POC Core Python Programming Concepts 1 — Questions and Answers
Question 1: What is the correct way to create a variable in Python?
- int x = 10
- x := 10
- x = 10 (Correct answer)
- define x 10
Correct answer: x = 10
In Python, variables are created simply by assigning a value to a name using the assignment operator `=`. Python is dynamically typed, meaning you don't need to declare the variable's type explicitly. The interpreter infers the type based on the assigned value.
Question 2: What is the output of: print(type(3.14))?
- <class 'int'>
- <class 'float'> (Correct answer)
- <class 'str'>
- <class 'bool'>
Correct answer: <class 'float'>
The `type()` function in Python returns the type of the object passed to it. The number `3.14` contains a decimal point, which Python recognizes as a floating-point number. Therefore, `print(type(3.14))` outputs `<class 'float'>`, indicating its data type.
Question 3: Which keyword is used to define a function in Python?
- func
- function
- define
- def (Correct answer)
Correct answer: def
In Python, the `def` keyword is used to define a function. It stands for "define" and is followed by the function name, parentheses for parameters, and a colon, signaling the start of the function's code block. This keyword is fundamental for creating reusable blocks of code.
Question 4: Which of the following is a list in Python?
- {1, 2, 3}
- [1, 2, 3] (Correct answer)
- (1, 2, 3)
- <1, 2, 3>
Correct answer: [1, 2, 3]
In Python, a list is a mutable, ordered sequence of elements enclosed in square brackets `[]`. It can hold items of different data types and is one of the most versatile collection types. Options A, C, and D represent a set, a tuple, and an invalid syntax respectively.
Question 5: What does the 'len()' function do?
- It checks the data type.
- It creates a list.
- It returns the length of an object (Correct answer)
- It converts strings to integers.
Correct answer: It returns the length of an object
The built-in `len()` function in Python is used to determine the number of items in an object. It can be applied to various sequence types like strings, lists, tuples, dictionaries, and sets, returning the count of their respective elements. This is useful for checking the size of collections.
Question 6: Which operator is used for exponentiation in Python?
- ^
- ** (Correct answer)
- exp
- //
Correct answer: **
In Python, the double asterisk `**` operator is used for exponentiation, meaning "to the power of." For example, `2 ** 3` would calculate 2 raised to the power of 3, resulting in 8. This operator provides a concise way to perform power calculations.
Question 7: Which loop is used to iterate over a sequence in Python?
- loop
- for (Correct answer)
- repeat
- while-do
Correct answer: for
The `for` loop in Python is specifically designed to iterate over the elements of a sequence (like a list, tuple, string, or range) or other iterable objects. It executes a block of code for each item in the sequence, making it ideal for processing collections of data.
Question 8: What does the 'break' keyword do in a loop?
- Repeats the loop.
- Skips current iteration.
- Exits the loop (Correct answer)
- Restarts the loop.
Correct answer: Exits the loop
The `break` keyword in Python is used to terminate the current loop prematurely. When `break` is encountered inside a `for` or `while` loop, the loop immediately stops, and program execution continues with the statement immediately following the loop. It's often used to exit a loop based on a specific condition.
Question 9: Which data type is used to store True or False values?
- int
- bool (Correct answer)
- str
- char
Correct answer: bool
The `bool` (Boolean) data type in Python is used to represent logical values, which can only be `True` or `False`. These values are fundamental for conditional statements and logical operations, controlling program flow based on truthiness. They are essential for decision-making in programs.
What is the correct way to create a variable in Python?