Python: The Basics: - Program
Python: The Basics: - Program
Basic Terminology
• Program
» Sequence of HLL instructions that specifies how to perform a
computation
»Follows from your algorithm
• Input
» Reading in data from keyboard or file or other device (process control)
• Output
» Display/output data to a screen, file or other device (process control)
Basic Terminology
• Recall Python allows you to use Shell or Script mode
• Shell
» Interactive.. Type in instructions and press return key
»Python’s Interpreter attempts to ‘interpret’ your instructions
»If successful, it gives you the result
• Script
» You write all your instructions into a file
» This is termed a computer program eg. AddNumbers.py
» You then ‘RUN’ the program
»This can be done from command line by telling Python what file to ‘run’
»..or can use IDLE
»Python’s default Integrated Development Environment
»Graphical User Interface
Python Keywords
Variables
• Numeric
» Integer eg. number of students
» Long Integers eg. Number of Molecules
» Float eg. height 1.87
Data Types
• Memory requirements of Data Types vary
» Not that significant a factor
Assignment Statement
Action
Evaluates the expression on the RHS and
assigns its value to the variable on the LHS
X = 7 + 1
Examples
StudAge = 15
AverTemp = (temp1 + temp2)/2
Dr. Hugh Melvin, Dept. of IT, NUI,G
Arithmetic Operators & Expressions
• Total = 3 + 4 * 2 3 + 8 =11
• Total = (3 + 4) * 2 7 * 2 = 14
• Total = 3 * 4 / 2 * 3 12 / 2 * 3 6 * 3 =18
(evaluate left-right)
• Total = (2 + (3 + 6) / 3) + 5 ** 2
( 2 + 9 / 3) + 5 ** 2 inner ( )
( 2 + 3 ) + 5** 2 div before add
( 5 ) + 5** 2 parenthesis
5 + 25 expon
= 30
Data Input/Output
• Need to read in data and output results for program to
be useful
• Input: Assign data to variables within a program
• Two Python functions for getting keyboard input
• input
» temp1 = input (“Enter temperature number 1 :”)
» Include a user friendly prompt in quotation marks
» Use assignment statement
• raw-input
» temp1 = raw_input (“Enter temperature number 1 :”)
Adder
Adder
• Note : Formatting of PRINT statements