Chapter 6 - Using Print and Input in Python



AD

6.1.0 Simple Programs Using Print & Input

6.2.0 Print statement

We will often use print functions to print out the value of a variable at a point in our program. It is especially useful for debugging. A simple program is as follows:

x=5
print('x = ' , x)
"""
Sometimes you will see comments like
this with triple quotes, instead of the # sign.
"""

With the result:

x = 5

I have given an example of a comment with triple quotes, instead of the # sign, in case you see it in other texts.

6.3.0 Input statement

We also need to input values. A simple input function follows:

myinteger = input("Input an integer: ")
print('myinteger= ' ,myinteger)
myinteger = int(myinteger)

# Output
# Input an integer: 8
# myinteger= 8

Copy and paste this to PyCharm, and run the program. The words “Input an integer” will appear in the lower output window. You must type in the integer “8” and press enter for “myinteger= 8” to appear. It is important to keep your cursor pointed in the lower left code box when entering the number, or you will type in the wrong spot.

Please note that 8 is not an integer, it is a string. To make the input a float or an integer, we must specify. The same is true for a float number.

mynumber = input("Input a float number: ")
print('mynumber= ' ,mynumber)
mynumber = float(mynumber)

# Output
# Input a float number: 16.1
# mynumber = 16.1





Engineering-Python

SALARSEN.COM
Table of Contents
Ch1-Install Python
Ch2-Install PyCharm
Ch3-Save Work
Ch4-Add Project
Ch5-Variables
Ch6-Print&Input
Ch7-Lists
Ch8-Loops
Ch9-If&Logical
Ch10-Functions
Ch11-Bubble Sort
Ch12-Plotting
Ch13-Files
Ch14-Print Format
Ch15-Dict&Comp&Zip
Ch16-Arrays
Ch17-Electrical
Ch18-Regression
Ch19-Differential
Ch20-Secant