M__
Posted on June 12, 2020
Python datatypes include: str(string), int(integer), float(float) and bool(boolean). Compared to Java they are few and more manageable (in my opinion) so the challenge was testing accepting input of various types and them printing out the sum of the output. For strings we concatenate while for integers and floats, we add the mathematically. All these are done using one operator; the ‘+’ sign.
One tip I learnt to help with knowing the data type in python is:
name = 'Muna'
type(name) #result = <class 'str'>
It saves you a lot of time.
My code was as follows:
i = 4
d = 4.0
s = 'HackerRank '
# Declare second integer, double, and String variables.
i_2 = int(input())
d_2 = float(input())
s_2 = input()
# Read and save an integer, double, and String to your variables.
# Print the sum of both integer variables on a new line.
print(i + i_2)
# Print the sum of the double variables on a new line.
print(d + d_2)
# Concatenate and print the String variables on a new line
# The 's' variable above should be printed first.
print(s + s_2)
# Sample Input:
# 12
# 4.0
# is the best place to learn and practice coding!
# Sample Output
# 16
# 8.0
# HackerRank is the best place to learn and practice coding!
This challenge was testing one's skills on input and output, data types and also how to use the '+' operator for mathematical operations as well as concatenating strings.
Posted on June 12, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.