WGU PYTHON D335 QUESTIONS AND
ANSWERS WITH COMPLETE
SOLUTIONS 100% CORRECT!!
Python Built-in Functions
Function: chr() Definition: This function converts an integer (representing a
Unicode code point) into its corresponding character string.
Example: chr(65) returns the string 'A'.
Function: len() Definition: Returns the total count of items in an object. When
used with a string, it provides the total number of characters (including spaces and
punctuation). When used with a list or set, it returns the number of elements.
The set Data Type
Definition: A set is a built-in Python data type used to store collections of data that
are unordered, unindexed, and contain only unique elements.
Unordered: Unlike a list, the items in a set do not have a defined order. You
cannot access a set item by referring to an index or a key.
Unique Elements: Sets automatically handle duplicates. If you attempt to
add an element that already exists in the set, Python simply ignores the
operation.
Creation: You define a set using curly braces {} or the set() constructor.
Manipulating Sets
Question: How do you add a new element to an existing set?
Answer: ✔️✔️ set_name.add(value) Because sets are unordered, there is no
"append" (which implies adding to the end). The .add() method simply places the
element into the collection wherever the internal hashing algorithm dictates.
Example:
Python
my_fruits = {"apple", "banana"}
my_fruits.add("cherry")
# my_fruits is now {"apple", "banana", "cherry"}
# (though the order may appear different when printed)
Quick Comparison Table
Feature List Set
Ordered Yes (has index) No
Duplicates Allowed Not Allowed
ANSWERS WITH COMPLETE
SOLUTIONS 100% CORRECT!!
Python Built-in Functions
Function: chr() Definition: This function converts an integer (representing a
Unicode code point) into its corresponding character string.
Example: chr(65) returns the string 'A'.
Function: len() Definition: Returns the total count of items in an object. When
used with a string, it provides the total number of characters (including spaces and
punctuation). When used with a list or set, it returns the number of elements.
The set Data Type
Definition: A set is a built-in Python data type used to store collections of data that
are unordered, unindexed, and contain only unique elements.
Unordered: Unlike a list, the items in a set do not have a defined order. You
cannot access a set item by referring to an index or a key.
Unique Elements: Sets automatically handle duplicates. If you attempt to
add an element that already exists in the set, Python simply ignores the
operation.
Creation: You define a set using curly braces {} or the set() constructor.
Manipulating Sets
Question: How do you add a new element to an existing set?
Answer: ✔️✔️ set_name.add(value) Because sets are unordered, there is no
"append" (which implies adding to the end). The .add() method simply places the
element into the collection wherever the internal hashing algorithm dictates.
Example:
Python
my_fruits = {"apple", "banana"}
my_fruits.add("cherry")
# my_fruits is now {"apple", "banana", "cherry"}
# (though the order may appear different when printed)
Quick Comparison Table
Feature List Set
Ordered Yes (has index) No
Duplicates Allowed Not Allowed