csc1015f
Introduction: What is computer science
Computer Science (CS) is the study of:
▪ Computer software
▪ Algorithms, abstractions and e ciency
▪ Theoretical foundation for computation
What you learn in Computer Science:
▪ Principles of computation
▪ How to make machines perform complex tasks
▪ How to program a computer
▪ What current technology exists and how to use it
▪ Problem solving
Problem Solving in CS
1. Understand the problem
•What are the knowns and unknowns?
2. Plan how to solve the problem
• What algorithm is used to solve the problem?
• What assumptions are being made?
• Is this similar to other problems?
• Can the problem be split into parts?
3. Carry out your plan – write program
• Write program(s) to implement algorithm(s)
4. Assess the result
1. Does the program conform to the algorithm?
2. Does the program/algorithm solve the problem?
3. Is the program correct for all cases?
5. Describe what you have learnt
• ... so you do not make the same mistakes again.
6. Document the solution
1. Write a report for users of the program.
2. Write comments within the program.
Algorithm: a process or set of rules to be followed in calculations or other problem-solving operations,
especially by a computer. An algorithm is a set of steps to accomplish a task.
• Algorithms must be precise so that they are
- Repeatable
- Have a predictable outcome
- Can be executed by di erent people
1
ff ffi
,Elements of algorithms
Sequence
▪ Each step is followed by another step
Selection
▪ A choice may be made among alternatives
Iteration
▪ A set of steps may be repeated
Any language with these 3 constructs can express any classical algorithm.
Algorithm to Boil Water in Kettle
1. Take the lid o kettle
2. If there is enough water already, go to step 7
3. Put kettle under tap
4. Open tap
5. While kettle is not full,
▪ Wait 6. Close tap
7. Replace lid on kettle
8. Plug kettle into power outlet
9. Turn kettle on
10. While water has not boiled,
▪ Wait
11. Turn kettle o
12. Remove plug from power outlet
Programs: A program is a set of instructions given to a computer, corresponding to an algorithm to
solve a problem.
▪ The act of writing a program is called programming.
▪ Programs are written in a precise language called a programming language
Q:How is an algorithm di erent from a program?
A: Algorithm is written in any language, its not written in a programming language. Programming is
written in code in a programming language
Programs work as follows:
▪ Ingest information from the real world (input).
▪ Process data internally.
▪ Send computed data back to real world (output).
▪ Because of di erent input, each time a program executes the results can be di erent
How We Program in Python
▪ We write programs, stored in text les
▪ Each program is a set of instructions that the Python interpreter will execute when the
program is executed by the user
-We often do both of these things in an Integrated Development Environment (IDE)
- We can also use the interactive interpreter to run short programs while testing our
ideas
2
ffff ff fi ff
, ▪ Later, we will neaten our code into blocks called functions
▪ Python is an OOP (object orientated) language but we will not use this
Introduction: Basics 2
Syntax errors are when your program does not conform to the structure required e.g. print
spelt incorrectly
▪ The program will not start at all
Logic errors are when your program runs but does not work as expected
▪ You MUST test your program
For numbers we have two primitive types:
Integers
Whole number with no fractional part
Integer operations
Operator precedence
For integer operations, the system will rst handle brackets (), then **, then *, // and %, and nally + and -.
If an expression contains multiple operations which are at the same level of precedence, like *, // and %,
they will be performed in order, either from left to right (for left-associative operators) or from right to
left (for right-associative operators). All these arithmetic operators are left-associative, except for **,
which is right-associative:
➡ Literals: are actual data values written into a program
• Numeric literals can be output just like text
3
fi fi
, print(12) 12, print(12+13) 25, print(2+2/2) 3.0
Even 1.0 is a oating-point number and not an integer.
➡ Expressions: Common operations
Modulus (%) 13
Otherwise known as the integer remainder operator
▪a % b = remainder of a divided by b
▪ Or subtract closest multiple of b from a
Examples:
▪ 9 % 3 = 0 (closest multiple of 3 is 9 or 9 // 3 * 3 = 9)
▪ 5 % 2 = 1 (closest multiple of 2 is 4 or 5 // 2 * 2 = 4)
▪ 18 % 5 = 3 (closest multiple of 5 is 15 or 18 // 5 * 5 = 15)
Floating-point numbers
Numbers with a decimal point, fraction or an exponent
Egs. Examples are 5.0, 10.24, 0.0, 12. and .3. We can use scienti c notation to denote very large or very
small oating point numbers, e.g. 3.8 x 1015
▪ WARNING: stores only an approximation to real number
▪ There is a limit to the precision, or accuracy, of the stored values
When displaying oats, we will usually specify how we would like them to be displayed, using string
formatting:
# This will print 12.35
print("%.2f" % 12.3456)
Floating-point operations and precedence
They also use the same operators, except for division – the oating-point division operator is /.
Floating-point operations always produce a oating-point solution.
Input
The purpose of the Python input statement is to get a text string from the user; this can be stored in a
variable
We get input using the input function
‘Variable’ = input(prompt
•prompt is usually some text asking the user for input
•variable is where the user’s response is stored)
Inputting numbers
- eval() is a Python function that converts a string into a number
- int() and oat() also work if we know what kind of number it is
4
fl fl fl fl fl flfi
Introduction: What is computer science
Computer Science (CS) is the study of:
▪ Computer software
▪ Algorithms, abstractions and e ciency
▪ Theoretical foundation for computation
What you learn in Computer Science:
▪ Principles of computation
▪ How to make machines perform complex tasks
▪ How to program a computer
▪ What current technology exists and how to use it
▪ Problem solving
Problem Solving in CS
1. Understand the problem
•What are the knowns and unknowns?
2. Plan how to solve the problem
• What algorithm is used to solve the problem?
• What assumptions are being made?
• Is this similar to other problems?
• Can the problem be split into parts?
3. Carry out your plan – write program
• Write program(s) to implement algorithm(s)
4. Assess the result
1. Does the program conform to the algorithm?
2. Does the program/algorithm solve the problem?
3. Is the program correct for all cases?
5. Describe what you have learnt
• ... so you do not make the same mistakes again.
6. Document the solution
1. Write a report for users of the program.
2. Write comments within the program.
Algorithm: a process or set of rules to be followed in calculations or other problem-solving operations,
especially by a computer. An algorithm is a set of steps to accomplish a task.
• Algorithms must be precise so that they are
- Repeatable
- Have a predictable outcome
- Can be executed by di erent people
1
ff ffi
,Elements of algorithms
Sequence
▪ Each step is followed by another step
Selection
▪ A choice may be made among alternatives
Iteration
▪ A set of steps may be repeated
Any language with these 3 constructs can express any classical algorithm.
Algorithm to Boil Water in Kettle
1. Take the lid o kettle
2. If there is enough water already, go to step 7
3. Put kettle under tap
4. Open tap
5. While kettle is not full,
▪ Wait 6. Close tap
7. Replace lid on kettle
8. Plug kettle into power outlet
9. Turn kettle on
10. While water has not boiled,
▪ Wait
11. Turn kettle o
12. Remove plug from power outlet
Programs: A program is a set of instructions given to a computer, corresponding to an algorithm to
solve a problem.
▪ The act of writing a program is called programming.
▪ Programs are written in a precise language called a programming language
Q:How is an algorithm di erent from a program?
A: Algorithm is written in any language, its not written in a programming language. Programming is
written in code in a programming language
Programs work as follows:
▪ Ingest information from the real world (input).
▪ Process data internally.
▪ Send computed data back to real world (output).
▪ Because of di erent input, each time a program executes the results can be di erent
How We Program in Python
▪ We write programs, stored in text les
▪ Each program is a set of instructions that the Python interpreter will execute when the
program is executed by the user
-We often do both of these things in an Integrated Development Environment (IDE)
- We can also use the interactive interpreter to run short programs while testing our
ideas
2
ffff ff fi ff
, ▪ Later, we will neaten our code into blocks called functions
▪ Python is an OOP (object orientated) language but we will not use this
Introduction: Basics 2
Syntax errors are when your program does not conform to the structure required e.g. print
spelt incorrectly
▪ The program will not start at all
Logic errors are when your program runs but does not work as expected
▪ You MUST test your program
For numbers we have two primitive types:
Integers
Whole number with no fractional part
Integer operations
Operator precedence
For integer operations, the system will rst handle brackets (), then **, then *, // and %, and nally + and -.
If an expression contains multiple operations which are at the same level of precedence, like *, // and %,
they will be performed in order, either from left to right (for left-associative operators) or from right to
left (for right-associative operators). All these arithmetic operators are left-associative, except for **,
which is right-associative:
➡ Literals: are actual data values written into a program
• Numeric literals can be output just like text
3
fi fi
, print(12) 12, print(12+13) 25, print(2+2/2) 3.0
Even 1.0 is a oating-point number and not an integer.
➡ Expressions: Common operations
Modulus (%) 13
Otherwise known as the integer remainder operator
▪a % b = remainder of a divided by b
▪ Or subtract closest multiple of b from a
Examples:
▪ 9 % 3 = 0 (closest multiple of 3 is 9 or 9 // 3 * 3 = 9)
▪ 5 % 2 = 1 (closest multiple of 2 is 4 or 5 // 2 * 2 = 4)
▪ 18 % 5 = 3 (closest multiple of 5 is 15 or 18 // 5 * 5 = 15)
Floating-point numbers
Numbers with a decimal point, fraction or an exponent
Egs. Examples are 5.0, 10.24, 0.0, 12. and .3. We can use scienti c notation to denote very large or very
small oating point numbers, e.g. 3.8 x 1015
▪ WARNING: stores only an approximation to real number
▪ There is a limit to the precision, or accuracy, of the stored values
When displaying oats, we will usually specify how we would like them to be displayed, using string
formatting:
# This will print 12.35
print("%.2f" % 12.3456)
Floating-point operations and precedence
They also use the same operators, except for division – the oating-point division operator is /.
Floating-point operations always produce a oating-point solution.
Input
The purpose of the Python input statement is to get a text string from the user; this can be stored in a
variable
We get input using the input function
‘Variable’ = input(prompt
•prompt is usually some text asking the user for input
•variable is where the user’s response is stored)
Inputting numbers
- eval() is a Python function that converts a string into a number
- int() and oat() also work if we know what kind of number it is
4
fl fl fl fl fl flfi