PCEP - Python Certified Entry Level Flashcards
What is the difference between a compiler and an interpreter ?
An interpreter will execute code whereas a compiler just translates code from one type to another.
How do you write a tab escape character?
A tab escape character is written as \t
How do you write a new line escape character?
A new line escape character is written as /n
What is Floor Division and what is the symbol to use it?
Floor division //, is a mathematical operation that is used to divide two numbers and obtain the quotient, rounding down to the nearest whole number if necessary.
What is Remainder or Mod?
Mod or % will give you the remainder, eg 5 % 3 = 2. This can be used to determine even or odd numbers by using Mod 2, eg 12 % 2 = 0 and 13 % 2 = 1
How do you calculate the exponent of a number?
Exponents are calculated using the ** notation
What happens when you compare two objects with AND?
When you compare two objects with AND it always returns the last TRUE or the first FALSE
How do you concatenate two strings together?
You concatenate two strings together using +
How do you concatenate a string to an existing string?
You can use the +=
operator to concatenate a string to an existing string variable
str1 = “Hello”
str1 += “ World”
# str1 will be “Hello World”
How do you concatenate a list of strings by specifying a separator?
Use str.join() to concatenate strings using a separator.
words = [“Hello”, “World”]
separators = “ “
result = separator.join(words)
# result will be “Hello World”
How can you use print() to concatenate strings?
str1 = “Hello”
str2 = “World”
print(str1, str2)
# Output will be “Hello World”
What is string repetition?
String repetition is the concatenate of multiple copies of a string by using the repetition operator *
.
str1 = “Hello”
repeated_str = str1 * 3
# repeated_str will be “HelloHelloHello”
What is the format of string slicing?
String slicing is in the format of [start, stop, step]
How can you use slicing to reverse a string?
You can reverse a string - str_test[::-1]
What do you use to add an item to the end of a list?
You can add elements to the end of a list using the append()
method. For example:
my_list = [1, 2, 3] my_list.append(4) # my_list is now [1, 2, 3, 4]
How can you extend one list with the contents of another?
You can extend a list with the elements of another iterable using the extend()
method or the +=
operator. For example:
my_list = [1, 2, 3] my_list.extend([4, 5]) # my_list is now [1, 2, 3, 4, 5]
How can you update a specific element in a list?
You can update the value of an element at a specific index by assigning a new value directly to that index. For example:
my_list = [1, 2, 3] my_list[1] = 4 # my_list is now [1, 4, 3]
How can you insert an element at a specific point in a list?
You can insert an element at a specific index using the insert()
method. For example:
my_list = [1, 2, 3] my_list.insert(1, 4) # my_list is now [1, 4, 2, 3]
What are the ways of removing an element from a list?
To remove an element from a list you can use remove, pop or del
How can you use remove() to remove an element in a list?
You give the specific value to remove and the first occurrence of the value will be removed.
my_list = [1, 2, 3, 2]
my_list.remove(2)
# my_list is now [1, 3, 2]
How can you use pop() to remove an element in a list?
Pop is used to remove an element at a specific index.
my_list = [1, 2, 3]
popped_element = my_list.pop(1)
# my_list is now [1, 3] and popped_element is 2
How can you use del() to remove an element in a list?
Like pop(), del can be used to remove an element by index, or it can clear the entire list
my_list = [1, 2, 3] del my_list[1] # my_list is now [1, 3] del my_list[:] # my_list is now []
How can slicing be used to update multiple elements at once?
my_list = [1, 2, 3, 4, 5]
my_list[1:4] = [10, 20, 30]
# my_list is now [1, 10, 20, 30, 5]
What are list comprehensions used for?
List comprehensions are a way to create a new list by applying an expression to each item in an existing list.
How are list comprehensions written?
List comprehensions are written:
[expression for item in iterable if condition]
my_list = [1, 2, 3] updated_list = [x * 2 for x in my_list] # updated_list is [2, 4, 6]
What is the difference between a list and a tuple?
Tuples are immutable meaning they cannot be changed as a list can.
How is a tuple denoted?
A tuple is written - my_tuple = (1, 2, 3)
Can lists or tuples contain multiple data types?
Both lists and tuples can contain multiple data types :
mixed_tuple = (1, “hello”, 3.14, True)
mixed_list = [1, “hello”, 3.14, True]
How do you concatenate two tuples?
You concatenate two tuples using the + sign
tuple1 = (1, 2) tuple2 = (3, 4) concatenated_tuple = tuple1 + tuple2 # Results in (1, 2, 3, 4)
What is tuple repetition?
my_tuple = (1, 2)
repeated_tuple = my_tuple * 3 # Results in (1, 2, 1, 2, 1, 2)
What are the two key tuple methods?
The two key tuple methods are count() and index()
my_tuple = (1, 2, 2, 3, 2) count = my_tuple.count(2) # Returns 3 my_tuple = (1, 2, 3, 4, 5) index = my_tuple.index(3) # Returns 2
What is tuple unpacking?
Tuple unpacking is the assigning of tuple elements to variables in a single line
my_tuple = (1, 2, 3) a, b, c = my_tuple # a=1, b=2, c=3
How can you check if an element exists in a tuple?
You can check if an element exists in a tuple using the in
operator.
my_tuple = (1, 2, 3) exists = 2 in my_tuple # Returns True
How can you check the length of a tuple?
You can find the length (number of elements) of a tuple using the len()
function.
my_tuple = (1, 2, 3) length = len(my_tuple) # Returns 3
How do you return a list of all the keys/values in a dictionary?
You can use the following:
my_dict = {‘name’: ‘Alice’, ‘age’: 30, ‘city’: ‘New York’}
keys = my_dict.keys()
values = my_dict.values()
How do you return both key and values as a list of key-value pairs?
The following will return a tuple of all key/value pairs:
items = my_dict.items()
How do you return the value associated with a given key if it exists, otherwise return a default value?
To return a value for a key else if it doesn’t exist return a default you do the following:
my_dict = {‘name’: ‘Alice’, ‘age’: 30, ‘city’: ‘New York’}
age = my_dict.get(‘age’, 0) # Returns 30
salary = my_dict.get(‘salary’, 0) # Returns 0 (default value)
What is the .update() method used to do with dictionaries?
The update() method updates the dictionary with the key-value pairs from another dictionary.
my_dict = {‘name’: ‘Alice’, ‘age’: 30, ‘city’: ‘New York’}
other_dict = {‘city’: ‘San Francisco’, ‘gender’: ‘Female’}
my_dict.update(other_dict)
my_dict = {‘name’: ‘Alice’, ‘age’: 30, ‘city’: ‘San Francisco’, ‘gender’: ‘Female’}
What does the dictionary pop() method do?
The pop() method removes the key-pair associated with the specified key and returns the value.
What does the popitem() method do and what is it used for?
The popitem() method is used to removes and returns an arbitrary key-value pair as a tuple.
How do you remove all items in a dictionary?
To remove all items in a dictionary you use:
my_dict.clear()
How do you return the number of key-value pairs in a dictionary?
To return the number of key-value pairs in a dictionary you use:
num_items = len(my_dict)
How do you find a specific key in a dictionary?
To find a specific key in a dictionary you would use :
if ‘name’ in my_dict:
print(‘Name is in the dictionary.’)
What is a shallow copy of a dictionary and how do you create one?
A shallow copy is one who references the original. So any changes to the original or the shallow copy will be reflected in both.
new_dict = my_dict.copy()
How do you return a sorted list of keys from your dictionary?
To return a sorted list of keys you use the following:
sorted_keys = sorted(my_dict)
How do you concatenate two strings together?
You use the + method to concatenate together two strings
How do you get elements from a list and write it to a string?
You use the join() method to write list elements to a string.
words = [‘Hello’, ‘world’, ‘how’, ‘are’, ‘you?’]
sentence = ‘ ‘.join(words)
print(sentence)
Hello world how are you?
How do you return the length of a string?
You use the Len() method to return the length of a string.
What are the methods available for string case conversion?
The string case conversion methods are:
-
str.upper()
: Converts the string to uppercase. -
str.lower()
: Converts the string to lowercase. -
str.capitalize()
: Capitalizes the first character. -
str.title()
: Capitalizes the first character of each word.
What are the methods available for string trimming and stripping?
The string trimming and stripping methods are:
-
str.strip()
: Removes leading and trailing whitespace. -
str.lstrip()
: Removes leading whitespace. -
str.rstrip()
: Removes trailing whitespace.
What are the methods available for string searching?
-
str.find(substring)
: Returns the index of the first occurrence of a substring or -1 if not found. -
str.index(substring)
: Similar tofind()
but raises an error if not found. -
str.count(substring)
: Returns the number of non-overlapping occurrences of a substring.
What is the method to find the first occurrence of a substring?
str.find(substring)`: Returns the index of the first occurrence of a substring or -1 if not found
What is the difference between str.find(substring) and str.index(substring)?
If the substring is not found, str.find returns -1 but str.index raises and error
How do you return the number of non-overlapping occurrences of a substring?
To return the number of non-overlapping occurrences of a substring you use:
str.count(substring)
How do you replace all occurrences of old
with new
in the string?
To replace old with new in a string you use:
str.replace(old, new)
How do you split a string using a delimiter?
To split a string using a delimiter you use:
str.split(delimiter)
How do you split a string at line breaks?
To split a string at line breaks you use:
str.splitlines()
eg
multi_line_string = “Hello\nWorld\nHow\nAre\nYou”
[‘Hello’, ‘World’, ‘How’, ‘Are’, ‘You’]
How do you test if all characters in a string are alphabetic?
To test if all characters are alphabetical you use:
str.isaplha()
How do you test if all characters are digits?
To test if all characters are digits you use:
str.isdigit()
How do you test if all characters are alphanumeric?
To test if all characters are alphanumeric you use:
str.isalnum()
How do you test if all characters are whitespace?
To test if all characters are whitespace you use:
str.isspace()
How do you get a slice from a string?
To get a slice from a string you use:
[start:end]
How do you convert an object to a string?
To convert an object to a string you use:
str[object]
What is a For Loop used for?
A For Loop is used for iterating over a sequence (such as a list, tuple, string, or range) or any iterable object.
What is a While Loop used for?
A While Loop is used for repeatedly executing a block of code as long as a specified condition is true.
How do you exit a loop prematurely when a condition is met?
To exit a loop early when a condition is met you use break()
What is continue() used for in the context of loops?
Continue() is used to skip the rest of the current iteration and continue with the next iteration.
How do you loop with an index?
You can use the enumerate()
function to iterate over elements of an iterable along with their indices.
- Example:for index, item in enumerate(iterable):
This gives access to both the index and the item
What is List Comprehension used for?
List comprehension is a concise and efficient way to create lists in Python.
List Comprehension allows you to generate a new list by applying an expression to each item in an existing iterable (such as a list, tuple, or range) and optionally filtering those items based on a condition.
What is an example of a List Comprehension?
new_list = [expression for item in iterable if condition]
~~~
squares_of_evens = [x**2 for x in range(10) if x % 2 == 0]
-
expression
: The expression that is applied to each item in the iterable to generate the new value. -
item
: A variable that represents each item in the iterable. -
iterable
: The existing iterable you want to iterate over. -
condition
(optional): A condition that filters the items from the iterable. The item is included in the new list only if the condition evaluates toTrue
.
What is the difference between a Generator and a standard iterable?
Generators in Python are a type of iterable, similar to lists or tuples, but they are not stored in memory all at once. Instead, they produce values on-the-fly as you iterate over them. This makes generators memory-efficient and well-suited for dealing with large datasets or infinite sequences.
How are Generators defined?
Generators are defined using functions with the yield
keyword. When a function contains a yield
statement, it becomes a generator function. When you call this function, it returns a generator object but doesn’t start executing the function immediately. Instead, it begins executing when you iterate over it.