Python How to Read Input as Variable

use raw_input Python

Python is 1 of the easiest languages to larn and use, while at the same time being very powerful. Python is a general-purpose interpreted, interactive, object-oriented, and high-level
programming linguistic communication.

The Python input() and raw_input() functions are used to read data from a standard input such equally a keyboard. This commodity I will show how to use python raw_input role on python 2/python 3 versions with of examples.

Reading Input from Keyboard For Python 2

Python two has two versions of input functions, input() and raw_input().

The input() function treats the received data as string if it is included in quotes '' or "", otherwise the
information is treated as number.

                      In Python 2      >>> historic period = input('How old are yous ?')     How old are you ?: 30            # entered information is treated as number.            >>> print historic period     30      >>> age = input('How old are yous ? :')     How quondam are you ? : '30'            # entered information is treated as string.            >>> print age     'xxx'        

The input('How old are you ?') function ask you about your age, Yous will inter integer value 30 from keyboard, This value will be stored in the age variable as integer. When nosotros print the age variable with print age, The output is integer value 30.

On other hands, the input('How old are you ?') office ask you about your age, Y'all will enter cord 'thirty' value from keyboard, This value will be stored in the historic period variable as string. When we print the historic period variable with impress age part, The output is string value '30'.

The raw_input() function treats the received data equally string even without quotes '' or "".

                      In Python two     >>> age = raw_input("How old are you ? :")    How old are you lot ? : 30            # entered data is treated as string even without ''            >>> print age    '30'      >>> historic period = raw_input("How old are yous ? :")     How sometime are you ? : '30'            # entered data treated every bit cord including ''            >>> print historic period     "'thirty'"                  

The raw_input('How one-time are you ?') function ask yous about your age, Y'all volition enter integer value 30 or cord value 'xxx' from keyboard, This value will be stored in the age variable as string. When we impress the age variable with impress age part, The output is string value '30' or "'thirty'".

Reading Input from Keyboard For Python iii

In Python 3, raw_input() role is deprecated. Farther, the received data is always
treated as string.

                      In Python 3      >>> age = input("How onetime are you ? :")     How sometime are you ? : 30     >>> print(historic period)     'thirty'      >>> historic period = input("How old are you lot ? :")     How old are you ? : '30'            # entered data treated equally cord with or without ''            >>> print(age)     "'30'"      >>> historic period = raw_input("How old are you ? :")            # will result NameError            Traceback (most recent call last):     File "<input>", line 1, in <module>     NameError: name 'raw_input' is not defined                  

The input('How old are yous ?') role inquire you nigh your historic period, You lot will enter integer value xxx or string value '30' from keyboard, This value volition be stored in the age variable as string. When we print the age variable with print(age) function, The output is string value '30' or "'30'".

How To Catechumen String Value To Integer Value

To catechumen cord value to integer value, We will use int() function.

                      In Python ii      >>> age = int(raw_input("How former are you ? :"))     How old are you ? : 30     >>> impress(age)     30      >>> age = int(raw_input("How quondam are you ? :"))     How quondam are y'all ? : 'thirty'     >>> print(age)            Traceback (most contempo call last):     File "<input>", line i, in <module>     ValueError: invalid literal for int() with base of operations xxx: "'xxx'"                  
                      In Python 3      >>> age = int(input("How old are you lot ? :"))     How old are you ? : 30     >>> print(age)     30      >>> age = int(input("How old are you lot ? :"))     How old are you ? : 'thirty'     >>> print(historic period)            Traceback (about recent telephone call last):     File "<input>", line 1, in <module>     ValueError: invalid literal for int() with base 30: "'30'"                  

When nosotros enter digit value, The raw_input() part in python 2 and input() in python three stored it as string value. After that the int() function convert it to integer value. If we enter the digit value between quotes

How To Calculate The Expanse OF Circle

We volition make a simple program to reading input from keyboard and how to execute information technology on Ubuntu eighteen.04, Adding of circle area.

          In Python 2  #!/usr/bin/python import math radius = int(raw_input("Please enter the radius of the circumvolve : ")) area_of_circle = math.pi * radius**2 impress area_of_circle        

We will create file circle_area2.py and shop the above lawmaking within it.

          smart@li615-141:~$ sudo nano circle_area2.py        

To execute this program, Nosotros volition type the control beneath.

          smart@li615-141:~$ sudo python circle_area2.py        
                      In Python 3      #!/usr/bin/python3     import math     radius = int(input("Please enter the radius of the circle : "))     area_of_circle = math.pi * radius**ii     impress(area_of_circle)        

Nosotros will create file circle_area3.py and shop the in a higher place code inside it.

          smart@li615-141:~$ sudo nano circle_area3.py        

To execute this program, We volition blazon the command below.

          smart@li615-141:~$ sudo python circle_area3.py        

Read Also:

  • How to Setup Python Virtual Environs on Ubuntu 18.04
  • How to Install Flask with Python 3 on Ubuntu xviii.04
  • How to Install Latest Python on Ubuntu 18.04

raw_input() and input() functions in python 2 and python iii respectively is very of import to receive the data from the users via keyboard. Likewise we must use int() function to convert the input cord value to integer value.

houstonchomem55.blogspot.com

Source: https://linoxide.com/python-raw-input-function-examples/

0 Response to "Python How to Read Input as Variable"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel