[Python] Chapter 02-03 Basics of Python programs (input / output)

[Python] Chapter 02-03 Input / Output

In this section, we will give a basic explanation of output to the screen and input from the keyboard. Output to a file and input from a file are not covered in this chapter and will be described later.

In addition, this time I would like to write a program on the editor of PyCharm and execute it without using Python Console.

This time, we have prepared an exercise at the end, so please try it.

Output to screen

This time we will create a folder and a .py file to enter the program into the editor. For this file, create a chap02 </ font> folder, and create a file called 02-03-01.py </ font> in it. .. I explained how to create it in "Chapter 01-03", but let's check it again.

First, right-click the [Python] folder in the project → press [New] → [Directory]. 1.png

Therefore, create a folder with the name chap02 </ font>.

Next, right-click on the created chap02 </ font> folder and Click [chap02] → [Python File]. 2.png

Therefore, create a file called 02-03-01.py </ font>. 3.png

You can now write your program in PyCharm.

First, enter the code below.

02-03-01.py


print(123)
print('Hello!')
print('Hello!')
print("Welcome to the fun world of programming!")

Then do it. You can do this in the following ways: 4.png

[Execution result] </ font> 123 Hello! Hello! Welcome to the fun world of programming!

I haven't touched on it so far, but you can use the print function to output what is in (). If it is a numerical value, you can enter it as it is, but if it is a character string, enclose it in "'" (single quotation marks) or "" "(double quotation marks).

Escape sequence

By inserting an escape character in the character string, you can insert a line break or a tab. Create a file called 02-03-02.py </ font> in the chap02 </ font> folder, and create the following program. * "" is the "" on the keyboard. </ font> * Please note that some books have "\ n" as "\ n". </ font>

02-03-02.py


print("Welcome,\n To the world of fun programming!")

[Execution result] </ font> Welcome, Enter the world of fun programming!

Others include: (An example)

letter meaning
\n new line
Display of \ symbol
' Display of single quotes
" Double quote display

Add the following program to the file 02-03-02.py </ font> that you just created.

  • Enter "En" for "" to convert.

02-03-02.py


print('Welcome,\n To the world of fun programming!')
print('\It will be ¥ 500')
print('This is Taro\'s pen.')

The "Taro's" part on the third line is marked with "" to distinguish it from the single quotation marks at both ends in () of the print function.

Input from the keyboard

In the previous section, we specified a variable in the program and specified a numerical value or a character string in the program to execute it. However, if it is left as it is, it cannot be executed by specifying a free numerical value or character string, and the program must be rewritten one by one.

Therefore, I will explain how to enter values from the keyboard.

To input on the keyboard, specify as follows.

x = input('Some string here')

Let's actually write the program. Create a file called 02-03-03.py </ font> and write it there.

02-03-03.py


x = input('Please enter a number:')
print(x)

When you execute it, you will be asked to enter it, so enter the number you like.

[Execution result] </ font> Please enter a numerical value: 15 </ font> (← here is keyboard input) 15

Next, consider adding numbers to the two variables x and y, respectively. Please write the following program. Create a file called 02-03-04.py </ font> and write it there.

02-03-04.py


x = input('Enter a number in the variable x:')
y = input('Enter a number in the variable y:')
print(x + y)

And if you enter two numbers, you will get the following result.

[Execution result] </ font> Enter a number in the variable x: 10 </ font> Enter a number in the variable y: 20 </ font> 1020

Originally, the entered numbers should be added, but the entered numbers are concatenated. In fact, ** all keyboard input is a string. ** Therefore, use the ** int function ** to change the string to a number as shown below. (Explained in Chapter 02-02)

02-03-04.py


x = input('Enter a number in the variable x:')
y = input('Enter a number in the variable y:')
print(int(x) + int(y))

[Execution result] </ font> Enter a number in the variable x: 10 </ font> Enter a number in the variable y: 20 </ font> 30

You can also convert a string to a number with the int function at the time of input as shown below.

02-03-04.py


x = int(input('Enter a number in the variable x:'))
y = int(input('Enter a number in the variable y:'))
print(x + y)

[Execution result] </ font> Enter a number in the variable x: 10 </ font> Enter a number in the variable y: 20 </ font> 30

Input and output combination

Consider the following issues and write a program.

Create a program that inputs numerical values to three integer variables x, y, and z and outputs the result of addition (sum) of the three numbers. However, please output so that you can see what was calculated at the time of output. For example, if you enter 2 for x, 4 for y, and 6 for z, you should output "The sum of 2, 4 and 6 is 12."

Set the sum and product variables to wa. The execution result should be as follows.

[Execution result] </ font> Variable x: 2 </ font> Variable y: 4 </ font> Variable z: 6 </ font> The sum of 2, 4 and 6 is 12.

First, think about the program. Create a file called 02-03-05.py </ font> and write it there.

02-03-05.py


x = input('Variable x:')
y = input('Variable y:')
z = input('Variable z:')

wa = int(x) + int(y) + int(z)

print(x+'When'+y+'When'+z+'The sum of' +str(wa)+ 'is.')

I will explain the points. First, since the input x, y, and z are character strings, they are converted to numbers with the ** int function **. Substitute those results in wa.

If you concatenate in the print function as it is, it will concatenate the character string and the numerical value and an error will occur, so convert it with the ** str function ** and then concatenate.

In addition, it is called format, and it can be written as follows when outputting.

02-03-05.py


x = input('Variable x:')
y = input('Variable y:')
z = input('Variable z:')

wa = int(x) + int(y) + int(z)

print(f'{x}When{y}When{z}The sum of{wa}is.')

Practice problem

We have prepared exercises. Please try to solve it. In addition, please use the file name specified in [] and create it in chap02 </ font>. You can specify any variable name you like. [02-03-p1.py] [1] Create a program that inputs numerical values to three integer variables x, y, and z and outputs the result of multiplication (product) of the three numbers. However, please output so that you can see what was calculated at the time of output. For example, if you enter 2 for x, 4 for y, and 6 for z, you should output "The product of 2, 4 and 6 is 48." [02-03-p2.py] [2] Create a program that reads the length and width of the rectangle, calculates the area of the rectangle, and outputs it. [02-03-p3.py] [3] Enter the radius of the circle and create a program that outputs the result of calculating the area of the circle. (Use 3.14 for pi) [02-03-p4.py] [4] Enter the time in seconds from the keyboard and create a program that divides the time into hours, minutes, and seconds as shown in the example below. (Hint: You can use the operator // to calculate the quotient and% to calculate the remainder.)

Enter the time in seconds: 5000 </ font> 5000 seconds is 1 hour 23 minutes 20 seconds.

Finally

Today I learned how to type from the keyboard, including escape sequences. When you input from the keyboard, the input result will be a character string, so don't forget to convert it to a numerical value with the int function, and don't forget to convert it to a character string with the str function when outputting. Let's do it.

Return to [Table of Contents Link]

Recommended Posts