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.
![]() |
KEYBOARD INPUT |
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.
![]() |
|
Data input from the keyboard is implemented through the input() function.
![]() |
input() |
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.
![]() |
|
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.
![]() |
|
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.
![]() |
|
So how will the program work?
s = input("Enter data: ")
Enter data: _
Looks complicated? Just try it, learn how it works. There is nothing you can't understand!
![]() |
INPUT OF A STRING |
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
![]() |
print("s") |
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
print(s)
will cause the error:
NameError: name 's' is not defined
![]() |
OUTPUT OF SEVERAL ARGUMENTS |
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.
![]() |
|
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.
![]() |
|
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'?
![]() |
|
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!
![]() |
print("Hello,",
userName + "!",
"How you doin'?") 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:
userName = input("Enter you name: ")
outString = "Hello, " + userName + "! How you doin'?"
print(outString)Click to look at
![]() |
INPUT OF INTEGERS |
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.
![]() |
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\Input.py", line 1, in 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).
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 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 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-2023 Viktor Trofimov
THE PROGRAM FOR A GRANDMA
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!
Scream of admiration
© 2019-2023 Translation and adaptation by Danil Shentsov
[ Top page ]