BARU
Ukuran huruf
Lembar kerjaExploring Python Fundamentals
Total soal: 60
Worksheet time: 30mins
How do you read input from the user in Python?
Use the 'fetch()' function to obtain user input.
Use the 'read()' function to get user input.
Use the 'input()' function to read input from the user.
Use the 'scan()' function to read input from the user.
What is the purpose of the 'print()' function in Python?
To create a new variable in memory.
To execute a loop in a program.
To output data to the console.
To read data from a file.
Explain the difference between 'if', 'elif', and 'else' statements.
'if' and 'else' are the same, while 'elif' is used for functions.
'if' checks a condition, 'elif' checks additional conditions, and 'else' executes if all previous conditions are false.
'if' always executes, 'elif' is for error handling, and 'else' checks for true conditions.
'if' is used for loops, 'elif' is a synonym for 'if', and 'else' is optional.
How does a 'for' loop differ from a 'while' loop in Python?
A 'for' loop is used for iterating over a sequence, while a 'while' loop continues based on a condition.
A 'for' loop requires a condition to start, while a 'while' loop does not need any condition.
A 'for' loop is only used for numeric ranges, while a 'while' loop can iterate over any data type.
A 'for' loop runs indefinitely, while a 'while' loop stops after a set number of iterations.
What does the 'break' statement do in a loop?
The 'break' statement exits a loop.
The 'break' statement restarts the loop from the beginning.
The 'break' statement skips the current iteration of a loop.
The 'break' statement pauses a loop.
How can you skip an iteration in a loop using 'continue'?
Placing 'continue' outside any loop will skip iterations.
You can use 'break' to skip an iteration in a loop.
Using 'continue' without any condition will always skip the iteration.
You can skip an iteration in a loop using 'continue' by placing it inside a conditional statement that determines when to skip.
What is the purpose of the 'pass' statement in Python?
The 'pass' statement is used to define a function.
The 'pass' statement terminates a loop immediately.
The 'pass' statement is used for error handling in Python.
The 'pass' statement serves as a placeholder in Python.
How do you define a function in Python?
def function_name(parameters):
create function_name(parameters)
function_name(parameters) =>
function_name: parameters()
What is the difference between parameters and return values in functions?
Parameters are used to define the function; return values are used to call the function.
Parameters are outputs from a function; return values are inputs to a function.
Parameters are inputs to a function; return values are outputs from a function.
Parameters are optional values; return values are mandatory values.
What is variable scope and how does it work in Python?
There are only two types of variable scope in Python.
Variable scope only applies to global variables.
Variable scope is irrelevant in Python programming.
Variable scope in Python defines where a variable can be accessed and its lifetime, with four types: local, enclosing, global, and built-in.
Can you explain the concept of recursion with an example?
Recursion is a method of sorting data in arrays.
Recursion is a technique used to optimize database queries.
Recursion is a way to iterate through a list using loops.
Recursion is a function that calls itself to solve smaller instances of a problem.
What are some common string operations in Python?
Sorting, merging, trimming
Encoding, decoding, hashing
Parsing, validating, formatting
Concatenation, repetition, slicing, finding, replacing, uppercasing, and property checks.
How do you create and manipulate lists in Python?
You cannot sort a list in Python.
Lists can only be manipulated using the list() function.
To create a list in Python, use square brackets. To manipulate, use methods like append(), remove(), and sort().
Use curly braces to create a list.
What is list slicing and how is it used?
List slicing allows you to add elements to the end of a list.
List slicing is a method to extract a portion of a list using the syntax list[start:end:step].
List slicing is used to merge two lists into one.
List slicing is a method to sort a list in ascending order.
What is a tuple and how does it differ from a list?
A tuple is a mutable unordered collection, while a list is an immutable ordered collection.
A tuple can be modified after creation, while a list cannot.
A tuple is a collection of unique elements, while a list can contain duplicates.
A tuple is an immutable ordered collection, while a list is a mutable ordered collection.
How do sets ensure uniqueness of elements in Python?
Sets use a list structure to store elements.
Sets require elements to be of the same data type.
Sets ensure uniqueness by using a hash table to prevent duplicate elements.
Sets allow duplicate elements by default.
What are dictionaries and how do you create key-value pairs?
Dictionaries are only used for storing lists.
Dictionaries can only contain numeric keys.
Key-value pairs are created using square brackets, e.g., ['key', 'value'].
Dictionaries are collections of key-value pairs created using curly braces, e.g., {'key': 'value'}.
How do you read from a file in Python?
Use read('filename') to access the file directly.
Use open('filename', 'r') to open the file and read its contents.
Use readlines('filename') to get the file contents.
Open the file with open('filename', 'w') to read it.
What are the different file modes available in Python?
'read', 'write', 'append'
'z', 'p', 'q'
The different file modes available in Python are: 'r', 'w', 'a', 'b', 'x', 't'.
'r+', 'w+', 'a+', 'c'
What is the purpose of the 'len()' function in Python?
To determine the length of a string or collection.
To create a new list from an existing one.
To convert a string to an integer.
To check if a variable is defined.
How do you handle exceptions in Python?
Using 'catch' statements to manage exceptions.
Using the 'try' and 'except' blocks to catch and handle exceptions.
By using 'if' statements to check for errors.
Exceptions cannot be handled in Python.
What is the difference between a shallow copy and a deep copy in Python?
A deep copy is only applicable to lists, while a shallow copy can be used for all data types.
A shallow copy is faster than a deep copy.
A shallow copy creates a new object but inserts references into it, while a deep copy creates a new object and recursively adds copies of nested objects.
There is no difference; both terms refer to the same process.
What is the purpose of the 'return' statement in a function?
To create a loop within a function.
To print output to the console from within a function.
To exit a function and return control to the calling function.
To define a function's parameters.
What is the difference between a list and a set in Python?
A list can only contain numeric values, while a set can contain any data type.
A list is immutable, while a set is mutable.
A list is ordered and allows duplicates, while a set is unordered and does not allow duplicates.
A list is created using curly braces, while a set is created using square brackets.
How can you concatenate two strings in Python?
Using the 'append()' function to add strings together.
Using the 'concat()' method to merge strings.
Using the '+' operator to join two strings.
Using the 'join()' method to concatenate strings.
