Data source is a source which gives to a program formalized information, reduced to a certain form in accordance with certain rules.

Formalization means that before requesting data, we should totally understand how the data enters and to which type it belongs to: to text, to numbers, to tables, to HTML-code and so on.

It is also important how we receive information. For example, reading from a file, we can use three ways to obtain data: load the entire file into memory, or load line by line, or character by character. Depending on the chosen method, we write an algorithm for processing this data.

Data sources include:

Despite the diversity of the sources, we will use data input only with a keyboard in this part of the tutorial. In the last part, where we will write a game using Tkinter, the data will be entered with a mouse.


There are two types of data you can get using a keyboard:

The programmer needs to be clearly aware of what type of data he requests from the user. Depending on data type, the part of the code responsible for obtaining information is written.

Everything typed on the keyboard always enters the program in text form (the str type). It's necessary to convert a string to a number if you need to do mathematical operations with data.

Data input from the keyboard is implemented through the input() function.


The input() function returns data that user types with a keyboard. The data is transmitted only when the Enter key is pressed. When data is requested through input(), the typing process isn't controlled at all. It is impossible to check, for example, the correctness of the data entered by the user before the key Enter is pressed.

If we need numbers in the program, and the user stubbornly enters characters, we can’t do anything with it. Only after he calms down and press Enter, the program will receive this data and we will gloatingly inform him that the number was required, not text, and will ask him to enter the data again (normally).

The phrase function returns data means that, having written the name of the function (in our case input ()), we can be firmly convinced that we will get the result of its work. Data return can be compared to a conversation.

You ask a friend:
- When is your birthday?
He answers:
- 16th of October!

Your question is a function here. The response of a friend is a result of the program work and information you will store in memory and will use when necessary.

The function input() returns a "response" to the question "What did the user enter?".

How it looks:

s = input("Enter data: ")

Here s is a variable in which Python will put the result of the input() function - entered characters.

In round brackets we write a prompt. Do not neglect the prompt, each respectable program, when requests data, marks what it means. Moreover, don't forget that the user does not see your program code and does not understand what the program requires of him.

Using of a prompt is necessary. It is written in round brackets after the function name and enclosed in quotation marks.

So how will the program work?

s = input("Enter data: ")

  1. Python will display the message "Enter data: " (without quotation marks) on the screen. To the right of the message the text cursor will blink. It looks like an underscore:

    Enter data: _

  2. At this point, the program will pause, Python will be waiting for user actions
  3. The user will be able to type on the keyboard any phrase. It is possible to edit the message with simple arrows, BackSpace, Delete.
  4. After typing all data, the user will press Enter.
  5. The entered information will be put into the variable s (it is stored by the computer).
  6. The program will continue to work further from the next line of program code.

Looks complicated? Just try it, learn how it works. There is nothing you can't understand!


Let's make an easy task. We will ask the user for a name and display it on the screen. To do it, we will write two simple lines:

s = input("Enter your name: ") print(s)

The result is:

Enter your name: Viktor Viktor

The print(s) function outputs not a character s, but the value of the s variable. Let's write s in quotation marks:

In this case, Python will output the character.

If the s variable is not initialized, or in other words, the value is not assigned to this variable, then an error will occur. The single line program


will cause the error:

NameError: name 's' is not defined


Let's make the previous task a little more complicated. We will ask a user name and output the greeting: "Hello, USER_NAME! How you doin'?".

We have already learned how to receive a user name, now we are going to form the output line with several arguments.

Arguments are the values obtained by the function. For example, the print() function will output everything we put in the brackets. The things we put in the brackets (they may include a variable name, a name of another variable, some text between quotation marks, a formula, etc.) are called (yes) arguments.

Each argument of the print() function are separated with commas. Like the homogeneous parts of the sentence are in the laconic Shakespeare's language.

The general view of the print() command with several arguments is given below.

print("Hello", "How", "You", "Doin'?")

The result is:

Hello How You Doin'?

Note the presence of commas after each part of the phrase.

Arguments are separated with commas.

Now let's get back to our program:

userName = input("Enter your name: ") print("Hello,", userName, "! How you doin'?")

The result is:

Enter your name: Viktor Hello, Viktor ! How you doin'?

Pay attention to the spelling the userName variable. The word "Name" is written with a capital letter, because it is the second word in the identifier. However, it doesn't really matter for Python. It is customary for professional programmers to write every new word with a capital letter.

And we have one more problem now. The fact is that separating an exclamation mark and a name (and any word) by space does not meet the standards. It is inappropriate to write "Hello, Viktor !". It must look like "Hello, Viktor!". The exclamation mark must be close to a word.

It happens because print() puts a space by default between comma-separated arguments. Let's fix the program using concatenation (i.e. line addition):

userName = input("Enter your name: ") print("Hello,", userName + "!", "How you doin'?")

The result is:

Enter your name: Viktor Hello, Viktor! How you doin'?

Bingo! The irrelevant space is removed!

Although each output argument is written in a new line:

print("Hello,", userName + "!", "How you doin'?") can easily write them in one line. The main thing is not to get confused with commas:

print("Hello,", userName + "!", "How you doin'?")

You can even assign the output string to a separate variable. Think about how to do it.

Assignment string to a separate variable:

Click to look at

userName = input("Enter you name: ") outString = "Hello, " + userName + "! How you doin'?" print(outString)


The data received with input() always belongs to the string str type of data. We can't implement math operations with string data. We can't subtract or divide it. Even if we put numbers into string variables, it won't work out:

a = input("Enter a number: ") b = input("Enter a number: ") sum = a + b print("The sum of the numbers is", sum)

The result is:

Enter a number: 35 Enter a number: 15 The sum of the numbers is 3515

The concatenation has just worked out. You will ask, "And what to do?". And I'll answer, "Convert the type!".

If we need to get an integer, then we'll use the int() function which will try to convert a string to an integer.

Why will it "try"? Because if the string contains letters or punctuation, then Python will throw an error. That is, int("100") will work, but int("Hello!") will ruin the program.

To convert a string to a number, use int() specifying a string with an integer in brackets as an argument. For example, to convert an input string you should write

a = int(input("Enter a number: "))

Now let's rewrite the program:

a = int(input("Enter a number: ")) b = int(input("Enter a number: ")) sum = a + b print("The sum of the numbers is", sum)

The result is:

Enter a number: 35 Enter a number: 15 The sum of the numbers is 50

Bingo! Now Python can work with the entered values, taking them as the numbers, in accordance with the rules of mathematical expressions.

But if the user turns out to be insidious, cunning and enters a fractional number, then Python will throw an error:

Enter a number: 3.14 Traceback (most recent call last): File "D:\Python\", line 1, in a = int(input("Enter a number: ")) ValueError: invalid literal for int() with base 10: '3.14'

So far, we can't avoid this. Therefore, check input data carefully. Verification will be set later.


Now we will write the program discussed in the previous chapter. It will help grandmother to calculate the area of the rectangle (the room, the plot of land - select the relevant option).

Friendly advice: when you write a program, ask yourself, will your grandma be able to use your program? Or your two-year-old brother? Your task is to write simple and clear programs with a simple and clear interface. The user should not guess what you want from him. The user should see what is necessary and shouldn't see unnecessary things.

I thought a little. What if a granny who begins to read this tutorial doesn't know the meaning of the word "interface"? So, especially for you, Grandma:

The interface of the program is a tool for user interaction with the program. Usually the graphic part is singled out. It includes all buttons, by clicking on which you can control the program, all input fields where it's necessary to enter values. That is, it includes all graphical control elements (also known as graphical widgets).

For example, the interface of your smartphone consists of all icons located on the screen. They should be comfortable, understandable, accessible, pretty. In spite of its apparent simplicity, the interface development is hard work. Don't forget that all control elements should be clear... to a granny!

The area of something for a grandma. Source code is below:

print("Hello, Dear Grandma!") print("I wrote a program specifically for you.") print("It can calculate the area of the room.") print("Just enter necessary numbers (integers!) and press the Enter key.") a = int(input("Enter the length of the room: ")) b = int(input("Enter the width of the room: ")) result = a * b print("The area of the room =", result)

The result is:

Hello, Dear Grandma! I wrote a program specifically for you. It can calculate the area of the room. Just enter necessary numbers (integers!) and press the Enter key. Enter the length of the room: 4 Enter the width of the room: 3 The area of the room = 12

Scream of admiration

Pals, this program is perfect!

If you see and understand what blocks we have just talked about and where they are, you have great prospects ahead! If there are some difficulties, please, go back and reread the chapter "Data processing".

© 2019-2021 Viktor Trofimov
© 2019-2021 Translation and adaptation by Danil Shentsov
[ Top page ]