Online Coding Lessons Python Coding Basics 1 — Questions and Answers
Question 1: Which keyword is used to define a function in Python?
- func
- def (Correct answer)
- function
- define
Correct answer: def
In Python, the 'def' keyword is used to define a function.
Question 2: What is the correct way to print 'Hello, World!' in Python?
- echo 'Hello, World!'
- console.log('Hello, World!')
- print('Hello, World!') (Correct answer)
- System.out.println('Hello, World!')
Correct answer: print('Hello, World!')
Python uses the built-in print() function to display output to the console.
Question 3: Which data type is used to store a sequence of characters in Python?
- int
- bool
- str (Correct answer)
- list
Correct answer: str
The 'str' (string) data type in Python is used to store sequences of characters.
Question 4: What does the len() function do in Python?
- Returns the largest item
- Returns the number of items in an object (Correct answer)
- Converts a value to a number
- Sorts a list
Correct answer: Returns the number of items in an object
The len() function returns the number of items (length) in an object such as a string, list, or tuple.
Question 5: How do you create a comment in Python?
- // This is a comment
- /* This is a comment */
- # This is a comment (Correct answer)
- <!-- This is a comment -->
Correct answer: # This is a comment
In Python, comments are created using the hash (#) symbol at the start of the line.
Question 6: Which of the following correctly creates a list in Python?
- myList = (1, 2, 3)
- myList = {1, 2, 3}
- myList = [1, 2, 3] (Correct answer)
- myList = <1, 2, 3>
Correct answer: myList = [1, 2, 3]
Lists in Python are created using square brackets [] with items separated by commas.
Which keyword is used to define a function in Python?