Python programming: Python -Data input.

0
183
Python programming Python -Data input
Python programming Python -Data input

Python -Data input: To declare the previous program, we just declare some variables, output them. Our program may require some input from the user. Our real-life programs are like RK. 

It is used for input and assigns it to a variable like the following:

var = input()

In the above program, we took some input from the user and put it in the variable named var. Now I can print to see what input I have taken:

var = input()
print (var)

After running the program above, something will be written to the console and press enter. But in our program we have to tell the user what input we have to give. For example, if we want to input the user name, we will write like this:

name = input('Enter your name: ')
print('Hello', name)

If we want to input a number from the user, then we want to print the square of that number. For that, we need to input the number first. In the previous program, we took a string input.

When inputting a number, it will also accept it as a string. For example:

number =  input('Enter a number: ') 
square = number * number
print ('Square is', square)

The above program will not work. Because the number is now a string. Here’s another example:

number1 =  input('Enter 1st number: ') 
number2 =  input('Enter 2nd number: ')  
addition  = number1 + number2
print ('Addition is', addition )

When I run the above program, we will input two numbers. Then add the numbers by adding = number1 + number2. But it didn’t do the string concatenation. Because the number is still a string. For example, if input 12 and 31 will get the following output:

Addition is 1231

For that we need to input and convert it to integers. It’s easy. int (num) By entering any valid number like this, it will convert us to an integer. Our Square Finding Program:

number =  input('Enter a number: ') 
 
number = int(number)
 
square = number * number
print ('Square is', square)

First we input the number. Then with the number = int (number), we input the number and place it again in the number variable. Then I took out the square and printed it at the end.

The program to add:

number1 =  input('Enter 1st number: ') 
number1 = int(number1)
 
number2 =  input('Enter 2nd number: ')  
number2 = int(number2)
 
addition  = number1 + number2
print ('Addition is', addition )

If we do not want to write the line at number = int (number), we can convert the integer to the first line:

number =  int(input('Enter a number: '))
square = number * number
print ('Square is', square)

If we want to input a decimal number, we will convert it to a floating-point number without converting it to an integer. Instead, use float () instead of int ().

If you test a floating-point number or decimal number input in the above addition program, it will show the error. We can also add any decimal number. So I will write the above program as follows:

number1 =  input('Enter 1st number: ') 
number1 = float(number1)
 
number2 =  input('Enter 2nd number: ')  
number2 =float(number2)
 
addition  = number1 + number2
print ('Addition is', addition )

With the above program, any number can now be added.

This time I try to write a more complex program. We will write a program to find out the mass. The user must first ask how many numbers they want to aggregate. Then we will input all the numbers one by one. And finally, print the sum/sum:

For him:

total_input =  int( input('How many number you want to add: ') )
count =1;
total = 0.0;
 
while count <= total_input:
    number =  float(input("Enter number " +  str(count) +":"))
    total +=number
    count +=1
      
print ('Summation is', total )

The program above will extract the sum or sum of all the programs we give. We can find the average if we want. The total number of inputs we have taken for him, after adding all the numbers, dividing the total by dividing the total number:

total_input =  int( input('How many number you want to add: ') )
count =1;
total = 0.0;
 
while count <= total_input:
    number =  float(input("Enter number " +  str(count) +":"))
    total +=number
    count +=1
 
average = total / total_input     
print ('Average is', average )

Note: More Info (Tech24InfoBD) (Python -Data input)

LEAVE A REPLY

Please enter your comment!
Please enter your name here