PCEP - Python Certified Entry Level Flashcards

1
Q

What is the difference between a compiler and an interpreter ?

A

An interpreter will execute code whereas a compiler just translates code from one type to another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you write a tab escape character?

A

A tab escape character is written as \t

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you write a new line escape character?

A

A new line escape character is written as /n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Floor Division and what is the symbol to use it?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Remainder or Mod?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you calculate the exponent of a number?

A

Exponents are calculated using the ** notation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens when you compare two objects with AND?

A

When you compare two objects with AND it always returns the last TRUE or the first FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you concatenate two strings together?

A

You concatenate two strings together using +

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you concatenate a string to an existing string?

A

You can use the += operator to concatenate a string to an existing string variable

str1 = “Hello”
str1 += “ World”
# str1 will be “Hello World”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you concatenate a list of strings by specifying a separator?

A

Use str.join() to concatenate strings using a separator.

words = [“Hello”, “World”]
separators = “ “
result = separator.join(words)
# result will be “Hello World”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you use print() to concatenate strings?

A

str1 = “Hello”
str2 = “World”
print(str1, str2)
# Output will be “Hello World”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is string repetition?

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the format of string slicing?

A

String slicing is in the format of [start, stop, step]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you use slicing to reverse a string?

A

You can reverse a string - str_test[::-1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What do you use to add an item to the end of a list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you extend one list with the contents of another?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How can you update a specific element in a list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How can you insert an element at a specific point in a list?

A

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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are the ways of removing an element from a list?

A

To remove an element from a list you can use remove, pop or del

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How can you use remove() to remove an element in a list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How can you use pop() to remove an element in a list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How can you use del() to remove an element in a list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How can slicing be used to update multiple elements at once?

A

my_list = [1, 2, 3, 4, 5]
my_list[1:4] = [10, 20, 30]
# my_list is now [1, 10, 20, 30, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What are list comprehensions used for?

A

List comprehensions are a way to create a new list by applying an expression to each item in an existing list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How are list comprehensions written?

A

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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is the difference between a list and a tuple?

A

Tuples are immutable meaning they cannot be changed as a list can.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

How is a tuple denoted?

A

A tuple is written - my_tuple = (1, 2, 3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Can lists or tuples contain multiple data types?

A

Both lists and tuples can contain multiple data types :

mixed_tuple = (1, “hello”, 3.14, True)
mixed_list = [1, “hello”, 3.14, True]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

How do you concatenate two tuples?

A

You concatenate two tuples using the + sign

 tuple1 = (1, 2)
 tuple2 = (3, 4)
 concatenated_tuple = tuple1 + tuple2  # Results in (1, 2, 3, 4)
30
Q

What is tuple repetition?

A

my_tuple = (1, 2)
repeated_tuple = my_tuple * 3 # Results in (1, 2, 1, 2, 1, 2)

31
Q

What are the two key tuple methods?

A

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
32
Q

What is tuple unpacking?

A

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
33
Q

How can you check if an element exists in a tuple?

A

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
34
Q

How can you check the length of a tuple?

A

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
35
Q

How do you return a list of all the keys/values in a dictionary?

A

You can use the following:

my_dict = {‘name’: ‘Alice’, ‘age’: 30, ‘city’: ‘New York’}

keys = my_dict.keys()
values = my_dict.values()

36
Q

How do you return both key and values as a list of key-value pairs?

A

The following will return a tuple of all key/value pairs:

items = my_dict.items()

37
Q

How do you return the value associated with a given key if it exists, otherwise return a default value?

A

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)

38
Q

What is the .update() method used to do with dictionaries?

A

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’}

38
Q

What does the dictionary pop() method do?

A

The pop() method removes the key-pair associated with the specified key and returns the value.

39
Q

What does the popitem() method do and what is it used for?

A

The popitem() method is used to removes and returns an arbitrary key-value pair as a tuple.

40
Q

How do you remove all items in a dictionary?

A

To remove all items in a dictionary you use:

my_dict.clear()

41
Q

How do you return the number of key-value pairs in a dictionary?

A

To return the number of key-value pairs in a dictionary you use:

num_items = len(my_dict)

42
Q

How do you find a specific key in a dictionary?

A

To find a specific key in a dictionary you would use :

if ‘name’ in my_dict:
print(‘Name is in the dictionary.’)

43
Q

What is a shallow copy of a dictionary and how do you create one?

A

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()
44
Q

How do you return a sorted list of keys from your dictionary?

A

To return a sorted list of keys you use the following:

sorted_keys = sorted(my_dict)
45
Q

How do you concatenate two strings together?

A

You use the + method to concatenate together two strings

46
Q

How do you get elements from a list and write it to a string?

A

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?

47
Q

How do you return the length of a string?

A

You use the Len() method to return the length of a string.

48
Q

What are the methods available for string case conversion?

A

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.
49
Q

What are the methods available for string trimming and stripping?

A

The string trimming and stripping methods are:

  • str.strip(): Removes leading and trailing whitespace.
  • str.lstrip(): Removes leading whitespace.
  • str.rstrip(): Removes trailing whitespace.
50
Q

What are the methods available for string searching?

A
  • str.find(substring): Returns the index of the first occurrence of a substring or -1 if not found.
  • str.index(substring): Similar to find() but raises an error if not found.
  • str.count(substring): Returns the number of non-overlapping occurrences of a substring.
51
Q

What is the method to find the first occurrence of a substring?

A

str.find(substring)`: Returns the index of the first occurrence of a substring or -1 if not found

52
Q

What is the difference between str.find(substring) and str.index(substring)?

A

If the substring is not found, str.find returns -1 but str.index raises and error

53
Q

How do you return the number of non-overlapping occurrences of a substring?

A

To return the number of non-overlapping occurrences of a substring you use:

str.count(substring)

54
Q

How do you replace all occurrences of old with new in the string?

A

To replace old with new in a string you use:

str.replace(old, new)

55
Q

How do you split a string using a delimiter?

A

To split a string using a delimiter you use:

str.split(delimiter)

56
Q

How do you split a string at line breaks?

A

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’]

57
Q

How do you test if all characters in a string are alphabetic?

A

To test if all characters are alphabetical you use:

str.isaplha()

58
Q

How do you test if all characters are digits?

A

To test if all characters are digits you use:

str.isdigit()

59
Q

How do you test if all characters are alphanumeric?

A

To test if all characters are alphanumeric you use:

str.isalnum()

60
Q

How do you test if all characters are whitespace?

A

To test if all characters are whitespace you use:

str.isspace()

61
Q

How do you get a slice from a string?

A

To get a slice from a string you use:

[start:end]

62
Q

How do you convert an object to a string?

A

To convert an object to a string you use:

str[object]

63
Q

What is a For Loop used for?

A

A For Loop is used for iterating over a sequence (such as a list, tuple, string, or range) or any iterable object.

64
Q

What is a While Loop used for?

A

A While Loop is used for repeatedly executing a block of code as long as a specified condition is true.

65
Q

How do you exit a loop prematurely when a condition is met?

A

To exit a loop early when a condition is met you use break()

66
Q

What is continue() used for in the context of loops?

A

Continue() is used to skip the rest of the current iteration and continue with the next iteration.

67
Q

How do you loop with an index?

A

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

68
Q

What is List Comprehension used for?

A

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.

69
Q

What is an example of a List Comprehension?

A

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 to True.
70
Q

What is the difference between a Generator and a standard iterable?

A

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.

71
Q

How are Generators defined?

A

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.