When given a problem, we convert it into an algorithm. Break down problem into sequence of steps.
Algorithm is a finite set of steps that specifies a sequence of operations to be carried out in order to solve a specific
problem.
A computer program is an algorithm defined in a programming language.
Program is a sequence of instructions that performs a task.
Natural Langs. English, spoken by humans
Formal Langs. Math Notations, chemical structures, programming languages.
Programs are deterministic, results well defined, predictable
Programming languages
Machines only execute low-level languages, high-level languages (python) need to be processed to be executed
which takes some time, disadvantage for high-level langs.
Easier to program high-level, less time to write, shorter, easier to read, more likely to be correct, easy to maintain.
High-level langs. Are portable. Portable: Run on different kinds of computers with few, no modifications. Low-level
only can run on one type of computer, must be rewritten for other. Most programs written in high-level, low-level
only for specialised applications.
Two types of programs to process high-level langs. Interpreter and Compiler.
Interpreter reads program, executes. It does what program says. It processes it little at a time, alternate line read
and computations.
Compiler reads program, translates completely, then executes. High-level program (source code) gets complied into
object code/executable (low level). Then it can be executed repeatedly without further translation.
Python Interpreter
Source Code (Programs) are written in a programming language
Python interpreter translates and executes source code, into machine-code called Bytecode
, Variables
Any name works with some restrictions (not function name, no number first, no operator, no spaces)
Store only one variable at a time
Can be assigned to values. (=)
Cannot be any keyword
and elif import raise as else in return assert except is try break finally lambda
while class for nonlocal with continue from not yield def global or del if pass
Python is case sensitive, use lowercase for variables
Pythons style guide, conventions etc
Four basic types of info
Integer
Floating point
String
Boolean
Integer can be + 0 or -, no decimal
Floating point numbers have decimal point. Can be + 0 –
Left handside is always a variable, right hand side is value, expression
The right hand side of an assignment operator (=) is always computed first and then is assigned to the
Left hand side
Math operators
Add +
Subtract –
Multiply *
Divide /
Exponentiation/power of **
Precedence of Operators is
()
**
*, /, //, %
,+, -
For operators with same precedence, calculations are done left to right.
Division operations (/) results are always given as floating points e.g. 3/2 = 1.5, 6/2 = 3.0
An expression/variable can be used anywhere a single value can be used
String
String is a collection of characters delimited by ‘ or “
Print function
Print() uses brackets
When print prints the values in the brackets a line break is added.
So any further printing will be shown on the next line.
If no argument is given with print e.g. print(), This prints a blank line
Can be used to print more than one value/variable on a single line
Each value to be printed is separated by a comma.
e.g. print(1,’Hello’,’World’) 1 Hello World
print(‘The final results are:’,56,’and’,44) The final results are: 56 and 44
The default separator between different values to be printed is a single space.
Print(‘Final amount is $’, 100) Final amount is $ 100 (unnecessary space)
To change the default separator we provide an optional last argument to the print function
sep=’[your choice of separator]’ with double or single quotes
Eg
Print(‘Hello’, ‘World’, sep=’*’ Hello*World
So we can fix $ 100 so
Print(‘Final amount is $’, 100,sep=””) Final amount is $100,
end
, COMSCI 101, Week 1, Lecture 3
Literals
Literals are values that can be stored in program memory
Eg.
20, string
Year=2022
Year is variable and 2022 is literal
Variables can be assigned to and literal value or an expression. Variables refer to a single piece of info
Expressions
Made up of literal values and variables, expressions always evauluate to a single value. Right hand side of (=) is
expression.
Docstrings
Special string used to provide documentation.
3 consecutive quotes are used to surround docstring. In the format:
“””
Docstring
“””
Interpreter ignores docstring
Docstring gives author info and program description.
Program must have docstring
Too much info should not be provided, only general info
EVERY PROGRAMMING ASSIGNMENT MUST HAVE DOCSTRING, UNLESS TOLD OTHERWISE
Notes
After # to the end of the line is considered a note/comment, will not be read by interpreter
Only add when necessary, when code is difficult to comprehend.
Better than comments is to use descriptive variable names like this: