A program is a sequence of statements that directs a computer to do a task. A small program might convert a measurement; another might help you organize personal records. You do not need to be a professional programmer to write one that meets a particular need. section 6.0 section 1.1
To make a first program, turn the task into smaller steps:
- Choose the starting value or get input from a user. section 6.0 section 11.0
- Store values in clearly named variables, then calculate or manipulate the result. section 6.0 section 12.5
- Show the result with print. Test it, and correct unexpected mistakes. section 6.6 section 1.3
Convert minutes to hours and minutes
Start with 123 minutes. Divide by 60 and convert the result to an integer to get 2 whole hours. The remainder after division by 60 is 3 minutes. Print both values. This program has four clear parts: initialize the input, find the hours, find the remaining minutes, and display the answer. section 6.5
Listing 6.2. Convert minutes to hours and minutes using the remainder
minutes_to_convert = 123 #1
hours_decimal = minutes_to_convert/60 #2
hours_part = int(hours_decimal) #3
minutes_part = minutes_to_convert%60 #4
print("Hours")
print(hours_part)
print("Minutes")
print(minutes_part)
Follow the short Python listing from the starting value through the two calculations to the printed result. The % operation supplies the minutes left over. section 6.5
A program can grow with the task. It can ask for names and combine parts of them, read a structured file of contacts for analysis, or respond to clicks in a window. Plan what it must do before writing code, then build and check one part at a time. section 12.5 section 23.0 section 37.5 section 12.2
