Basic questions in python

Basic Questions in Python.

#Basic questions in python

1) Write a program to take the number of kilometers as input and convert into meters and print the      number of meters.

SOLUTION:
#taking kilometers as input of float data type
given_kilometers = float(input())
#converting kilometers to meters
meters = given_kilometers*1000
#printing the meters as output
print(int(meters))

INPUT: 1.2

OUTPUT: 1200

2.Write a program that reads a percentage P and prints the percentage P of the number 200.
SOLUTION:

#Taking input containing an integer.
a = int(input())
#Total value.
b = 200
#converting into percentage.
c = (a/100)*b
#printing the result as float in the output.
print(float(c))

INPUT: 50
OUTPUT: 100.0

3.Given a word W and an integer N, write a program to print the character present at the index N in the word W.

SOLUTION:


#Taking a word as first input.
word = input()
#Taking a number as second input.
number = int(input())
#indexing
index = word[number]
#printing index as output
print(index)

INPUT:
1.MOHAN
2.  2
OUTPUT:
H

4.Given a word and a number N, write a program to print the given word, N number of times in a single line.

SOLUTION:
#Taking a string as first input
string = input()
#Taking a number to repeat string as second input
number =int(input())
reapeated_string = string*number
#printing reapeated_string as output
print(reapeated_string)

INPUT:
1.Yashu
2. 3
OUTPUT:
YashuYashuYashu

moyasite

5.Write a program to read a single line of input and print the first three characters in it.

SOLUTION:


#Taking a word as input
word = input()
#first three leters
final_word = word[:3]
#printing the output
print(final_word)

INPUT:
1.Four

OUTPUT:
Fou

6.Write a program that reads a word and prints the word excluding the fourth letter of the word.

SOLUTION:

word = input()
befor_letter = word[0:3]
fourth_letter = word[4:]
print(befor_letter + fourth_letter)

INPUT:
1.Equality
OUTPUT:
Equlity
7.Write a program that reads a word and prints the first two and the last two letters of the word and prints the stars (*) instead of the remaining letters.
SOLUTION:

word=input()
length=len(word)
no_of_stars=length-4
first_two=word[:2]
last_two=word[length-2:]
middle_one="*"*no_of_stars
print(first_two+middle_one+last_two)

INPUT:
1.Message
OUTPUT:
me***ge

8.Write a program that reads two numbers A and B, and checks if both A and B are greater than 35 or A is greater than B.

SOLUTION:
number=input()
number=int(number)
number2=input()
number2=int(number2)
first=(number > 35) and (number2 > 35) or (number)>(number2)
print(first)

INPUT:
1.45
2.25
OUTPUT:
TRUE

9.Write a program that reads two numbers A and B, and checks if both A and B are positive numbers or both A and B are less than 70.

SOLUTION:

first_num = input()
first_num = int(first_num)
second_num = input()
second_num = int(second_num)
positive = (first_num > 0) and (second_num > 0)
less = (first_num < 70) and (second_num < 70)
result = positive or less
print(result) 

INPUT:
1.200
2.50
OUTPUT:
True

10.Write a program that reads three numbers A, B, and C, and checks if the sum of any two numbers is always greater than 10.

SOLUTION:

num1=int(input())
num2=int(input())
num3=int(input())
first=(num1+num2)>10
second=(num2+num3)>10
third=(num3+num1)>10
print(first and second and third) 

INPUT:
1.4
2.10
3.18
OUTPUT:
True

 

11.Write a program that reads a three-digit number and checks if all the digits of the number are the same.

SOLUTION:

number = input()
first_digit = number[0]
second_digit = number[1]
third_digit = number[2]
result = first_digit == second_digit == third_digit
print(result)

INPUT:
1.222
OUTPUT:
True
12.Write a program that reads two numbers A and B and prints the greatest among the two numbers.
SOLUTION:
first_number = int(input())
second_number = int(input())
if first_number > second_number:
    print(first_number)
else:
print(second_number)
INPUT:
1.4
2.3
OUTPUT:
4

Leave a comment