0% found this document useful (0 votes)
142 views

Input Print Print O/p:: "Enter Name: " "First Name: " "Last Name: "

The document provides Python code examples for various input/output operations: 1) It shows examples of splitting input strings, getting lists of inputs, using map functions to cast strings to integers, calculating the area of an equilateral triangle, and defining functions. 2) It then discusses 9 code kata problems from GUVI involving getting different input formats and printing the corresponding output formats. 3) The last two problems demonstrate getting input for calculating simple interest and solving a quadratic equation, rounding outputs to two decimal places.

Uploaded by

Priya Sivakumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views

Input Print Print O/p:: "Enter Name: " "First Name: " "Last Name: "

The document provides Python code examples for various input/output operations: 1) It shows examples of splitting input strings, getting lists of inputs, using map functions to cast strings to integers, calculating the area of an equilateral triangle, and defining functions. 2) It then discusses 9 code kata problems from GUVI involving getting different input formats and printing the corresponding output formats. 3) The last two problems demonstrate getting input for calculating simple interest and solving a quadratic equation, rounding outputs to two decimal places.

Uploaded by

Priya Sivakumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

My GitHub – https://github.

com/rufeeya

Google Collab – Welcome To Colaboratory - Colaboratory (google.com)

1) Split example
first_name, last_name = input("Enter Name : ").split()
print("First Name: " + first_name)
print("Last Name: ", last_name)
o/p:
Enter Name : Hema Divya
First Name: Hema
Last Name: Divya
2) Get List of inputs
#list = ["apple", "banana", "cherry", "apple"]
list1 = input("Enter space separated fruit names: ").split()
print("The List is: ", list1)
o/p:
Enter space separated fruit names: apple banana cherry apple
The List is: ['apple', 'banana', 'cherry', 'apple']
3) Map

#Map function : perform operation on each item and return
#list_num = input("Enter Numbers: ").split()
#print("Sum: ", sum(list_num)) Type error, because it is list of string
#list_numbers = map(int, input("Enter Numbers: ").split()) # working withou
t casting to list as well
list_numbers = list(map(int, input("Enter Numbers: ").split())) #Convert ma
p to list
print("Sum: ", sum(list_numbers))
o/p:
Enter Numbers: 1 2 3 4
Sum: 10 

4) Calculate eq triangle
#1/4(sqrt(3a^2))
side = int(input())
area = (1/4)*((3**0.5)*(side**2)) #Double asterisk( ** ) for exponent ( ^ )
print(round(area,2))
o/p:
20
173.21

5) Functions

#Functions
def sum():
  a = float(input())
  b = float(input())
  c = a+b
  print(round(c,1))
sum()
o/p:
3.6
2
5.6

GUVI Code Kata -

1) Write a code to get the input in the given format and print the output in the given format [ Not Submitted, Email sent
7th question of Input output ]

Input Description:
A single line contains a string.

Output Description:
Print the characters in a string separated by space.

Sample Input :
guvi
Sample Output :
guvi
Solution

userInput = input()

#By Default, print takes to new line, extra parameter end = “ “, to specify break by space

for a in userInput:

print( a, end = " ")

Note- Testcase did not pass because space might have been appended in the end. Check solution 4, where
test cases passed

SOLUTION [ FROM GUVI ]:


word=str(input()) #get the input
print(*word, sep=" ") #print by using space separator

2) Write a code to get the input in the given format and print the output in the given format. [ I/O 8 th ques ]

Input Description:
A single line contains three float values separated by space.

Output Description:
Print the float value separated by line.

Sample Input :
2.3 4.5 7.8

Sample Output :
2.3
4.5
7.8

Solution

userInput = input()

nums = userInput.split(' ')

print( float(nums[0]) )

print( float(nums[1]) )

print( float(nums[2]) )

3) Write a code to get the input in the given format and print the output in the given format.

Input Description:
A single line contains a string.

Output Description:
Print the characters in a string separated by line.

Sample Input :
guvigeek

Sample Output :
g
u
v
i
g
e
e
k

Solution

userInput = input()

for a in userInput:

print(a)

4) Write a code to get the input in the given format and print the output in the given format.

Input Description:
A single line contains a string.

Output Description:
Print the characters in a string separated by comma.

Sample Input :
guvi
Sample Output :
g,u,v,i

Solution

userInput = input()

i=0

length = len(userInput) #length of string

for a in userInput:

if( i < (length-1)):

print( a, end = ",")

else:

print( a )

i=i+1

5) Write a code to get the input in the given format and print the output in the given format

Input Description:
First-line indicates two integers separated by space. Second-line indicates three integers separated by space. Third-line
indicates three integers separated by space

Output Description:
Print the input in the same format.

Sample Input :
25
256
245

Sample Output :
25
256
245

Solution

userInput1 = input()

userInput2 = input()

userInput3 = input()

line1 = userInput1.split(' ')


print( int(line1[0]) , end = ' ')

print( int(line1[1]) )

line2 = userInput2.split(' ')

print( int(line2[0]) , end = ' ')

print( int(line2[1]) , end = ' ')

print( int(line2[2]) )

line3 = userInput3.split(' ')

print( int(line3[0]) , end = ' ')

print( int(line3[1]) , end = ' ')

print( int(line3[2]) )

6) Write a code to get the input in the given format and print the output in the given format

Input Description:
Three integers are given in line by line.

Output Description:
Print the integers in a single line separate by space.

Sample Input :
2
4
5

Sample Output :
245

Solution

userInput = input()

userInput1 = input()

userInput2 = input()

print( int(userInput), end =' ')

print( int(userInput1), end =' ')

print( int(userInput2))

7) Write a code to get the input in the given format and print the output in the given format

Input Description:
A single line contains integers separated by space

Output Description:
Print the integer list of integers separated by space
Sample Input :
2345678

Sample Output :
2345678

Solution

userInput = input()

i=0

length = len(userInput) #length of string

for a in userInput:

if( i < (length-1)):

print( a, end = "" )

else:

print( a )

i=i+1

8) Write a code to get the input in the given format and print the output in the given format.

Input Description:
First-line indicates two integers which are the size of array and 'K' value. Second-line indicates an integer contains
elements of an array.

Output Description:
Print the taken input in the same format.

Sample Input :
53
12345

Sample Output :
53
12345

Solution

userInput = input()

userInput2 = input()

i=0

length = len(userInput) #length of string

for a in userInput:

if( i < (length-1)):

print( a, end = "" )

else:
print( a )

i=i+1

i=0

length = len(userInput2) #length of string

for a in userInput2:

if( i < (length-1)):

print( a, end = "" )

else:

print( a )

i=i+1

9) Write a code to get the input in the given format and print the output in the given format

Input Description:
First-line indicates two integers separated by space. Second-line indicates two integers separated by space. Third-line
indicates two integers separated by space.

Output Description:
Print the input in the same format.

Sample Input :
24
24
24

Sample Output :
24
24
24

Solution

userInput = input()

userInput1 = input()

userInput2 = input()

print(userInput.split(' ')[0], userInput.split(' ')[1], sep= " ")

print(userInput1.split(' ')[0], userInput1.split(' ')[1], sep= " ")

print(userInput2.split(' ')[0], userInput2.split(' ')[1], sep= " ")

10) You are given with Principle amount($), Interest Rate(%) and Time (years) in that order. Find Simple Interest.

Print the output up to two decimal places (Round-off if necessary).


(S.I. = P*T*R/100)

Input Description:
Three values are given to you as the input. these values correspond to Principle amount, Interest Rate and Time in that
particular order.

Output Description:
Find the Simple interest and print it up to two decimal places. Round off if required.

Sample Input :
1000 2 5

Sample Output :
100.00

Solution

userInput = input()

p, n , r = map(float, userInput.split())

si = (p*n*r)/100

print(round(si,2))

11) You are given the coefficients of a quadratic equation in order A, B & C.

Where A is the coefficient of X2,  B is the coefficient of X and C is the constant term in the most simplified form.

Example: For  X2 + 5X + 6 = 0, you are given the input as: 1 5 6.

Write a program to find all of the roots of the quadratic.

Note: The output should be up to 2nd decimal place (round off if needed) and in case of a recurring decimal use
braces i.e. for eg: 0.33333..... => 0.33.

Note: Use Shri Dharacharya's Method to solve i.e. X = {-b + √(b² - 4ac) } / 2a & {-b-√(b² -4ac)} / 2a

Input Description:
Three numbers corresponding to the coefficients of x(squared), x and constant are given as an input in that particular
order

Output Description:
Print the two values of X after rounding off to 2 decimal places if required.

Sample Input :
1 2 -3

Sample Output :
1.00
-3.00

Solution

userInput = input()

a, b, c = map(float, userInput.split())

root1 = round((-b + (((b**b) - (4*a*c)) ** 0.50))/(2*a),2)

root2 = (-b - (((b**b) - (4*a*c)) ** 0.50))/(2*a)


print( "%.2f" % root1 ) // Instead of round, because we want .00 also to display fine

print( "%.2f" % root2 )

Another method for printing decimal –

print( "%.2f" % root1 )

12)

You might also like