Python User Input – Linux Hint

Python is a flexible, efficient and easy-to-learn programming language that provides the freedom to build dynamic systems. Developers often need to interact with users. The user can enter certain data used for processing and calculation. For example, to write a Python program that calculates the sum of two values, the user enters the values and the program returns the value of the sum as output. In this case, you must contact the user to calculate the amount.

Python makes it possible to accept user input. Python offers two built-in functions for receiving user input:

  1. Admission
  2. raw_input ()

In Python 3.6 the input () function is used to get data from users, while in Python 2.7 the raw_input () function is used to get data from users. The examples in this article use Python 3.6, and the Spyder3 editor is used to create and write scripts in Python.

First we discuss the input function ().

Using the inputs () Function

This chapter describes the syntax of the input function (). The syntax of the input function () is given below:

Quick

A note is a string of characters that appears in the console and asks the user for a value in response. The value entered by the user is then stored in the variable as follows

Name = Input (Enter your name)

Each value entered by the user is stored in the variable name. For example:

## User InputPrint (Username:,Name)

Get off.

The output is displayed on the Python console.

The value entered by the user is always converted to a string, regardless of whether the user enters an integer value, a value with a floating point, and so on. The function of type () can be used to determine the type of the value entered by the user. The input data comes from the user and the value entered by the user is always a string of characters. Let’s take an example.

#
value= input (string value input: )
# print value type
print (type, value, is, type(value))

# user enters int
num= (enter integer value: )
# print type
(type, number, is, type(number))

#
float_num= input (input float value: )
# type
printout number (type, float_num, is, type(float_num))

# enter complex number
complex_num= input (enter complex number: )
# print complex number type
print(type, complex_num, is, type(complex_num))

Get off.

The output is displayed on the Python console. In this output you can see that the type of each value entered is a string. It does not matter whether the user enters a string value, an integer, a value with a floating point or a complex number; the type of value entered by the user is always a string value.

Conversion of user-entered data to other data types

Although we cannot obtain integer, floating-point or complex numbers as user input, we can convert the user input value to other types of data mentioned above. For example:

#
value= input (string value input: )
# print value type
print (type, value, is, type(value))

#
num= entry (enter integer value: )
# enter value in integer number
num=int(num)
# print type
print (type, number, is, type(number)))

#
float_num= input (input float value: )
# user input float value
float_num=float(float_num)
# print float Type
print(Type, float_num, is, type(float_num))

#
complex_num= entry (enter complex number: )
# user enters value in complex number in
complex_num=complex(complex_num)
# returns type complex number
print(type, complex_num, is, type (complex_num)))

Get off.

The output is displayed on the Python console. In the following conclusions you can see that the type of integer, floating point and complex number has changed.

Conclusion

In this article you have read the information about the connections of users to Python. Under Python it is easy to get information from users. The custom value is a string, but you can easily change the data type of the custom value. The mechanism for converting data types is briefly explained in the examples in this article.

Related Tags:

cybersecurity awareness month ideas,cyber security awareness month 2019,cyber security awareness month 2020 uk,cyber security awareness month australia,its cybersecurity month,cybersecurity awareness month philippines,cyber security awareness month uk,staysafeonline org resources,cybersecurity awareness month canada,cyber security awareness month 2020,cybersecurity awareness month australia 2020,cybersecurity awareness month 2020 theme,national cyber security awareness month 2020 theme,national cybersecurity awareness month 2020 toolkit,cybersecurity week 2020,when is cybersecurity awareness month