wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Exploring Python Basics and Libraries

Total questions: 25

Worksheet time: 13mins

Name
Class
Date
1.

What is the difference between a list and a tuple in Python?

a)

Lists are faster than tuples in all operations.

b)

The main difference is that lists are mutable and tuples are immutable.

c)

Lists can contain only numbers while tuples can contain any data type.

d)

Tuples are created using square brackets while lists use parentheses.

2.

How do you create a set in Python?

a)

You must use the dict() function to create a set

b)

Sets can only be created with the list() function

c)

You can create a set using square brackets []

d)

You can create a set in Python using curly braces {} or the set() function.

3.

What method would you use to add an item to a dictionary?

a)

Use add(key, value) method.

b)

Append item to dictionary using append() method.

c)

Use dict[key] = value.

d)

Set value[key] = item.

4.

Explain how to read a file in Python.

a)

Use 'with open("filename.txt") as file: content = file.read()' without specifying the mode.

b)

Read a file using 'file.read("filename.txt")'.

c)

Use 'open("filename.txt")' to read a file in Python.

d)

Use 'with open("filename.txt", "r") as file: content = file.read()' to read a file in Python.

5.

What is the purpose of the 'with' statement when working with files?

a)

The 'with' statement ensures proper management of file resources by automatically closing the file after its block of code is executed.

b)

The 'with' statement is used to read files line by line.

c)

The 'with' statement allows for multiple files to be opened simultaneously.

d)

The 'with' statement is a way to create new files without specifying a mode.

6.

How can you convert a list to a set?

a)

Apply convert(your_list) to change a list into a set.

b)

Use array(your_list) to transform a list into an array.

c)

Use list(your_set) to convert a set to a list.

d)

Use set(your_list) to convert a list to a set.

7.

What is the output of the following code: len((1, 2, 3))?

a)

3.5

b)

4

c)

3

d)

2

8.

How do you access the first element of a list named 'my_list'?

a)

get(my_list, 0)

b)

my_list[1]

c)

my_list[0]

d)

my_list.first()

9.

What function would you use to find the maximum value in a list?

a)

maximumValue()

b)

max()

c)

getHighest()

d)

findMax()

10.

How do you create a DataFrame in Pandas?

a)

DataFrames can only be created from CSV files in Pandas.

b)

You can create a DataFrame using pd.DataFrame.from_records(data).

c)

Use pd.createDataFrame(data) for creating a DataFrame.

d)

Use pd.DataFrame(data) where data can be a dictionary, list, or NumPy array.

11.

What is the purpose of NumPy in Python?

a)

To create graphical user interfaces in Python.

b)

To manage databases and data storage.

c)

To enhance web development capabilities in Python.

d)

The purpose of NumPy in Python is to facilitate numerical computing with powerful array and matrix operations.

12.

How can you filter rows in a Pandas DataFrame?

a)

Sort the DataFrame by a specific column

b)

Apply a for loop to iterate through rows

c)

Use boolean indexing or the `query()` method.

d)

Use the `filter()` function

13.

What is the difference between 'append()' and 'extend()' methods in lists?

a)

'append()' adds multiple items, 'extend()' adds one item from an iterable.

b)

'append()' modifies the original list, 'extend()' creates a new list.

c)

'append()' can only be used with strings, 'extend()' can be used with any data type.

d)

'append()' adds one item, 'extend()' adds multiple items from an iterable.

14.

How do you check if a key exists in a dictionary?

a)

Use 'key in dictionary' to check for key existence.

b)

Use 'key.exists()' to verify key presence.

c)

Use 'dictionary.key' to access the value directly.

d)

Check if 'dictionary.get(key)' returns None.

15.

What is the output of the following code: {1, 2, 2, 3}?

a)

{1, 2, 2}

b)

{1, 2, 3}

c)

{2, 3}

d)

{1, 2, 2, 3, 4}

16.

What is the output of the following code: {1, 2, 3, 3}?

a)

{2, 3}

b)

{1, 2, 3, 4}

c)

{1, 2, 3, 3}

d)

{1, 2, 3}

17.

What method would you use to remove an item from a dictionary?

a)

Use dictionary.delete(key) to remove an item.

b)

Use remove(key) method.

c)

Use del dictionary[key] to remove an item.

d)

Use dictionary.pop(key) to delete an item.

18.

How do you remove duplicates from a list in Python?

a)

Apply a for loop to create a new list without duplicates.

b)

Use the remove_duplicates() method on the list.

c)

Convert the list to a set and back to a list: list(set(your_list)).

d)

Use the unique() function to filter duplicates.

19.

What method would you use to read a file line by line in Python?

a)

Use 'for line in file:' to iterate through each line.

b)

Use 'file.read_line()' to read a single line.

c)

Use 'file.readlines()' to read all lines at once.

d)

Use 'file.get_line()' to access a specific line.

20.

How do you create a dictionary in Python?

a)

Use new_dict = [] to initialize a dictionary.

b)

Use dict() function to create a dictionary.

c)

Use curly braces {} with key-value pairs.

d)

Use list() to create a dictionary.

21.

What is the output of the following code: len([1, 2, 3, 4])?

a)

4

b)

3

c)

2

d)

5

22.

What is the primary use of the Pandas library in Python?

a)

To perform web scraping and data extraction.

b)

The primary use of the Pandas library in Python is to provide data structures and functions needed to manipulate structured data.

c)

To create machine learning models.

d)

To facilitate data manipulation and analysis.

23.

How do you concatenate two DataFrames in Pandas?

a)

Use the append() method on one DataFrame.

b)

Use pd.concat([df1, df2]) to concatenate two DataFrames.

c)

Use the concat() function from the NumPy library.

d)

Use the merge() function to combine DataFrames.

24.

What is the output of the following code: [1, 2, 3] + [4, 5]?

a)

[1, 2, 3, 4, 5, 6]

b)

[1, 2, 3, 4, 5]

c)

[5, 6, 7]

d)

Error: Unsupported operation for lists.

25.

What is the output of the following code: [5, 10, 15] * 2?

a)

Error: Unsupported operation for lists.

b)

[5, 10, 15, 5, 10, 15]

c)

[10, 20, 30]

d)

[5, 10, 15, 5, 10]