25 C
Jaipur
Wednesday, October 21, 2020

Python User Input – Linux Hint

Must read

Microsoft Edge For Linux (Preview) Arrives: Here’s How To Install

Exactly one month ago, Microsoft officially announced that the Microsoft Edge preview build for Linux would arrive in October and be available for download...

lights buying guide: Things to know about ‘smart lights’ before you buy them this Diwali

Diwali is around the corner. It’s time people shop for lights to decorate and brighten up their homes and give it a festive...

US Justice Department files landmark case against Google, but why it may not hurt the company – Latest News

The U.S. Department of Justice and 11 states filed their long-awaited competition lawsuit against Google - the most momentous antitrust showdown since Washington took...

Python is a flexible, efficient, and easy-to-learn programming language that provides a complete way and the liberty to build dynamic systems. Often, developers need to interact with users. The user might enter some data that is used for processing and calculation purposes. For example, for writing a program in Python that calculates the sum of two values, the user enters the values and the program returns the sum value as an output. In this case, it is necessary to take an input from the user to calculate the sum.

Python allows you to take the input from the user. Python provides two built-in functions for taking inputs from users:

  1. input ()
  2. raw_input ()

In Python 3.6, the input () function is used to take input from users, whereas, in Python 2.7, the raw_input () function is used to take input from users. The examples shown in this article use Python 3.6, and the Spyder3 editor is used for creating and writing the Python scripts.

First, we will discuss the input () function.

Using the input () Function

This section covers the syntax of the input () function. The following is the syntax of the input () function:

input (prompt)

The ‘prompt’ is a string that is displayed on the console that asks the user to enter the value in response. The user-entered input value is then stored into a variable, as follows:

name = input(“Enter your name”)

Whichever name value that the user enters will be stored in the ‘name’ variable. For example:

# user entering the name value
name = input (“Enter your name: “)
#printing the username
print(nThe username is:”,name)

Output

The output is displayed on the Python console.

The user input value is always converted into a string, no matter whether the user enters an integer value, float value, etc. The type () function can be used to determine the type of the user-entered value. The input is taken from the user and the type of the user-entered value will always be a string. Let us see an example of this.

# user entering the name value
value= input (“Enter a string value: “)
#printing the type of value
print(“The type of “,value,” is”, type(value))

# user entering the int value
num= input (“Enter an integer value: “)
#printing the type of num
print(“The type of “,num,” is”, type(num))

# user entering the float value
float_num= input (“Enter an float value: “)
#printing the type of float number
print(“The type of “,float_num,” is”, type(float_num))

# user entering the complex number
complex_num= input (“Enter a complex number: “)
#printing the type of complex number
print(“The type of “,complex_num,” is”, type(complex_num))

Output

The output is displayed in the Python console. In the given output, you can see that the type of every value entered is a string. It does not matter whether the user enters a string value, integer value, float value, or complex number; the type of the user-entered value will always be a string.

Convert User Input into Other Data Types

Although we cannot get an integer, float, or complex number as an input from the user, we can convert the user input value into other above-mentioned data types. For example:

# user entering the name value
value= input (“Enter a string value: “)
#printing the type of value
print(“The type of “,value,” is”, type(value))

# user entering the int value
num= input (“Enter an integer value: “)
#convrting the value into an integer
num=int(num)
#printing the type of num
print(“The type of “,num,” is”, type(num))

# user entering the float value
float_num= input (“Enter an float value: “)
#convrting the value into a floating point number
float_num=float(float_num)
#printing the type of float number
print(“The type of “,float_num,” is”, type(float_num))

# user entering the complex number
complex_num= input (“Enter a complex number: “)
#convrting the value into a complex number
complex_num=complex(complex_num)
#printing the type of complex number
print(“The type of “,complex_num,” is”, type(complex_num))

Output

The output is displayed in the Python console. In the output below, it can be seen that the type of the integer, floating point number, and complex number have now changed.

Conclusion

In this article, you learned about Python user inputs. It is easy to take an input from users in Python. The user-entered value is a string, but you can easily change the data type of the user-entered value. The data type conversion mechanism is briefly explained in the examples provided in this article.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Microsoft Edge For Linux (Preview) Arrives: Here’s How To Install

Exactly one month ago, Microsoft officially announced that the Microsoft Edge preview build for Linux would arrive in October and be available for download...

lights buying guide: Things to know about ‘smart lights’ before you buy them this Diwali

Diwali is around the corner. It’s time people shop for lights to decorate and brighten up their homes and give it a festive...

US Justice Department files landmark case against Google, but why it may not hurt the company – Latest News

The U.S. Department of Justice and 11 states filed their long-awaited competition lawsuit against Google - the most momentous antitrust showdown since Washington took...

Amazon Great Indian sale: 15 power banks, earphones and other gadgets you can buy at Rs 499 or less

Amazon Great Indian Festival sale is on. The e-tailer's biggest annual sale has discounts on almost every category, across segments. For those looking to...