Python3 Practice Program
Remove the leading spaces from the string input_str = ' This is my first code '
input_str = ' This is my first code'
final_str = input_str.strip()
print(final_str)
String Split
Description Split the string input_str = 'Kumar_Ravi_003' to the person's second name, first name and unique customer code. In this example, second_name= 'Kumar', first_name= 'Ravi', customer_code = '003'.
A sample output of the input 'Kumar_Ravi_003' is: Ravi Kumar 003
Note that you need to print in the order first name, last name and customer code.
# First Method
input_str ='Kumar_Ravi_003'
first_name = input_str[6:10]
second_name = input_str[0:5]#write your answer here
customer_code = input_str[11:14]#write your answer here
print(first_name)
print(second_name)
print(customer_code)
# Second Method
input_str ='Kumar_Ravi_003'
name = input_str.split('_')
first_name=name[0]
second_name = name[1]
customer_code = name[2]
print(second_name +" "+ first_name+" "+customer_code)
input_str = 'Kumar_Ravi_003'
#first we split the string using '_'
#first name will be the 2nd element (index 1) second name will be first element(index 0)
n_list=input_str.split("_")
first_name = n_list[1]#write your answer here
second_name = n_list[0]#write your answer here
customer_code = n_list[2]#write your answer here
print(first_name)
print(second_name)
print(customer_code)
List_remove_append
Description Remove SPSS from input_list=['SAS', 'R', 'PYTHON', 'SPSS'] and add 'SPARK' in its place.
input_list = ['SAS', 'R', 'PYTHON', 'SPSS']
# Write code to remove 'SPSS'
input_list.remove('SPSS')
# Write code to append 'SPARK'
input_list.append("SPARK")
print(input_list)
string to list conversion
Description Convert a string input_str = 'I love Data Science & Python' to a list by splitting it on ‘&’. The sample output for this string will be: ['I love Data Science ', ' Python']
input_str = 'I love Data Science & Python'
#we will simply split the string at &
output_list = input_str.split('&')#Type your answer here
print(output_list)
List to String
Description Convert a list ['Pythons syntax is easy to learn', 'Pythons syntax is very clear'] to a string using ‘&’. The sample output of this string will be: Pythons syntax is easy to learn & Pythons syntax is very clear
Note that there is a space on both sides of '&' (as usual in English sentences).
input_str = ['Pythons syntax is easy to learn', 'Pythons syntax is very clear']
string_1 =" & ".join(input_str) #Type your answer here
print(string_1)
Nested List
Description Extract Python from a nested list input_list = [['SAS','R'],['Tableau','SQL'],['Python','Java']]
input_list = [['SAS','R'],['Tableau','SQL'],['Python','Java']]
print(input_list[2][0])
Tuple
Description Add the element ‘Python’ to a tuple input_tuple = ('Monty Python', 'British', 1969). Since tuples are immutable, one way to do this is to convert the tuple to a list, add the element, and convert it back to a tuple.
To learn how to convert a list to a tuple, search for it on Google / Stack Overflow etc.
input_tuple = ('Monty Python', 'British', 1969)
list1=list(input_tuple)
list1.append('Python')
print(tuple(list1))
Dict_Error
Description From a Dictionary input_dict={'Name': 'Monty', 'Profession': 'Singer' }, get the value of a key ‘Label’ which is not a part of the dictionary, in such a way that Python doesn't hit an error. If the key does not exist in the dictionary, Python should return 'NA'.
# Method 1
input_dict={'Name': 'Monty', 'Profession': 'Singer' }
input_dict.get('Label','NA')
# Method 2
input_dict={'Name': 'Monty', 'Profession': 'Singer' }
if('Label' in input_dict.keys()):
answer = input_dict['Label']
else:
answer='NA'
print(answer)
Getting a Value from a Dictionary.
Description Extract the company headed by Tim Cook from the dictionary {'Jack Dorsey': 'Twitter', 'Tim Cook': 'Apple','Jeff Bezos': 'Amazon','Mukesh Ambani': 'RJIO'}
input_dict={'Jack Dorsey': 'Twitter', 'Tim Cook': 'Apple','Jeff Bezos': 'Amazon','Mukesh Ambani': 'RJIO'}
name = input_dict['Tim Cook']
print(name)
List of Values in a Dictionary.
Description Create a SORTED list of all values from the dictionary input_dict = {'Jack Dorsey' : 'Twitter' , 'Tim Cook' : 'Apple','Jeff Bezos' : 'Amazon' ,'Mukesh Ambani' : 'RJIO'}
input_dict = {'Jack Dorsey' : 'Twitter' , 'Tim Cook' : 'Apple','Jeff Bezos' : 'Amazon' ,'Mukesh Ambani' : 'RJIO'}
value_list = input_dict.values()
print(sorted(value_list))
What will the output of the following set of instructions be?
d = {'Python':40, 'R':45} print(list(d.keys()))
d = {'Python':40, 'R':45}
print(list(d.keys()))
O/p : ['Python', 'R']
Set_diff
Description Find the difference, using difference and symmetric_difference, between two given lists - list1 and list2.
First, convert the lists into sets and store them as set_1 and set_2. Then store the difference and symmetric difference in answer_1 and answer_2 respectively. Print both the answers as sorted lists, i.e. convert the final sets to lists, sort it and then return it.
list_1 = [1,2,3,4,5,6]
list_2 = [2,3,4,5,6,7,8,9]
set_1 = set(list_1)
set_2 = set(list_2)
answer_1 = sorted(list(set_1.difference(set_2)))
answer_2 = sorted(list(set_1.symmetric_difference(set_2)))
print(answer_1)
print(answer_2)
o/p
[1]
[1, 7, 8, 9]
If-Else
Description Write a code to check if the string in input_str starts with a vowel or not. Print capital YES or NO.
For example, if input_str = 'analytics' then, your output should print 'YES'.
#method1
input_str="alpha"
if input_str[0] in ['a','e','i','o','u']:
print('YES')
else:
print('NO'
#Method2
input_str="alpha"
i=input_str[0]
if(i in "aeiou"):
print('YES')
else:
print('NO')
What will the following segment of code print? Try solving it verbally.
if True or True:
if False and True or False:
print('A')
elif False and False or True and True:
print('B')
else:
print('C')
else:
print('D')
O/P : B
What will the following segment of code print? Try doing this verbally.
if (10 < 0) and (0 < -10):
print("A")
elif (10 > 0) or False:
print("B")
else:
print("C")
Creating a List Comprehension
Description You are given an integer 'n' as the input. Create a list comprehension containing the squares of the integers from 1 till n^2 (including 1 and n), and print the list.
For example, if the input is 4, the output should be a list as follows:
[1, 4, 9, 16]
#Method1
n = int(input('Enter number'))
square=[i**2 for i in range(1,n+1)]
print(square)
Enter number4
[1, 4, 9, 16]
#Method2
n = int(input('Enter number'))
# Write your code here (remember to print the list)
final_list=[i**2 for i in range(1,n+1)] #remember to use range(1,n+1)
#using range(n) will give 0,1,2,... n-1
#we want 1, 2, 3, 4, ... n
print(final_list)
Function
Description Create a function squared(), which takes x and y as arguments and returns the x**y value. For e.g., if x = 2 and y = 3 , then the output is 8.
input_list = ['6','7']
x = int(input_list[0])
y = int(input_list[1])
def squared(x,y):
return(x**y)
print(squared(x,y))
Lambda
Description Create a lambda function 'greater', which takes two arguments x and y and return x if x>y otherwise y. If x = 2 and y= 3, then the output should be 3.
#Method1
input_list = [4,5]
a = int(input_list[0])
b = int(input_list[1])
greater=lambda x,y: x if x>y else y
print(greater(a,b))
#Method2
input_list = [4,5]
a = int(input_list[0])
b = int(input_list[1])
#Write your code here
def greater(a,b):
if(a>b):
return a
return b
print(greater(a,b))
Print word number of Times
def say(message, times = 1):
print(message * times)
say('Hello')
say('World', 5)
Map Function
Description Using the Map function, create a list 'cube', which consists of the cube of numbers in input_list.
For e.g. if the input list is [5,6,4,8,9], the output should be [125, 216, 64, 512, 729]
input_list = [5,6,4,8,9]
cube=list(map(lambda x: x**3, input_list))
print(cube)
Map Function
Description Using the function Map, count the number of words that start with ‘S’ in input_list.
input_list = ['San Jose', 'San Francisco', 'Santa Fe', 'Houston']
count = sum(map(lambda x: x[0] == 'S', input_list))
print(count)
Map Function
Description Create a list ‘name’ consisting of the combination of the first name and the second name from list 1 and 2 respectively.
For e.g. if the input list is: [ ['Ankur', 'Avik', 'Kiran', 'Nitin'], ['Narang', 'Sarkar', 'R', 'Sareen']]
the output list should be the list: ['Ankur Narang', 'Avik Sarkar', 'Kiran R', 'Nitin Sareen']
input_list = [['Ankur','Avik','Kiran','Nitin'],['Narang','Sarkar','R','Sareen']]
first_name = input_list[0]
last_name = input_list[1]
combine=lambda x,y:x+' '+y
name = list(map(combine,first_name,last_name))
print(name)
O/P : ['Ankur Narang', 'Avik Sarkar', 'Kiran R', 'Nitin Sareen']
Filter Function
Description You are given a list of strings such as input_list = ['hdjk', 'salsap', 'sherpa'].
Extract a list of names that start with an ‘s’ and end with a ‘p’ (both 's' and 'p' are lowercase) in input_list.
Note: Use the filter() function
input_list = ['hdjk', 'salsap', 'sherpa']
sp =list(filter(lambda x:x[0].lower()=='s' and x[-1]=='p',input_list))
print(sp)
O/P: ['salsap']
Reduce Function
Description Using the Reduce function, concatenate a list of words in input_list, and print the output as a string. If input_list = ['I','Love','Python'], the output should be the string 'I Love Python'.
input_list=['All','you','have','to','fear','is','fear','itself']
from functools import reduce
result=reduce(lambda x,y: x+" "+y, input_list)
print(result)
O/P: All you have to fear is fear itself
Reduce Function
Description You are given a list of numbers such as input_list = [31, 63, 76, 89]. Find and print the largest number in input_list using the reduce() function
input_list = [65,76,87,23,12,90,99]
from functools import reduce
answer = reduce(lambda x,y: x if x>y else y,input_list)
print(answer)
How will you extract ‘love’ from the string S = “I love Python”?
S = "I love Python"
print(S[2:6])
print(S[2:-7])
print(S[-11:-7])
love
love
love
Dictionary Iteration
What will the output be of the following code?
D = {1:['Raj', 22], 2:['Simran', 21], 3:['Rahul', 40]}
for val in D:
print(val)
O/p
1
2
3
Python Comprehensions
What will the ‘comprehension equivalent’ be for the following snippet of code?
for sentence in paragraph:
for word in sentence.split():
single_word_list.append(word)
Answer
[word for sentence in paragraph for word in sentence.split()]
Feedback :
[word for sentence in paragraph for word in sentence.split()] is the right comprehension equivalent of the code provided. You need to put it in square brackets [] since the output will be a list.
Function Arguments
What will the output of the following code be?
def my_func(*args):
return(sum(args))
print(my_func(1,2,3,4,5))
print(my_func(6,7,8))
def my_func(*args):
return(sum(args))
print(my_func(1,2,3,4,5))
print(my_func(6,7,8))
15
21
squares of all the numbers in a list L = [1, 2, 3, 4]?
L = [1, 2, 3, 4]
print(list(map(lambda x : x ** 2, L)))
[1, 4, 9, 16]
Factorial
Description Given a number ‘n’, output its factorial using reduce(). Note: Make sure you handle the edge case of zero. As you know, 0! = 1
P.S.: Finding the factorial without using the reduce() function might lead to deduction of marks.
Examples:
Input 1: 1 Output 1: 1
Input 2: 3 Output 2: 6
def factorial(n):
if (n == 0):
return 1
else:
return reduce( lambda x,y:x*y , range(1,n+1))
print(factorial(n))
#with Reduce Function
n = int(input())
fact = reduce(lambda x, y: x*y, range(1, n + 1)) if n>0 else 0 if n == 0 else 'factorial not possible'
print(fact)
#method2
# Read the input as an integer
n = int(input())
# Import the reduce() function
from functools import reduce
# If n is zero, simply print 1 as this case can't be handles by reduce()
if n==0:
print(1)
# In all other cases, use reduce() between the range 1 and (n+1). For this range,
# define a lambda function with x and y and keep multiplying them using reduce().
# This way, when the code reaches the end of the range, i.e. n, the factorial
# computation will be complete.
else:
print(reduce(lambda x, y: x * y, range(1, n+1)))
Missing Values removal
Description Count the number of missing values in each column of the dataset 'marks'.
import pandas as pd
marks = pd.read_csv('https://query.data.world/s/HqjNNadqEnwSq1qnoV_JqyRJkc7o6O')
print(marks.isnull().sum())
Prefix 0
Assignment 2
Tutorial 12
Midterm 16
TakeHome 9
Final 5
dtype: int64
Removing rows with missing values
Description Remove all the rows in the dataset 'marks' having 5 missing values and then print the number of missing values in each column.
import pandas as pd
marks = pd.read_csv('https://query.data.world/s/HqjNNadqEnwSq1qnoV_JqyRJkc7o6O')
marks=marks.dropna(thresh=2)
print(marks.isnull().sum())
#Method1
import pandas as pd
df = pd.read_csv('https://query.data.world/s/HqjNNadqEnwSq1qnoV_JqyRJkc7o6O')
df = df[df.isnull().sum(axis=1) != 5]
print(df.isnull().sum())
Removing extra characters from a column
Description The given data frame 'customer' has a column 'Cust_id' which has values Cust_1, Cust_2 and so on. Remove the repeated 'Cust_' from the column Cust_id so that the output column Cust_id have just numbers like 1, 2, 3 and so on. Print the first 10 rows of the dataset 'customer' after processing.
#METHOD1
import pandas as pd
customer = pd.read_csv('https://query.data.world/s/y9rxL9mGdP6AXPiDaIL4yYm6DsfTV2')
customer['Cust_id'] =customer['Cust_id'].str.replace("Cust_",'')
print(customer.head(10))
#METHOD2
import pandas as pd
customer = pd.read_csv('https://query.data.world/s/y9rxL9mGdP6AXPiDaIL4yYm6DsfTV2')
customer['Cust_id'] = customer['Cust_id'].map(lambda x: x.strip('Cust_'))
print(customer.head(10))
Customer_Name Province Region Customer_Segment Cust_id
0 MUHAMMED MACINTYRE NUNAVUT NUNAVUT SMALL BUSINESS 1
1 BARRY FRENCH NUNAVUT NUNAVUT CONSUMER 2
2 CLAY ROZENDAL NUNAVUT NUNAVUT CORPORATE 3
3 CARLOS SOLTERO NUNAVUT NUNAVUT CONSUMER 4
4 CARL JACKSON NUNAVUT NUNAVUT CORPORATE 5
Rounding decimal places of a column
Description The given dataframe 'sleepstudy' has a column 'Reaction' with floating integer values up to 4 decimal places. Round off the decimal places to 1.
# Method1
from pydataset import data
sleepstudy =data('sleepstudy')
sleepstudy['Reaction'] = sleepstudy['Reaction'].round(1)
print(sleepstudy.head(10))
#Method2
from pydataset import data
sleepstudy =data('sleepstudy')
sleepstudy['Reaction'] = sleepstudy['Reaction'].round(decimals=1)
print(sleepstudy.head(10))
Reaction Days Subject
1 249.6 0 308
2 258.7 1 308
3 250.8 2 308
Duplicated Rows
Description The given Dataframe 'rating' has repeated rows. You need to remove the duplicated rows.
import pandas as pd
rating = pd.read_csv('https://query.data.world/s/EX0EpmqwfA2UYGz1Xtd_zi4R0dQpog')
rating_update = rating.drop_duplicates()
print(rating.shape)
print(rating_update.shape)
(1254, 5)
(1149, 5)
Derived Variable
Description The given dataset 'cust_rating' has 3 columns i.e 'rating', ' food_rating', 'service_rating'. Create a new variable 'avg_rating'.
import pandas as pd
cust_rating = pd.read_csv('https://query.data.world/s/ILc-P4llUraMaYN6N6Bdw7p6kUvHnj')
cust_rating['avg_rating'] = round( (cust_rating['rating']+ cust_rating['food_rating']+ cust_rating['service_rating'])/3)
print(cust_rating.head(10))
userID placeID rating food_rating service_rating avg_rating
0 U1077 135085 2 2 2 2.0
1 U1077 135038 2 2 1 2.0
2 U1077 132825 2 2 2 2.0
Extracting Day From a Date
Description The given dataset 'order' has a variable 'Order_Date' with the dates of purchase. Create a new variable 'day' which will contain the day from the date at variable Order_Date.
import pandas as pd
order = pd.read_csv('https://query.data.world/s/3hIAtsCE7vYkPEL-O5DyWJAeS5Af-7')
order['Order_Date'] = pd.to_datetime(order['Order_Date'])
order['day'] = order['Order_Date'].dt.day
print(order.head(10))
Order_ID Order_Date Order_Priority Ord_id day
0 3 2010-10-13 LOW Ord_1 13
1 293 2012-01-10 HIGH Ord_2 10
2 483 2011-10-07 HIGH Ord_3 7
3 515 2010-08-28 NOT SPECIFIED Ord_4 28
4 613 2011-06-17 HIGH Ord_5 17
#Alternate MEthod
import pandas as pd
order = pd.read_csv('https://query.data.world/s/3hIAtsCE7vYkPEL-O5DyWJAeS5Af-7')
order['Order_Date'] = pd.to_datetime(order['Order_Date'])
order['day'] = order['Order_Date'].apply(lambda x: x.day)
print(order.head(10))
You're given a list of non-negative integers. Your task is to round the given numbers to the nearest multiple of 10. For instance, 15 should be rounded to 20 whereas 14 should be rounded to 10. After rounding the numbers, find their sum.
Hint: The Python pre-defined function round() rounds off to nearest even number - it round 0.25 to 0.2. You might want to write your own function to round as per your requirement.
Sample input (a list): [2, 18, 10]
Sample output (an integer): 30
import ast,sys
import math
input_str = sys.stdin.read()
input_list = [2, 18, 10]
# write code here
# rounds to nearest, ties away from zero
def custom_round(n, ndigits=1):
"""
Takes in any decimal number and outputs rounded number
examples:
0.25 is rounded to 0.3
0.35 is rounded to 0.4
0.21 is rounded to 0.2
"""
part = n * 10 ** ndigits
delta = part - int(part)
# round to nearest, ties away from zero
if delta >= 0.5:
part = math.ceil(part)
else:
part = math.floor(part)
return part / (10 ** ndigits)
def round_to_nearest_10(n):
""" takes in 15 and outputs 20"""
return int(100*custom_round(n/100, 1))
rounded_list = list(map(round_to_nearest_10, input_list))
result = sum(rounded_list)
# do not change the following code
print(result)
Sum and Squares You're given a natural number 'n'. First, calculate the sum of squares of all the natural numbers up to 'n'. Then calculate the square of the sum of all natural numbers up to 'n'. Return the absolute difference of these two quantities.
For instance, if n=3, then natural numbers up to 3 are: 1, 2 and 3. The sum of squares up to 3 will be 1^2 + 2^2 + 3^2 = 14. The square of the sum of natural numbers up to 3 is (1+2+3)^2=36. The result, which is their absolute difference is 22.
Sample input (an integer): 3
Sample output (an integer): 22
n = 3
def sum2(n):
s=0
for i in range(n+1):
s=s+i**2
return s
def sum1(n):
s=0
for i in range(n+1):
s=s+i
return s**2
# store the result in the following variable
abs_difference = sum1(n)-sum2(n)
# print result --- do not change the following code
print(abs_difference)
#Method 1
import ast,sys
input_str = sys.stdin.read()
n = 3
# write your code here
numbers = [number+1 for number in range(n)]
sum_of_squares = sum(list(map(lambda x: x**2, numbers)))
square_of_sum = sum(numbers)**2
# store the result in the following variable
abs_difference = abs(sum_of_squares - square_of_sum)
# print result --- do not change the following code
print(abs_difference)
def reverse(s):
str = ""
for i in s:
str = i + str
return str
s = ['ram', 'krishn','mishra']
rev=list(map(reverse,s))
print(rev)
Weird Function
In data science, quite often you need to implement research papers and write code according to what's present in those papers. Research papers have a lot of maths involved and you need to implement the maths in code. In this exercise, you're required to implement some maths in code. The problem is as follows:
For fixed integers a, b, c, define a weird function F(n) as follows: F(n) = n - c for all n > b F(n) = F(a + F(a + F(a + F(a + n)))) for all n ≤ b.
Also, define S(a, b, c) = ∑F(n) where n takes the values 0 till b [in other words, S(a, b, c) = F(0) + F(1) + F(2) + .... F(b-1) + F(b)].
The input will be the value of a, b and c. The output should be S(a, b, c). You can define the functions in your own customized way with no restrictions on the number of parameters. For example, you can define the function S which can take additional parameters than a, b and c. Just make sure the code behaves as per the maths.
For example, if a = 20, b = 100 and c = 15, then F(0) = 195 and F(2000) = 1985. Therefore, S(20, 100, 15) = 14245
import numpy as np
input_list = [20,100,15]
a = input_list[0]
b = input_list[1]
c = input_list[2]
# write code here
sum = 0
def weird_function(a,b,c,n):
if n>b:
return n-c
else:
return weird_function(a,b,c,a+weird_function(a,b,c,a+weird_function(a,b,c,a+weird_function(a,b,c,a+n))))
def large_sum(a, b, c):
large_sum = 0
for value in range(b+1):
large_sum += weird_function(a, b, c, value)
return large_sum
# store the result in the following variable
result = large_sum(a, b, c)
# print result -- do not change the following code
print(result)
Python Program to check Armstrong Number
def digit(n):
count=0
while n!=0:
rem=n%10
n=n//10
count=count+1
return count
def armstrong(n,d):
sum=0
while n!=0:
rem=n%10
sum=sum+(pow(rem,d))
n=n//10
return sum
n=int(input('Enter number'))
print(armstrong(n,digit(n)))
if(n==armstrong(n,digit(n))):
print('Number is Armstrong')
else:
print('Number is Not Armstrong')
Swap two rows
Description Given m and n, swap the mth and nth rows of the 2-D NumPy array given below.
a = [[4 3 1] [5 7 0] [9 9 3] [8 2 4]]
import numpy as np
# Given array
a = np.array([[4, 3, 1], [5, 7, 0], [9, 9, 3], [8, 2, 4]])
# Read the values of m and n
m = 0
n = 2
a[[m,n]]=a[[n,m]]
# Print the array after swapping
print(a)
Create border array Description Given a single integer n, create an (n x n) 2D array with 1 on the border and 0 on the inside.
Note: Make sure the array is of type int.
Example: Input 1: 4 Output 1: [[1 1 1 1] [1 0 0 1] [1 0 0 1] [1 1 1 1]] Input 2: 2 Output 2: [[1 1] [1 1]]
# Read the variable from STDIN
n = int(input())
import numpy as np
a=np.ones((n,n), dtype=int)
a[1:-1,1:-1] = 0
print(a)
Set Index in Dataframe
Description Using set_index command set the column 'X' as the index of the dataset and then print the head of the dataset. Hint: Use inplace = False
import pandas as pd
df = pd.read_csv('https://query.data.world/s/vBDCsoHCytUSLKkLvq851k2b8JOCkF')
df_2 = df.set_index('X',inplace=False)
print(df_2.head())
Y month day FFMC DMC DC ISI temp RH wind rain area
X
7 5 mar fri 86.2 26.2 94.3 5.1 8.2 51 6.7 0.0 0.0
7 4 oct tue 90.6 35.4 669.1 6.7 18.0 33 0.9 0.0 0.0
Sorting Dataframes
Description Sort the dataframe on 'month' and 'day' in ascending order in the dataframe 'df'.
import pandas as pd
df = pd.read_csv('https://query.data.world/s/vBDCsoHCytUSLKkLvq851k2b8JOCkF')
df_2 = df.sort_values(by=['month','day'],ascending=True)
print(df_2.head(20))
X Y month day FFMC DMC DC ISI temp RH wind rain area
241 4 4 apr fri 83.0 23.3 85.3 2.3 16.7 20 3.1 0.0 0.00
442 6 5 apr mon 87.9 24.9 41.6 3.7 10.9 64 3.1 0.0 3.35
19 6 4 apr sat 86.3 27.4 97.1 5.1 9.3 44 4.5 0.0 0.00
239 7 5 apr sun 81.9 3.0 7.9 3.5 13.4 75 1.8 0.0 0.00
DataFrames
Description Given a dataframe 'df' use the following commands and analyse the result. describe() columns shape
import numpy as np
import pandas as pd
df = pd.read_csv('https://query.data.world/s/vBDCsoHCytUSLKkLvq851k2b8JOCkF')
print(df.describe())
print(df.columns)
print(df.shape)
X Y FFMC ... wind rain area
count 517.000000 517.000000 517.000000 ... 517.000000 517.000000 517.000000
mean 4.669246 4.299807 90.644681 ... 4.017602 0.021663 12.847292
std 2.313778 1.229900 5.520111 ... 1.791653 0.295959 63.655818
min 1.000000 2.000000 18.700000 ... 0.400000 0.000000 0.000000
Indexing Dataframes
Description Print only the even numbers of rows of the dataframe 'df'.
Note: Don't include the row indexed zero.
import pandas as pd
df = pd.read_csv('https://query.data.world/s/vBDCsoHCytUSLKkLvq851k2b8JOCkF')
df_2 = df[2::2]
print(df_2.head(20))
X Y month day FFMC DMC DC ISI temp RH wind rain area
2 7 4 oct sat 90.6 43.7 686.9 6.7 14.6 33 1.3 0.0 0.0
4 8 6 mar sun 89.3 51.3 102.2 9.6 11.4 99 1.8 0.0 0.0
6 8 6 aug mon 92.3 88.9 495.6 8.5 24.1 27 3.1 0.0 0.0
8 8 6 sep tue 91.0 129.5 692.6 7.0 13.1 63 5.4 0.0 0.0
10 7 5 sep sat 92.5 88.0 698.6 7.1 17.8 51 7.2 0.0 0.0
12 6 5 aug fri 63.5 70.8 665.3 0.8 17.0 72 6.7 0.0 0.0
Remove the leading spaces from the string input_str = ' This is my first code'
[ ]
1
2
3
4
5
#Remove the leading spaces from the string input_str = ' This is my first code'
# Reading the input as a string; ignore the following two lines
input_str = ' This is my first Code'
final_str = input_str.strip()
print(final_str)
This is my first Code
String Split
Description Split the string input_str = 'Kumar_Ravi_003' to the person's second name, first name and unique customer code. In this example, second_name= 'Kumar', first_name= 'Ravi', customer_code = '003'.
A sample output of the input 'Kumar_Ravi_003' is: Ravi Kumar 003
Note that you need to print in the order first name, last name and customer code.
# First Method
input_str ='Kumar_Ravi_003'
first_name = input_str[6:10]
second_name = input_str[0:5]#write your answer here
customer_code = input_str[11:14]#write your answer here
print(first_name)
print(second_name)
print(customer_code)
Ravi
Kumar
003
# Second Method
input_str ='Kumar_Ravi_003'
name = input_str.split('_')
first_name=name[0]
second_name = name[1]
customer_code = name[2]
print(second_name +" "+ first_name+" "+customer_code)
Ravi Kumar 003
input_str = 'Kumar_Ravi_003'
#first we split the string using '_'
#first name will be the 2nd element (index 1) second name will be first element(index 0)
n_list=input_str.split("_")
first_name = n_list[1]#write your answer here
second_name = n_list[0]#write your answer here
customer_code = n_list[2]#write your answer here
print(first_name)
print(second_name)
print(customer_code)
Ravi
Kumar
003
17 / 3 # classic division returns a float 17 // 3 # floor division discards the fractional part
List_remove_append
Description Remove SPSS from input_list=['SAS', 'R', 'PYTHON', 'SPSS'] and add 'SPARK' in its place.
[ ]
input_list = ['SAS', 'R', 'PYTHON', 'SPSS']
# Write code to remove 'SPSS'
input_list.remove('SPSS')
# Write code to append 'SPARK'
input_list.append("SPARK")
print(input_list)
['SAS', 'R', 'PYTHON', 'SPARK']
string to list conversion
Description Convert a string input_str = 'I love Data Science & Python' to a list by splitting it on ‘&’. The sample output for this string will be: ['I love Data Science ', ' Python']
input_str = 'I love Data Science & Python'
#we will simply split the string at &
output_list = input_str.split('&')#Type your answer here
print(output_list)
['I love Data Science ', ' Python']
List to String
Description Convert a list ['Pythons syntax is easy to learn', 'Pythons syntax is very clear'] to a string using ‘&’. The sample output of this string will be: Pythons syntax is easy to learn & Pythons syntax is very clear
Note that there is a space on both sides of '&' (as usual in English sentences).
[ ]
input_str = ['Pythons syntax is easy to learn', 'Pythons syntax is very clear']
string_1 =" & ".join(input_str) #Type your answer here
print(string_1)
Pythons syntax is easy to learn & Pythons syntax is very clear
Nested List
Description Extract Python from a nested list input_list = [['SAS','R'],['Tableau','SQL'],['Python','Java']]
input_list = [['SAS','R'],['Tableau','SQL'],['Python','Java']]
print(input_list[2][0])
Python
Tuple
Description Add the element ‘Python’ to a tuple input_tuple = ('Monty Python', 'British', 1969). Since tuples are immutable, one way to do this is to convert the tuple to a list, add the element, and convert it back to a tuple.
To learn how to convert a list to a tuple, search for it on Google / Stack Overflow etc.
input_tuple = ('Monty Python', 'British', 1969)
list1=list(input_tuple)
list1.append('Python')
print(tuple(list1))
('Monty Python', 'British', 1969, 'Python')
[ ]
Dict_Error
Description From a Dictionary input_dict={'Name': 'Monty', 'Profession': 'Singer' }, get the value of a key ‘Label’ which is not a part of the dictionary, in such a way that Python doesn't hit an error. If the key does not exist in the dictionary, Python should return 'NA'.
[ ]
# Method 1
input_dict={'Name': 'Monty', 'Profession': 'Singer' }
input_dict.get('Label','NA')
'NA'
# Method 2
input_dict={'Name': 'Monty', 'Profession': 'Singer' }
if('Label' in input_dict.keys()):
answer = input_dict['Label']
else:
answer='NA'
print(answer)
NA
Getting a Value from a Dictionary.
Description Extract the company headed by Tim Cook from the dictionary {'Jack Dorsey': 'Twitter', 'Tim Cook': 'Apple','Jeff Bezos': 'Amazon','Mukesh Ambani': 'RJIO'}
input_dict={'Jack Dorsey': 'Twitter', 'Tim Cook': 'Apple','Jeff Bezos': 'Amazon','Mukesh Ambani': 'RJIO'}
name = input_dict['Tim Cook']
print(name)
Apple
List of Values in a Dictionary.
Description Create a SORTED list of all values from the dictionary input_dict = {'Jack Dorsey' : 'Twitter' , 'Tim Cook' : 'Apple','Jeff Bezos' : 'Amazon' ,'Mukesh Ambani' : 'RJIO'}
[ ]
1
2
3
input_dict = {'Jack Dorsey' : 'Twitter' , 'Tim Cook' : 'Apple','Jeff Bezos' : 'Amazon' ,'Mukesh Ambani' : 'RJIO'}
value_list = input_dict.values()
print(sorted(value_list))
['Amazon', 'Apple', 'RJIO', 'Twitter']
What will the output of the following set of instructions be?
d = {'Python':40, 'R':45} print(list(d.keys()))
[ ]
1
2
d = {'Python':40, 'R':45}
print(list(d.keys()))
['Python', 'R']
Set_diff
Description Find the difference, using difference and symmetric_difference, between two given lists - list1 and list2.
First, convert the lists into sets and store them as set_1 and set_2. Then store the difference and symmetric difference in answer_1 and answer_2 respectively. Print both the answers as sorted lists, i.e. convert the final sets to lists, sort it and then return it.
[ ]
1
2
3
4
5
6
7
8
9
10
list_1 = [1,2,3,4,5,6]
list_2 = [2,3,4,5,6,7,8,9]
set_1 = set(list_1)
set_2 = set(list_2)
answer_1 = sorted(list(set_1.difference(set_2)))
answer_2 = sorted(list(set_1.symmetric_difference(set_2)))
print(answer_1)
print(answer_2)
[1]
[1, 7, 8, 9]
If-Else
Description Write a code to check if the string in input_str starts with a vowel or not. Print capital YES or NO.
For example, if input_str = 'analytics' then, your output should print 'YES'.
[ ]
1
2
3
4
5
6
#method1
input_str="alpha"
if input_str[0] in ['a','e','i','o','u']:
print('YES')
else:
print('NO')
YES
[ ]
1
2
3
4
5
6
7
#Method2
input_str="alpha"
i=input_str[0]
if(i in "aeiou"):
print('YES')
else:
print('NO')
YES
What will the following segment of code print? Try solving it verbally.
[ ]
1
2
3
4
5
6
7
8
9
if True or True:
if False and True or False:
print('A')
elif False and False or True and True:
print('B')
else:
print('C')
else:
print('D')
B
What will the following segment of code print? Try doing this verbally.
[ ]
1
2
3
4
5
6
if (10 < 0) and (0 < -10):
print("A")
elif (10 > 0) or False:
print("B")
else:
print("C")
B
Creating a List Comprehension
Description You are given an integer 'n' as the input. Create a list comprehension containing the squares of the integers from 1 till n^2 (including 1 and n), and print the list.
For example, if the input is 4, the output should be a list as follows:
[1, 4, 9, 16]
[ ]
1
2
3
4
5
6
#Method1
n = int(input('Enter number'))
square=[i**2 for i in range(1,n+1)]
print(square)
Enter number4
[1, 4, 9, 16]
[ ]
1
2
3
4
5
6
7
8
9
10
#Method2
n = int(input('Enter number'))
# Write your code here (remember to print the list)
final_list=[i**2 for i in range(1,n+1)] #remember to use range(1,n+1)
#using range(n) will give 0,1,2,... n-1
#we want 1, 2, 3, 4, ... n
print(final_list)
Enter number5
[1, 4, 9, 16, 25]
Function
Description Create a function squared(), which takes x and y as arguments and returns the x**y value. For e.g., if x = 2 and y = 3 , then the output is 8.
[ ]
1
2
3
4
5
6
7
8
input_list = ['6','7']
x = int(input_list[0])
y = int(input_list[1])
def squared(x,y):
return(x**y)
print(squared(x,y))
279936
Lambda
Description Create a lambda function 'greater', which takes two arguments x and y and return x if x>y otherwise y. If x = 2 and y= 3, then the output should be 3.
[ ]
1
2
3
4
5
6
#Method1
input_list = [4,5]
a = int(input_list[0])
b = int(input_list[1])
greater=lambda x,y: x if x>y else y
print(greater(a,b))
5
[ ]
1
2
3
4
5
6
7
8
9
10
11
12
#Method2
input_list = [4,5]
a = int(input_list[0])
b = int(input_list[1])
#Write your code here
def greater(a,b):
if(a>b):
return a
return b
print(greater(a,b))
5
[ ]
1
2
3
4
5
def say(message, times = 1):
print(message * times)
say('Hello')
say('World', 5)
Hello
WorldWorldWorldWorldWorld
Map Function
Description Using the Map function, create a list 'cube', which consists of the cube of numbers in input_list.
For e.g. if the input list is [5,6,4,8,9], the output should be [125, 216, 64, 512, 729]
[ ]
1
2
3
input_list = [5,6,4,8,9]
cube=list(map(lambda x: x**3, input_list))
print(cube)
[125, 216, 64, 512, 729]
Map Function
Description Using the function Map, count the number of words that start with ‘S’ in input_list.
[ ]
1
2
3
input_list = ['San Jose', 'San Francisco', 'Santa Fe', 'Houston']
count = sum(map(lambda x: x[0] == 'S', input_list))
print(count)
3
Map Function
Description Create a list ‘name’ consisting of the combination of the first name and the second name from list 1 and 2 respectively.
For e.g. if the input list is: [ ['Ankur', 'Avik', 'Kiran', 'Nitin'], ['Narang', 'Sarkar', 'R', 'Sareen']]
the output list should be the list: ['Ankur Narang', 'Avik Sarkar', 'Kiran R', 'Nitin Sareen']
[ ]
1
2
3
4
5
6
input_list = [['Ankur','Avik','Kiran','Nitin'],['Narang','Sarkar','R','Sareen']]
first_name = input_list[0]
last_name = input_list[1]
combine=lambda x,y:x+' '+y
name = list(map(combine,first_name,last_name))
print(name)
['Ankur Narang', 'Avik Sarkar', 'Kiran R', 'Nitin Sareen']
Filter Function
Description You are given a list of strings such as input_list = ['hdjk', 'salsap', 'sherpa'].
Extract a list of names that start with an ‘s’ and end with a ‘p’ (both 's' and 'p' are lowercase) in input_list.
Note: Use the filter() function.
[ ]
1
2
3
input_list = ['hdjk', 'salsap',