Last updated: Tuesday 28th February 2023, 13:23 PT, AD




And now for something
completely different . . .

Part 2 - Introduction to Programming

using Python 3

 

2.0 Data processing with Python 3

2.1 A simple programming problem

2.2 Simple math statements

2.3 Simple output statements

2.4 Data types

 

http://www.python.org/about/quotes/

 

2.0 Data processing with Python

 

All data in a Python program has a data type. The most common types of data are text (e.g.  "Tuesday" and "Good day") and numbers (e.g. 56 and 76.9). Text has to be enclosed in matching quote marks (single, double or treble). For example, the following are all examples of valid text values:

 

 

'Hong Kong'

"Hong Kong"

'''Hong Kong'''

(note the treble quotes are typed in using three single quote marks)

 

A Python program might have the following statement:

 

print ("Anne was here")

 

print is one of the built-in functions of the language, and is always shown in purple font in Python 3's IDLE editor (unless you change the editor colors). Keywords of the Python language are shown in orange font. Keywords are words of the Python language which have a special meaning.

 

This is the full set of Python 3's keywords:

 

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

 

The above list was created by running the following program in Python 3's IDLE editor window:

 

import keyword

print (keyword.kwlist)

 

 

When you type in program statements in Python's IDLE editor window and save the file with a name ending in .py, or simply type the line into a Python Shell window, any keywords, for example, import - are shown in orange text, built-in functions, for example, print  - are shown in purple font, and quoted text is always shown in green - see below... 

 

When the statement:

 

print ("Anne was here")

 

is executed by the Python interpreter (i.e. the program runs), this is what is output:

 

Anne was here

 

You run a Python program from the IDLE editor window by selecting Run Module from the Run menu.

 

You can also print out numbers...

 

Consider this Python statement:

 

print (45)

 

When the statement:

print (45)

is executed by the Python interpreter (i.e. when the program runs), this is what is output:

 

45

 

Notice, Python always outputs results in a blue font in a Python Shell window.

 

Data can be input to a Python program and placed into a repository known as a variable:

 

#01-02.py

 

thetext = input("Enter some text ")

print ("This is what you entered:")

print (thetext)

 

 

 

Please note: all Python 3 example programs can be found here:

http://www.annedawson.net/python3programs.html

 

In the program script above, thetext is the name of the variable. You can use any name you like for a variable's name, as long as you don't use one of Python 3's keywords, and the name has no spaces, and starts with a letter.  You can use the underscore character in a variable name, but not as the first character.

 

The following are valid (OK) variable names:

 

thetext

age

salary

tax1

tax2

height

weight

height_in_inches

Weight_In_Pounds

weight_in_pounds

 

The last two variable names are different variable names.

Don't use all capital letters for your variable names.

 

 

 

 

The following are invalid (not OK) variable names:

 

 

2tax             invalid because starts with a number

weight in pounds invalid because you cannot have spaces

_weight         invalid because you cannot start a variable name with a _

 

 

If you've already installed Python on your home computer, or if you're working at the college, you can follow the instructions here to enter and run the following Python program script using Python's IDLE editor.  (Click here if you need to install Python at home.)

 

 

 

#01-02.py

 

thetext = input("Enter some text ")

print ("This is what you entered:")

print (thetext)

 

 

 

Note: in the program above, the first (red) line is a comment,

and has no effect on the execution of the program.

 

The second line is an input statement.

When the second line runs, Python will first output the text:

 

Enter some text

 

and will then wait for you to enter some text.

If you entered the words:  Hello world!

and then pressed the Enter key, Python then will place the text "Hello world!" in into the variable called thetext

Execution of the second line is then complete.

 

 

When the third line runs,

Python will print out to the screen the text:

 

This is what you entered:

 

When the last line runs, Python will print out to the screen the text:

 

Hello World!

 

 

Note: when a variable is used in a print statement such as:

 

print (thetext)

 

its value is printed out to the screen - not its name.

 

 

 

2.1 A simple programming problem

 

The Problem:

Find the average of three numbers

 

Before we can solve this problem using a computer program, you really have to understand what the problem is. Most of us understand how to find the average of three numbers.

 

Image you're trying to explain how to do this to someone who has never done it before. Then you have to break the solution down into simple steps, and place them in the correct order. We usually use pen and paper to do this.

 

Once you have your list of steps, you have produced what is known as an algorithm. Some algorithms are short, some are long, but each step is small...

 


Click here to see an example algorithm to

find the average of three numbers

 

 

 

Once you've written your algorithm, you're ready to think about how to solve the problem on the computer using a language like Python 3.

 

You first need to study the basic syntax (grammar) of the Python language so that you can display text to the screen and get input from the user of your program...

 

 

Find the average of three floating point numbers

 

(e.g. the average of these floating point numbers 10.4, 13.17 and 27.19)

 

 

Using Python

 

How to Create and Run Python Programs using IDLE

Python Program (02-02.py)

Find the average of three numbers:

 

 

#                                                             

# 02-02.py                                                     

# Purpose: to demonstrate storage of a floating point number  

#                                                             

# Programmer: Anne Dawson                                     

# Last updated: Sunday 21st March 2010, 12:45 PT               

#                                                             

# See this resource to find out how the input function works: 

# http://www.annedawson.net/Python3_Input.txt                 

#                                                              

# See this resource to find out how important comments are:   

# http://www.annedawson.net/PythonComments.txt                

#                                                             

number1=float(input("Enter the first number: "))

number2=float(input("Enter the second number: "))

number3=float(input("Enter the third number: "))

total = number1 + number2 + number3

average = total / 3

print ("The average is: ")

print (average)

 

 

 

Click here to see how user input works in Python

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

Important information about use of spaces in a Python program
 

http://www.annedawson.net/python3programs.html

 

 

2.2 Simple math statements

 

result = 1 + 1
result = 2 / 3
result = 3 * 2
students = 10
books = students * 4
x = 5**2
y = (5+9)*(15-7)

 

 

2.3   Simple output statements

 

result = 1 + 1

print (result)


result = 2 / 3

print (result)

average = 37

print ("The average is " + str(average))

2.4   Data types

 

int       (integer, e.g. 12, 14, 101)
string    (text, e.g. "Anne", 'Anne', "Hello World!")
float     (floating point number, e.g. 3.142, 98.6)

Variables in Python

 

A variable is a name that refers to a value.

The assignment statement creates new variables and gives them values:

message = "Hello World!"
print (message)

Legal Variables names in Python:

 

message
result  
student1

student2
student_2
max_temperature
last_name
final_exam_score

Python 3's 33  keywords:

 

False  None  True  and  as  assert  break  class  continue  def  del  elif  else  except  finally  for  from  global  if  import  in  is  lambda  nonlocal  not  or  pass  raise  return  try  while  with  yield

 



Note: keywords cannot be used as variable names

This Presentation uses the following program files:

 


02-01.py

02-02.py

 

 

http://www.annedawson.net/python3programs.html
















 

 

 

End of Python3_Intro.htm