Download: http://www.ucalgary.ca/it/software/downloads (SSH comes with MacOS so no download is needed.
(SSH still needs to be installed but it is far easier to install SSH than it is to install and setup Python).
Alternative (not recommended): Getting Python (get version 3.X and not version 2.X)
Requires that Python is configured (the “path”) on your computer (it is not mandatory to install Python at home, follow these instructions carefully, missteps occur at your own peril!)
Working At Home (2)
(If you have installed Python on your own computer and still can’t get ‘Python’ to run – this approach works although it’s a ‘inelegant’ solution).
Note where you installed Python (folder or directory)
Create and run your Python programs from this location.
Basic explanation of concepts (for beginners: along with examples to illustrate)
More advanced information about Python libraries (more advanced: useful for looking up specific details of pre-created Python functions after you have a general idea of how things work e.g., what is the exact wording of the function used to open a file).
General help (includes the above two help links and more):
The Process Of Creating A Computer Program
A person (programmer) writes a computer program (series of instructions).
The program is written and saved using a text editor.
The instructions in the programming language are high level (look much like a human language).
A special computer program (translator) translates the program written by the programmer into the only form that the computer can understand (machine language/binary)
The machine language instructions can now be directly executed by the computer.
The process is similar on other platforms/OS’s (the TA’s will show you how to do it on the lab computers during tutorials).
You need a text editor (e.g., WordPad, Notepad) to enter the program.
It can be done using any editor that you, want but don’t use a word processor (e.g., MS-Word) and remember to save it as a text file.
Also you need to open a command line to translate/run your Python program.
Creating Programs: One Operating System
To translate/run your program type “python ” at the command line.
The first example program would be executed by typing “python small.py”
For a program whose filename is called “output1.py” you would type “python output1.py”.
If you didn’t catch it: Make sure the exact and complete filename is entered (even the “dot-py” suffix).
The computers in the Computer Science lab have already been set up so don’t need to do any installing or pre-configuring of Python.
This applies even if you work remotely from home using a program such as SSH.
On the Computer Science computers: make sure you are running the correct version of python by typing “python3” (not just “python”)
E.g., “python3 small.py”
Creating Programs: One Operating System (2)
Working on your own computer: When you translate/run your program in the command window make sure that your command line is in the same location as your Python program (‘inelegant but works’).
Alternatively you have set up your computer so it ‘knows’ where python has been installed (e.g., setting the ‘path’ in Windows).
See earlier slide: “…follow these instructions carefully, missteps occur at your own peril!”
The computers in the CPSC lab have already been set up properly.
The Python program is in another location.
Make sure you type the whole file name (including the part after the period) when you translate/run your program.
In the computer lab type: “python3 filename.py”
On your home computer type: “python filename.py”
When you work remotely via a program such as SSH type: “python3 filename.py”
Displaying String Output
String output: A message appears onscreen that consists of a series of text characters.
Whatever is contained with the quotes (single or double) is what appears onscreen.
Don’t mix and match different types of quotation marks.
print ("the message that you wish to appear")
print ('the message that you wish to appear')
Set aside a location in memory.
Used to store information (temporary).
This location can store one ‘piece’ of information.
Putting another piece of information at an existing location overwrites previous information.
At most the information will be accessible as long as the program runs.
Some of the types of information which can be stored in variables include: integer (whole), floating point (fractional), strings (text)
<name of variable> = <Information to be stored in the variable>
Integer (e.g., num1 = 10)
Floating point (e.g., num2 = 10.0)
Strings (e.g., name = "james")
Picture from Computers in your future by Pfaffenberger B
The assignment operator '=' used in programming languages does not have the same meaning as mathematics.
Don’t mix them up!
y = 3
x = y
x = 6
y = 13
What is the end result? How was this derived (what are the intermediate results)?
Variable Naming Conventions
Rules built into the Python language for writing a program.
Somewhat analogous to the grammar of a ‘human’ language.
If the rules are violated then the typical outcome is the program cannot be translated (nor run).
A language such as Python may allow for a partial execution (it runs until the error is encountered).
Approaches for producing a well written program.
(The real life analogy is that something written in a human language may follow the grammar but still be poorly written).
If style requirements are not followed then the program can still be translated but there may be other problems (more on this during the term).
Variable Naming Conventions (2)
Style requirement: The name should be meaningful.
Style and Python requirement: Names must start with a letter (Python requirement) and should not begin with an underscore (style requirement).
Style requirement: Names are case sensitive but avoid distinguishing variable names only by case.
Style requirement: Variable names should generally be all lower case (see next point for the exception).
Style requirement: For variable names composed of multiple words separate each word by capitalizing the first letter of each word (except for the first word) or by using an underscore. (Either approach is acceptable but don’t mix and match.)
Python requirement: Can't be a keyword (see next slide).
On the computer all information is stored in binary (2 states)
Example: RAM/memory stores information in a series of on-off combinations
Variables: Storing Information (2)
Information must be converted into binary to be stored on a computer.
Can be stored in the computer as
Storing Integer Information
1 bit is used to represent the sign, the rest is used to store the size of the number
Sign bit: 1/on = negative, 0/off = positive
Digits representing the size of the number
Size of number, in this case = 13
Storing Real Numbers In The Form Of Floating Point
Mantissa: digits of the number being stored
Exponent: the direction and the number of places the decimal point must move (‘float’) when storing the real number as a floating point value.
Examples with 5 digits used to represent the mantissa:
e.g. One: 123.45 is represented as 12345 * 10-2
e.g. Two: 0.12 is represented as 12000 * 10-5
e.g. Three: 123456 is represented as 12345 * 101
Remember: Using floating point numbers may result in a loss of accuracy (the float is an approximation of the real value to be stored).
Storing Character Information
Typically characters are encoded using ASCII
Each character is mapped to a numeric value
E.g., ‘A’ = 65, ‘B’ = 66, ‘a’ = 97, ‘2’ = 50
These numeric values are stored in the computer using binary
ASCII numeric code
Storing Information: Bottom Line
Why it important to know that different types of information is stored differently?
Certain operations only apply to certain types of information and can produce errors or unexpected results when applied to other types of information.
num = input("Enter a number")
numHalved = num / 2
Converting Between Different Types Of Information
Example motivation: you may want numerical information to be stored as a string (for the formatting capabilities) but also you want that same information in numerical form (in order to perform calculations).
Some of the conversion mechanisms available in Python:
int (<value to convert>)
float (<value to convert>)
Program name: convert1.py
x = 10.9
y = int (x)
print (x, y)
Converting Between Different Types Of Information (2)