Input and output in Python. Input and print

Pleasant user interaction with Python is one of the many attractive features of the program. Simplicity, modernity, conciseness and understandability - this is what fascinates the software product. The very first lessons for novice users is to learn how to input and output information. In Python, input and print make life easier for developers.

python input

Introduction

Without exception, all software products process and display data. Only in some is the information laid down initially, while in others it comes from outside. You can draw data from files or directly from user-entered text. In the second case, an interactive mode of operation is assumed. It turns out, the user enters information, the program processes it and displays it on the screen. In this case, they say that the code is not closed on itself, but can "communicate" with the external environment. So, to input information in recent versions of Python, input is used.

Information output

To print any data on the screen, the standard built-in print function is used. In the version of "Python" 3, after the word print, there are parentheses in which the displayed information is indicated. In version 2.7, there should be no parentheses after the reserved print command.

In the earlier version, after the word print, writing a variable and putting a comma would mean suppressing the line feed. In the new edition of "Python" with the output command, the variable and end = ”” is indicated in parentheses. This means adding a space instead of a line feed.

In the old version, only the print command is allowed, which means Enter. In the new edition, you must call the function. The print () entry is invalid.

In one command with print in Python, input works. We will talk about this team below.

Entering Information

To enter data from the console, you need the raw_input command (Python 2.7). She displays an invitation to the user, awaits data entry and carries out further work in accordance with the arguments received. In version 3, Python input is used. What is the difference between the two teams?

In earlier versions (2.7), raw_input was needed to enter string data types. If you needed to get a number, then the command was executed with the int () function. This function converted a number from a string to an integer type. In the same version of the input function, Python introduced numerical expressions, and there was no need to use an additional int. Python 3.0 developers removed raw_input and left only input. If you enter a number through input in the new version, you must additionally use int or float to get an integer or real expression. If you use the command with empty parentheses, the program will wait for actions from the user.

According to some experts, using input string in Python is not recommended, because the interpreter can perform syntactic actions through this command. This is fraught with a security breach of program code.

If in Python 3.0 you don’t “digitize” a string, then when performing mathematical operations, the entered data will be added as strings.

raw input python

This is what the correct program fragment should look like using input and numeric data.

python input functions

Tasks with solution and algorithm

1. Create a program that will ask the user his name and age. Display a message that says how old he would be in a century.

Algorithm:

  • display an invitation to enter the name and age;
  • make the necessary mathematical calculations;
  • display a message of the type: “Marina, you will turn 100 in 2099”

python input string

2. Write a program in which 2 lines are given. Swap the first 2 characters of each line.

Algorithm:

  • Assign the sum of slices to the new variable.
  • assign the second variable the sum of the slices;
  • return the value of the function;
  • display the result.

python input

Fixation Tasks

1. Write a code that will search and display the hypotenuse value of a right triangle. The length of the legs is set by the user.

2. Create a program code that will ask the user for the coefficients of the quadratic equation. In the future, the roots of the quadratic equation should be displayed on the screen.

3. Write a program that prompts the user to solve a mathematical example: 4 * 100-54. If the correct answer is entered, a message with congratulations appears. If not, an error notification is displayed. Additionally, you need to redo the program in which in the while loop these actions will be repeated until the user enters the correct answer.

Source: https://habr.com/ru/post/K6401/


All Articles