30 Days of Code: HackerRank Solutions for Python (Day 1)

Susanna Han
2 min readJan 18, 2021

The one-stop spot for beginners.

Day 1: Data Types

Task
Complete the code in the editor below. The variables i, d, and s are already declared and initialized for you. You must:

  1. Declare 3 variables: one of type int, one of type double, and one of type String.
  2. Read 3 lines of input from stdin (according to the sequence given in the Input Format section below) and initialize your 3 variables.
  3. Use the + operator to perform the following operations:
  4. Print the sum of i plus your int variable on a new line.
  5. Print the sum of d plus your double variable to a scale of one decimal place on a new line.
  6. Concatenate s with the string you read as input and print the result on a new line.

Solution

#There are three variables already declared shown below. i = 4
d = 4.0
s = 'HackerRank '
# variable name = value

You can declare a variable by assigning a value to any relevant variable name using ‘=’.

#Declare second integer, double, and String variables.”i2 = int(input())
d2 = float(input())
s2 = str(input())

To declare a certain data type you can use the built-in functions int( ) for integer, float( ) for any decimal number, and str( ) for a sequence of characters, string. More built-in functions can be found here: https://docs.python.org/3/library/functions.html

# Print the sum of both integer variables on a new line.
print (i+i2)
# Print the sum of the double variables on a new line.
print (d+d2)
# Concatenate and print the String variables on a new line
print (s+s2)

The + operator adds the integers, floats, and joins string variables.

Thanks for stopping by. If you want to see more solutions share and clap for more!

--

--