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]