So you have landed an interview and worked hard at upskilling your Python knowledge. There are going to be some questions about Python and the different aspects of it that you will need to be able to talk about that are not all coding!
Here we discuss some of the key elements that you should be comfortable explaining.
What are the key Features of Python?
In the below screenshot that will feature in our video, if you are asked this question they will help you be able to discuss.
Below I have outlined some of the key benefits you should be comfortable discussing.
It is great as it is open source and well-supported, you will always find an answer to your question somewhere.
Also as it is easy to code and understand, the ability to quickly upskill and deliver some good programs is a massive benefit.
As there are a lot of different platforms out there, it has been adapted to easily work on any with little effort. This is a massive boost to have it used across a number of development environments without too much tweaking.
Finally, some languages need you to compile the application first, Python does not it just runs.
What are the limitations of Python?
While there is a lot of chat about Python, it also comes with some caveats which you should be able to talk to.
One of the first things to discuss is that its speed can inhibit how well an application performs. If you require real-time data and using Python you need to consider how well performance will be inhibited by it.
There are scenarios where an application is written in an older version of code, and you want to introduce new functionality, with a newer version. This could lead to problems of the code not working that currently exists, that needs to be rewritten. As a result, additional programming time may need to be factored in to fix the compatibility issues found.
Finally, As Python uses a lot of memory you need to have it on a computer and or server that can handle the memory requests. This is especially important where the application is been used in real-time and needs to deliver output pretty quickly to the user interface.
What is Python good for?
As detailed below, there are many uses of Python, this is not an exhaustive list I may add.
A common theme for some of the points below is that Python can process data and provide information that you are not aware of which can aid decision-making.
Alternatively, it can also be used as a tool for automating and or predicting the behaviour of the subjects it pertains to, sometimes these may not be obvious, but helps speed up the delivery of certain repetitive tasks.
What are the data types Python support?
Finally below is a list of the data types you should be familiar with, and be able to discuss. Some of these are frequently used.
These come from the Python data types web page itself, so a good reference point if you need to further understand or improve your knowledge.
In our first video on python interview questions we discussed some of the high-level questions you may be asked in an interview.
In this post, we will discuss interview questions about python dictionaries.
So what are Python dictionaries and their properties?
First of all, they are mutable, meaning they can be changed, please read on to see examples.
As a result, you can add or take away key-value pairs as you see fit.
Also, key names can be changed.
One of the other properties that should be noted is that they are case-sensitive, meaning the same key name can exist if it is in different caps.
As can be seen below, the process is straightforward, you just declare a variable equal to two curly brackets, and hey presto you are up and running.
An alternative is to declare a variable equal to dict(), and in an instance, you have an empty dictionary.
The below block of code should be a good example of how to do this:
# How do you create an empty dictionary?
empty_dict1 = {}
empty_dict2 = dict()
print(empty_dict1)
print(empty_dict2)
print(type(empty_dict1))
print(type(empty_dict2))
Output:
{}
{}
<class 'dict'>
<class 'dict'>
If you want to add values to your Python dictionary, there are several ways possible, the below code, can help you get a better idea:
#How do add values to a python dictionary
empty_dict1 = {}
empty_dict2 = dict()
empty_dict1['Key1'] = '1'
empty_dict1['Key2'] = '2'
print(empty_dict1)
#Example1 - #Appending values to a python dictionary
empty_dict1.update({'key3': '3'})
print(empty_dict1)
#Example2 - Use an if statement
if "key4" not in empty_dict1:
empty_dict1["key4"] = '4'
else:
print("Key exists, so not added")
print(empty_dict1)
Output:
{'Key1': '1', 'Key2': '2'}
{'Key1': '1', 'Key2': '2', 'key3': '3'}
{'Key1': '1', 'Key2': '2', 'key3': '3', 'key4': '4'}
One of the properties of dictionaries is that they are unordered, as a result, if it is large finding what you need may take a bit.
Luckily Python has provided the ability to sort as follows:
#How to sort a python dictionary?
empty_dict1 = {}
empty_dict1['Key2'] = '2'
empty_dict1['Key1'] = '1'
empty_dict1['Key3'] = '3'
print("Your unsorted by key dictionary is:",empty_dict1)
print("Your sorted by key dictionary is:",dict(sorted(empty_dict1.items())))
#OR - use list comprehension
d = {a:b for a, b in enumerate(empty_dict1.values())}
print(d)
d["Key2"] = d.pop(0) #replaces 0 with Key2
d["Key1"] = d.pop(1) #replaces 1 with Key1
d["Key3"] = d.pop(2) #replaces 2 with Key3
print(d)
print(dict(sorted(d.items())))
Output:
Your unsorted by key dictionary is: {'Key2': '2', 'Key1': '1', 'Key3': '3'}
Your sorted by key dictionary is: {'Key1': '1', 'Key2': '2', 'Key3': '3'}
{0: '2', 1: '1', 2: '3'}
{'Key2': '2', 'Key1': '1', 'Key3': '3'}
{'Key1': '1', 'Key2': '2', 'Key3': '3'}
How do you delete a key from a Python dictionary?
From time to time certain keys may not be required anymore. In this scenario, you will need to delete them. In doing this you also delete the value associated with the key.
#How do you delete a key from a dictionary?
empty_dict1 = {}
empty_dict1['Key2'] = '2'
empty_dict1['Key1'] = '1'
empty_dict1['Key3'] = '3'
print(empty_dict1)
#1. Use the pop function
empty_dict1.pop('Key1')
print(empty_dict1)
#2. Use Del
del empty_dict1["Key2"]
print(empty_dict1)
#3. Use dict.clear()
empty_dict1.clear() # Removes everything from the dictionary.
print(empty_dict1)
Output:
{'Key2': '2', 'Key1': '1', 'Key3': '3'}
{'Key2': '2', 'Key3': '3'}
{'Key3': '3'}
{}
How do you delete more than one key from a Python dictionary?
Sometimes you may need to remove multiple keys and their values. Using the above code repeatedly may not be the most efficient way to achieve this.
To help with this Python has provided a number of ways to achieve this as follows:
#How do you delete more than one key from a dictionary
#1. Create a list to lookup against
empty_dict1 = {}
empty_dict1['Key2'] = '2'
empty_dict1['Key1'] = '1'
empty_dict1['Key3'] = '3'
empty_dict1['Key4'] = '4'
empty_dict1['Key5'] = '5'
empty_dict1['Key6'] = '6'
print(empty_dict1)
dictionary_remove = ["Key5","Key6"] # Lookup list
#1. Use the pop method
for key in dictionary_remove:
empty_dict1.pop(key)
print(empty_dict1)
#2 Use the del method
dictionary_remove = ["Key3","Key4"]
for key in dictionary_remove:
del empty_dict1[key]
print(empty_dict1)
How do you change the name of a key in a Python dictionary?
There are going to be scenarios where the key names are not the right names you need, as a result, they will need to be changed.
It should be noted that when changing the key names, the new name should not already exist.
Below are some examples that will show you the different ways this can be acheived.
# How do you change the name of a key in a dictionary
#1. Create a new key , remove the old key, but keep the old key value
# create a dictionary
European_countries = {
"Ireland": "Dublin",
"France": "Paris",
"UK": "London"
}
print(European_countries)
#1. rename key in dictionary
European_countries["United Kingdom"] = European_countries.pop("UK")
# display the dictionary
print(European_countries)
#2. Use zip to change the values
European_countries = {
"Ireland": "Dublin",
"France": "Paris",
"United Kingdom": "London"
}
update_elements=['IRE','FR','UK']
new_dict=dict(zip(update_elements,list(European_countries.values())))
print(new_dict)
Output:
{'Ireland': 'Dublin', 'France': 'Paris', 'UK': 'London'}
{'Ireland': 'Dublin', 'France': 'Paris', 'United Kingdom': 'London'}
{'IRE': 'Dublin', 'FR': 'Paris', 'UK': 'London'}
How do you get the min and max key and values in a Python dictionary?
Finally, you may have a large dictionary and need to see the boundaries and or limits of the values contained within it.
In the below code, some examples of what you can talk through should help explain your knowledge.
#How do you get the min and max keys and values in a dictionary?
dict_values = {"First": 1,"Second": 2,"Third": 3}
#1. Get the minimum value and its associated key
minimum = min(dict_values.values())
print("The minimum value is:",minimum)
minimum_key = min(dict_values.items())
print(minimum_key)
#2. Get the maximum value and its associated key
maximum = max(dict_values.values())
print("The maximum value is:",maximum)
maximum_key = max(dict_values.items())
print(maximum_key)
#3. Get the min and the max key
minimum = min(dict_values.keys())
print("The minimum key is:",minimum)
#2. Get the maximum value and its associated key
maximum = max(dict_values.keys())
print("The maximum key is:",maximum)
Output:
The minimum value is: 1
('First', 1)
The maximum value is: 3
('Third', 3)
The minimum key is: First
The maximum key is: Third
Creating empty dictionaries has many benefits as you can manipulate them as they are mutable.
Also, they can grow and shrink as you need, you just need to make sure that any new key you add is unique and not already stored in the dictionary.
Another thing to note about them is that they are unordered.
Finally, if you are adding data to a dictionary, the keys are case-sensitive, so the same key can exist in the dictionary, but it has to be different regards the case applied to it.
## How do you create an empty dictionary?
# empty_dict1 = {}
# empty_dict2 = dict()
# print(empty_dict1)
# print(empty_dict2)
# print(type(empty_dict1))
# print(type(empty_dict2))
#Use Keys,values in lists
# list_keys = []
# List_values = []
#
# n = dict(zip(list_keys, List_values))
# print(n)
# print(type(n))
# Note using Zip just allows you to iterate over two lists in parallel , and the output is a set of pairs
##Use a function ##
#
# def create_dictionary():
# d = {}
# print(d)
# print(type(d))
#
# create_dictionary()
##Use list comprehension##
# loop_list = []
# d = { i: j for i, j in enumerate(loop_list)}
# print(d)
# print(type(d))
In our Python Overview Interview Questions we started off the process of trying to prepare you how to answer any questions that may come up in an interview scenario.
How to use the pop() method to delete a key from a dictionary
In the below example we tell Python to find the key “Key1”, then when it does it prints the python dictionary without that key or its value.
empty_dict1 = {}
empty_dict1['Key2'] = '2'
empty_dict1['Key1'] = '1'
empty_dict1['Key3'] = '3'
print(empty_dict1)
#1. Use the pop function
empty_dict1.pop('Key1')
print(empty_dict1)
Result:
{'Key2': '2', 'Key3': '3'}
How to use the Del keyword to delete a key from a dictionary
In this example, we are taking the output of the above example, and just telling the logic to remove the key “Key2” then what it does is it prints the python dictionary without that key or its value.
del empty_dict1["Key2"]
print(empty_dict1)
Result:
{'Key3': '3'}
How to use dict.clear() to delete a key from a dictionary
In this final example, we use dict.clear(). Note this will empty everything out of the dictionary, so be careful in its use.
As can be seen, it takes the output of the previous example and empties it completely.
empty_dict1.clear() # Removes everything from the dictionary.
print(empty_dict1)
Result:
{}
Slide 8
#How to delete more than one key from a dictionary
#1. Create a list to lookup against
empty_dict1 = {}
empty_dict1['Key2'] = '2'
empty_dict1['Key1'] = '1'
empty_dict1['Key3'] = '3'
empty_dict1['Key4'] = '4'
empty_dict1['Key5'] = '5'
empty_dict1['Key6'] = '6'
print(empty_dict1)
dictionary_remove = ["Key5","Key6"] # Lookup list
#1. Use the pop method
for key in dictionary_remove:
empty_dict1.pop(key)
print(empty_dict1)
#2 Use the del method
dictionary_remove = ["Key3","Key4"]
for key in dictionary_remove:
del empty_dict1[key]
print(empty_dict1)
We hope you enjoyed this, we have plenty of videos that you can look at to improve your knowledge of Python here: Data Analytics Ireland Youtube
Following on from that, in this post, we are going to look at changing key names. You may find that you have the dictionary you want but you need to change some of the key names to ensure the proper names are consistent with other parts of your program.
Option1 uses the pop method :
Create a dictionary with the key-value pairs in it. Note that this will also be used for the second option.
Then this line European_countries[“United Kingdom”] = European_countries.pop(“UK”) basically says to find “UK” in the values, drop it and replace it with the value “United Kingdom”
After the values using the zip method are: {‘IRE’: ‘Dublin’, ‘FR’: ‘Paris’, ‘UK’: ‘London’}
#Slide 9
# How do you change the name of a key in a dictionary
#1. Create a new key , remove the old key, but keep the old key value
# create a dictionary
European_countries = {
"Ireland": "Dublin",
"France": "Paris",
"UK": "London"
}
print(European_countries)
#1. rename key in dictionary
European_countries["United Kingdom"] = European_countries.pop("UK")
# display the dictionary
print(European_countries)
#2. Use zip to change the values
European_countries = {
"Ireland": "Dublin",
"France": "Paris",
"United Kingdom": "London"
}
update_elements=['IRE','FR','UK']
new_dict=dict(zip(update_elements,list(European_countries.values())))
print(new_dict)
We hope you enjoyed this, we have plenty of videos that you can look at to improve your knowledge of Python here: Data Analytics Ireland Youtube
In our latest post, we are going to discuss formatting issues that you may come across. It relates to how you format the data when passing it from the input of a user.
This scenario can happen as a result of user input, which then has some comparisons completed on it.
How does the problem occur?
The problem occurs when you pass the data, but do not format it correctly before you present it in the output.
In the below code you will see these lines:
print ("'{0}' is older than '{1}'"% age1, age2)
print("'{0}'is younger than '{1}', that looks correct!" % age1, age2)
The specific problem is that when you put the % before age1, and age 2 you will get this TypeError.
Why does the problem occur?
This problem occurs as the way you are referencing age1 and age2 has been deprecated.
age1 = input("Please enter your age: ")
age2 = input("Please enter your father's age: ")
if age1 == age2:
print("The ages are the same, are you sure?!")
if age1 > age2:
#print ("'{0}' is older than '{1}'"% age1, age2)
print("'{0}' is older than '{1}', you better check!" .format(age1, age2))
if age1 < age2:
print("'{0}'is younger than '{1}', that looks correct!" % age1, age2)
#print("'{0}'is younger than '{1}', that looks correct!".format(age1, age2))
As you can see, I have included the old incorrect code and the new correct code, and when I use the proper Python logic, it gives me the correct output as follows:
Please enter your age: 25
Please enter your father's age: 28
'25'is younger than '28', that looks correct!
Process finished with exit code 0