You are on page 1of 6

Python: The Basics

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)

Dr. Hugh Melvin, Dept. of IT, NUI,G

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

Dr. Hugh Melvin, Dept. of IT, NUI,G


Basic Terminology
• Testing/Debugging your code (removing errors) is a
vital step
• Types of Errors
1. Syntax Errors
• Structure of code doesn’t follow the ‘rules’ of the language
• Code will not ‘run’ ..Python will give you an error message
• Every language has reserved words (keywords)
• How many in python?
2. Runtime Errors
• Code will run .. But
• Error will appear (at some stage!)
3. Logical/Semantic Errors
• Code runs .. No ‘run-time’ errors.. BUT
• Code does not do what it is supposed to do
Dr. Hugh Melvin, Dept. of IT, NUI,G

Python Keywords

Dr. Hugh Melvin, Dept. of IT, NUI,G

Variables

• Powerful feature of Programming Languages


• Allows you to easily store and manipulate data
• Storage location (address) in memory containing a value
that can change during program execution
• Use an appropriate meaningful name  do not have to
remember address
• Must begin with letter but may contain numbers, underscore
» NumStudent , Num_Student
» InterestRate1, InterestRate

• No Keywords allowed .. Python will advise/complain


• Python supports many different data types

Dr. Hugh Melvin, Dept. of IT, NUI,G


Basic Data Types
• String
» Literally a ‘string’ of characters
» Eg. print “Hello World”

• Numeric
» Integer eg. number of students
» Long Integers eg. Number of Molecules
» Float eg. height 1.87

• Use most appropriate type

Dr. Hugh Melvin, Dept. of IT, NUI,G

Data Types
• Memory requirements of Data Types vary
» Not that significant a factor

• But.. precision is a key factor…

Dr. Hugh Melvin, Dept. of IT, NUI,G

Dr. Hugh Melvin, Dept. of IT, NUI,G


Why?
• Look at binary equivalent of decimal number

• 2147483647 <type 'int'>


» 1111111111111111111111111111111 in binary
» Length 31 bits

• 2147483648 <type 'long'>


» 10000000000000000000000000000000 in binary
» Length 32 bits

• 32 bit number -1 sign bit = 31 bits available


» 20 to 230 max number is 231-1
» Eg. 4 bit number  max = 24 -1 = 15 ( 11112)
Dr. Hugh Melvin, Dept. of IT, NUI,G

Expressions & The Assignment Statement

• The RHS value is assigned to the variable on the LHS


temp1 = 3.0
temp2 = 4
AverTemp = (temp1 + temp2)/2
Name = “Flann O Brien”
Note: temp1,temp2,AverTemp
temp1 temp2 AverTemp are float numeric variable,
variable Name is string variable
• The equals sign does not imply equality - it is the assignment
statement
total = total + 1
The value of variable total on RHS is current value
The value of variable total on LHS is new value
• An expression is a combination of two or more variables and/or
constants with operators
• Note: Equality test uses same character twice ! .. == ..confusion
Dr. Hugh Melvin, Dept. of IT, NUI,G

Assignment Statement

Form variable = expression


variable = constant
variable = variable
…………………….

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

• Hierarchy/Rules of precedence for operators


exponentation **
multiplication * and division /
addition + and subtraction -
Evaluate left to right if equal priority
• Overruled by use of parentheses

Dr. Hugh Melvin, Dept. of IT, NUI,G

Arithmetic Operators and 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

Dr. Hugh Melvin, Dept. of IT, NUI,G

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 :”)

• Will explain difference later..


Dr. Hugh Melvin, Dept. of IT, NUI,G
Data Output
• Have already seen the print statement
• Using commas will print multiple details on same line

Dr. Hugh Melvin, Dept. of IT, NUI,G

Adder

Dr. Hugh Melvin, Dept. of IT, NUI,G

Adder
• Note : Formatting of PRINT statements

Dr. Hugh Melvin, Dept. of IT, NUI,G

You might also like