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:
https://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.
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...
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...
(e.g. the average of these floating
point numbers 10.4, 13.17 and 27.19)
#
# 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)
https://www.annedawson.net/python3programs.html
result = 1 + 1
print (result)
result = 2 / 3
print (result)
average = 37
print ("The
average is " + str(average))
2.4 Data types
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
02-01.py