📖
Introduction to programming with Python
  • Introduction to programming with Python 3
  • Preface
    • Common misconceptions about learning to program
    • The Hard truth about learning to program
    • Personal qualities for success
  • 1 - The way of the program
    • Python Programming Language
    • The first program
    • What is debugging?
    • Glossary
    • Exercises
  • 2 - Variables, expressions and statements
    • Values, types and variables
    • Common Built-in data types
    • Expressions
    • Code Format
    • Debugging
    • Glossary
    • Exercises
  • 3 - Functions
    • Python "built-in" Functions
    • Math Functions
    • Composition
    • User Defined Functions
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 4 - Conditionals
    • Boolean expressions
    • Conditional statements
    • PEP 8 Recommendations
  • 5 - Iteration
    • The while statement
    • The for statement
    • break and continue statements
    • Nested Loops
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 6 - A short introduction to testing: Building Reliable Software
  • 7 - A deeper dive into Strings, Lists and Tuples
    • More on Strings
    • More on Lists
    • More on Tuples
    • Debugging
    • Glossary
    • Exercises
  • 8 - A deeper look at Functions in Python
    • Function Preconditions and Postconditions
    • Positional and Keywords arguments
    • Nested Functions
    • Scope of a variable
    • Recursion
    • Functions' side effects
    • Glossary
    • Exercises
  • 9 - Code Documentation
    • Basics of Commenting Code
    • Documenting Code via Python Docstring
  • 10 - Sets and dictionaries
    • Sets
    • Dictionaries
    • Which data structure should I use?
    • Debugging
    • Glossary
    • Exercises
  • 11 - File I/O
    • Read/Write to a file
    • File management
    • Debugging
    • Glossary
    • Exercises
  • 12 - Handling Errors and Exceptions
  • 13 - Writing modules
  • 14 - Classes
    • Classes and Objects
    • Classes and Functions
    • Classes and Methods
    • Pythonic Magic: Understanding and Implementing Dunder Methods
    • Glossary
    • Exercises
  • 15 - Python's Type Hinting
  • Acknowledgements
Powered by GitBook
On this page
Edit on GitHub
  1. 1 - The way of the program

The first program

PreviousPython Programming LanguageNextWhat is debugging?

Last updated 1 year ago

Traditionally, the first program you write in a new language is called "Hello, World!" because all it does is display the words, "Hello, World!"

Before we can write our first program we need to launch the Python interpreter. In this book, we assume you have Python 3 installed on the Windows 10 or later operating system. There are two ways to launch Python:

  • Open a command window, and at the prompt type python. If the interpreter is not started like shown in the figure below, you may want to check that Python is in your PATH environment variable.

  • Open IDLE(Python 3.6 or later) from the Windows start menu. A Python shell opens and is ready to accept your code:

Now that we have a Python shell opened, we can write our program. In Python 3, it looks like this:

>>> print('Hello, World!') 
 Hello, World!
>>>

The quotation marks in the program mark the beginning and end of the text to be displayed; they don't appear in the result.

Now that we can start programming, it might be a good time to illustrate the type of errors we may encounter whilst programming. The first one we mentioned was a syntax error like this one:

>>> 1 + 2) 
 SyntaxError: invalid syntax
>>>

where the opening bracket is missing.

The second type of error are runtime errors like the division by zero shown here:

>>> print(10/0) 
 Traceback (most recent call last): 
     File "<pyshell#1>", line 1, in print(10/0) 
 ZeroDivisionError: division by zero
>>>

Probably the most challenging one is a semantic error. For example, if we try to convert a temperature tFt_FtF​ in Fahrenheit into Celsius the formula to use is:

tC=59(tF−32)\begin{equation*} t_C = \frac{5}{9}(t_F - 32) \end{equation*}tC​=95​(tF​−32)​

Now if we write the following implementation:

>>> print('Fahrenheit 35 in Celsius degree is:', 5/9*35-32) 
 Fahrenheit 35 in Celsius degree is: -12.555555555555554
>>>

the program runs and does not create any error. Does that mean that the program is correct? Unfortunately, NO. The result should have been 1.66 not -12.55.

Exercise

Write the correct implementation of the conversion from Fahrenheit to Celsius.

Answer
print('Fahrenheit 35 in Celsius degree is:', 5/9*(35-32))

In conclusion, it is not because your program runs and returns a result that your work is finished. You must ensure that the returned result is correct. It is essential when implementing a code to define a series of tests (i.e. a series of known outputs given certain inputs) that can validate your code.

Using the Python interpreter from the command shell
Using the Python interpreter via IDLE