Home News Sport Earth Reel Worklife
Home Learn Study support Careers My Bitesize More
KS3
Programming basics
Input and output
Programming is writing computer code to create a
program, in order to solve a problem. To program a
computer, you need to know how programs are
constructed.
Part of Computer Science Programming
Save to My Bitesize
Revise Test
Pages
Arithmetic
Variables
Naming variables
Working with variables
Input and output
Input and output
Programs require data to be input. This data is used (processed) by the
program, and data (or information ) is output as a result.
Data input
Programs are written to solve problems. To solve a problem, a program
needs data input and data, or information, output.
Data can be input in different ways:
Written directly into the program. This is called hard coding.
By the user when the program is running.
From a file or other source when the program is running.
Consider this Python (3.x) program for calculating the perimeter of a
square:
side_length = 5
perimeter = side_length * 4
print(perimeter)
The data in the variable ‘side_length’ has been hard coded, ie it has been
written directly into the program. If we wanted to change this data, we would
have to go back and change the program.
It is usually best to avoid hard coding as much as possible because it is
difficult and time-consuming to rewrite a computer program just to change a
single value.
Consider this alternative Python (3.x) program:
side_length = input("Type in the side length: ")
side_length = int(side_length)
perimeter = side_length * 4
print(perimeter)
This time, the data for the variable ‘side_length’ is input by the user when the
program is running. The statement ‘input’ is used to tell the computer that
the user must enter some data before the program can continue.
Data output
Once data has been processed, programs often need to output the data they
have generated. In Python, the ‘print’ statement is used to output data.
Consider this Python (3.x) program for calculating the perimeter of a square:
side_length = input("Type in a side length: ")
side_length = int(side_length)
perimeter = side_length * 4
print("The perimeter of the square is: ")
print(perimeter)
This program uses the ’print’ statement to:
Display a message explaining what information is being output. Text is placed
within quotes.
Output the contents of the variable ‘perimeter’. Variables are not placed
within quotes.
The print statement uses brackets to surround the data to be printed.
Previous page Next up
Working with variables Test your understanding
More guides on this topic
Introduction to programming Selection in programming
Iteration in programming Boolean logic Arrays and lists
Procedures and functions Writing error-free code
Related links
Jobs that use Computer Science BBC Click BBC Technology news
Tech Tent Headsqueeze Khan Academy Raspberry Pi
Code Club Subscription Educational App Store Subscription
Language:
English
Home Earth Travel Music Sounds
News Reel Culture TV
Sport Worklife Future Weather
Terms of Use About the BBC Privacy Policy Cookies Accessibility Help Parental Guidance
Contact the BBC Get Personalised Newsletters Why you can trust the BBC Advertise with us
Do not share or sell my info
© 2024 BBC. The BBC is not responsible for the content of external sites. Read about our approach to external
linking.