Python Programming Practice-Python input

Created with Sketch.

Python Programming Practice-Python input

The input function is a simple way for your program to get information from people using your program. Here is an example:

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

 

The basic structure is

variable name = input ( message to user )

 

The above works for getting text from the user. To get numbers from the user to use in calculations, we need to do something extra. Here is an example:

num = eval ( input ( ' Enter a number: ' ))
print ( ' Your number squared: ' , num*num)

 

The eval function converts the text entered by the user into a number. One nice feature of this is you can enter expressions, like 3*12+5 , and eval will compute them for you.

Note If you run your program and nothing seems to be happening, try pressing enter. There is a bit of a glitch in IDLE that occasionally happens with input statements.

Python Programming-Python Print

Here is a simple example:

print ( ' Hi there ' )

 

The print function requires parenthesis around its arguments. In the program above, its only argument is the string ‘ Hi there ‘ . Anything inside quotes will (with a few exceptions) be printed exactly as it appears. In the following, the first statement will output 3+4 , while the second will output 7 .

print ( ' 3+4 ' )
 print (3+4)

 

To print several things at once, separate them by commas. Python will automatically insert spaces between them. Below is an example and the output it produces.

print ( ' The value of 3+4 is ' , 3+4)
 print ( ' A ' , 1, ' XYZ ' , 2)

 

 

The value of 3+4 is 7 A 1 XYZ 2

 

Leave a Reply

Your email address will not be published. Required fields are marked *