Python Basic Interview Questions
Describe list
Lists are used to store multiple items in a single variable.
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has an index [0]
, the second item has index [1]
etc.
When we say that lists are ordered, the items have a defined order, and that order will not change.
If you add new items to a list, the new items will be placed at the end of the list.
The list is changeable, meaning we can change, add, and remove items in a list after it has been created.
example : thislist = [“apple”, “banana”, “cherry”, “apple”, “cherry”]
Create a single list from the nested list without the nested values
lista = [1,2,3,4,[5,6],[[7,8,9]]]
def combine_list(value):
if isinstance(value,list):
for v in value:
for e in check_list(v):
yield e
else:
yield value
vv=list(combine_list(lista))
print(vv)
- Combine the two lists below and discard identical values.
a=[1,2,3,4,5]
b=[4,5,7,8,9]
common_values = set(a) & set(b) #get common values by using & symbol between two sets
joinlist = set(a+b) #Join two lists without duplicates by using set func
final_list = list(joinlist-common_values) #Remove identical values
- What Is set?
- Sets are used to store multiple items in a single variable.
- Sets are unordered, so you cannot be sure in which order the items will appear
- It won't allow duplicate values
- Once a set is created, you cannot change its items, but you can remove and add new items.
thisset = {"apple", "banana", "cherry", "apple"}
- What’s a map?
# Return double of n
def addition(n):
return n + n
# We double all numbers using map()
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))
- The
map()
function executes a specified function for each item in an iterable. The item is sent to the function as a parameter.
If variables A and B has the same value then what will be the ID of these variables?
Answer: “Yes” both Id’s are same
a = 7
b = 7
print('Id of a', id(a))
print('Id of b', id(b))
#output
#Id of a 1514755615216
#Id of b 1514755615216
Pandas Questions:
- What distinguishes iloc from loc in a dataframe?
- The main difference between pandas
loc[]
vsiloc[]
is loc gets DataFrame rows & columns by labels/names and iloc[] gets by integer Index/position. For loc[], if the label is not present it gives a key error. For iloc[], if the position is not present it gives an index error.
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
'Fee' :[20000,25000,26000,22000,24000],
'Duration':['30day','40days','35days','40days','60days'],
'Discount':[1000,2300,1200,2500,2000]
}
df = pd.DataFrame(technologies)
# Select Single Column by label
print(df.loc[:, "Courses"])
# Select Single Column by Index
print(df.iloc[:, 0])
# Using Conditions
print(df.loc[df['Fee'] >= 24000])
print(df.iloc[list(df['Fee'] >= 24000)])
#%%
# Select Multiple Rows by Index
print(df.iloc[[1,2]])
index_labels=['r1','r2','r3','r4','r5']
df['lb'] = index_labels
df = df.set_index(lb)
# Select Multiple Rows by Label
print(df.loc[['r2','r3']])
# Select Single Row by Index Label
print(df.loc['r2'])
What is difference between dataframe.info and dataframe.describe?
The describe()
method returns description of the data in the DataFrame.
If the DataFrame contains numerical data, the description contains these information for each column:
count — The number of not-empty values.
mean — The average (mean) value.
std — The standard deviation.
min — the minimum value.
25% — The 25% percentile*.
50% — The 50% percentile*.
75% — The 75% percentile*.
max — the maximum value.
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
'Fee' :[20000,25000,26000,22000,24000],
'Duration':['30day','40days','35days','40days','60days'],
'Discount':[1000,2300,1200,2500,2000]
}
df = pd.DataFrame(technologies)
print(df.describe())
Fee Discount
count 5.000000 5.000000
mean 23400.000000 1800.000000
std 2408.318916 667.083203
min 20000.000000 1000.000000
25% 22000.000000 1200.000000
50% 24000.000000 2000.000000
75% 25000.000000 2300.000000
max 26000.000000 2500.000000
The info()
method prints information about the DataFrame.
The information contains the “ total rows”, “number of columns”, column labels, column data types, memory usage, range index, and the number of cells in each column (non-null values).
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
'Fee' :[20000,25000,26000,22000,24000],
'Duration':['30day','40days','35days','40days','60days'],
'Discount':[1000,2300,1200,2500,2000]
}
df = pd.DataFrame(technologies)
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Courses 5 non-null object
1 Fee 5 non-null int64
2 Duration 5 non-null object
3 Discount 5 non-null int64
dtypes: int64(2), object(2)
memory usage: 288.0+ bytes
What distinguishes JOIN, MERGE, and CONCAT in a data-frame?
- JOIN- Used to join two dataframes without mention the column name but we can mention the join type (right or left)
import pandas as pd
# Creating the two dataframes
left = pd.DataFrame([['a', 1], ['b', 2]], list('XY'), list('PQ'))
right = pd.DataFrame([['c', 3], ['d', 4]], list('XY'), list('PR'))
joined_df = left.join(right, lsuffix='_')
print(joined_df)
- MERGE- more or less does the same thing as join. Both methods are used to combine two dataframes together, but merge is more versatile, it requires specifying the columns as a merge key. We can specify the overlapping columns with parameter on, or can separately specify it with left_on and right_on parameters.
merged_df = left.merge(right, on='P', how='outer')
print(merged_df)
- CONCAT-your dataframes are just stitched together along an axis — either the row axis or column axis.
- Concatenation is a bit different from the merging techniques that you saw above. With merging, you can expect the resulting dataset to have rows from the parent datasets mixed in together, often based on some commonality.
concatenated = pandas.concat([left, right], axis=1)
concatenated = pandas.concat([left, right], axis=0)
Flask Questions
How to render the footer dynamically in Flask
- define the variable which is going to be rendered in footer in HTML file(wherever you are displaying footer)
<p>{{ content}} - is logged in</p>
- send the “content” variable in every response from the views(backend). HTML will automatically picks up the variable and applied the value to declared page
- This {{ }} called jinja templates variable mapping
Additional considerations that must be made include:
1. How to perform the operations of Delete, Insert, and Update on a List, dictionaries
2. How to join the two list
3. How to join the two dictionaries
Happy coding!