0% found this document useful (0 votes)
50 views

Python

The document provides information about Python programming language and computational concepts like algorithms, variables, data types, statements, and sorting methods. It explains that Python is an interpreted, high-level programming language used for general purpose programming. It also defines key computational terms and demonstrates how to write a simple sorting algorithm in Python using bubble sort.

Uploaded by

api-416920618
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Python

The document provides information about Python programming language and computational concepts like algorithms, variables, data types, statements, and sorting methods. It explains that Python is an interpreted, high-level programming language used for general purpose programming. It also defines key computational terms and demonstrates how to write a simple sorting algorithm in Python using bubble sort.

Uploaded by

api-416920618
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Python

Computation

kɒmpjʊˈteɪʃ(ə)n/

noun

1.the action of mathematical calculation.


"methods of computation"

2.the use of computers, especially as a subject of research or study.

Computation is any type of calculation that includes both arithmetical and logical steps and
follows a well-defined model.

The study of computation is paramount to the discipline of computer science.

***Python is an interpreted high-level programming language for general-purpose


programming.

Programming

Programming languages are designed to be easy for a human to


understand and write in. However, a computer cannot run programs
written in these languages directly. Most programming languages have
to be translated into machine code before the computer can execute the
instructions.

Translators

A piece of translator software, which is usually included within programming software,


converts high-level languages into machine code. These translators are known
as compilers and interpreters. Programs are translated using either a compiler or
interpreter.

Compiler

A compiler translates a human-readable program directly into an executable, machine-


readable form before the program can run.
Interpreter

An interpreter translates a human-readable program into an executable, machine-readable


form, instruction by instruction. It then executes each translated instruction before moving
on to the next one. A program is translated every time it is run. Python is an example of an
interpreted language.

Machine code Also called object-code, this is low-level code that


represents how computer hardware and CPUs understand instructions. It
is represented by binary.

Psuedocode A method of writing up a set of instructions for a computer


program using plain English.

Algorithm A sequence of logical instructions for carrying out a task.

What is a program?

Programs are made up of statements that the programming language knows and
understands.

Just as words are put together to form a sentence, a program puts one or more statements
together to form an instruction. Each statement tells the computer to perform a specific
task, and the instructions tell a computer what to do.

Statements

Different programming languages use different statements. A few of these are listed in this
table:

Statement Purpose
print Output a message on the screen
input Get data from the user
if…else A decision
while A loop controlled by a decision
for A loop controlled by a counter
def Create a procedure or function

Creating a program from an algorithm

Consider this simple problem. A cinema is offering discount tickets to anyone who is under
15. Decomposing this problem, gives this algorithm:

1. find out how old the person is

2. if the person is younger than 15 then say “You are eligible for a discount ticket.”

3. otherwise, say “You are not eligible for a discount ticket.”

In pseudocode, the algorithm would look like this:

OUTPUT "How old are you?"

INPUT User inputs their age

STORE the user's input in the age variable

IF age < 15 THEN OUTPUT "You are eligible for a discount."

ELSE OUTPUT "You are not eligible for a discount."

To convert the flowchart or pseudocode into a program, look at each individual step, and
write an equivalent instruction.

Creating the program in Python

age = int(input("How old are you?"))


if age < 15: print("You are eligible for a discount.")

else: print("You are not eligible for a discount.")

Basic arithmetic

Arithmetic process Programming equivalent


Addition (plus) +
Subtraction (minus) -
Multiplication *
Division /

Variables

All variables are made up of three parts: name, type and value.

Variables are a key element of programming. They are used for


calculations, for storing values for later use, in decisions and in iteration.

Iteration a single pass through a set of instructions.


Variable naming rules

Consistency: ‘name’ is not the same as ‘Name’ or ‘NAME’.

Spacing: variable names should not have a space in them. Use underscores or
camelCase instead, eg total_money; totalMoney).

Digits: variable names should not start with a digit.

Data types

Variables come in all shapes and sizes. Some are used to store numbers,
some are used to store text and some are used for much more
complicated types of data.

The data types to know are:

String Used for letters, numbers and symbols.

Character Used for single letters.

Integer Used for whole numbers.

Float Used for numbers that contain decimal points, or fractions.

Boolean True/False or yes/no.


Working with text

A variable can hold a number, but it can also hold a piece of text. Just one letter is called a
character. More than one character is called a string.

A text variable works in the same way as a number variable, with a couple of differences:

• text variables hold characters (letters, digits, punctuation)

• the data in text variables is placed in quotes

• arithmetic calculations cannot be performed on text variables

INPUT data – PROCESS data – OUTPUT data

An algorithm is a sequence of logical instructions for carrying out a task.


In computing, algorithms are needed to design computer programs.

An algorithm is a plan, a set of step-by-step instructions designed to


solve a problem. When designing algorithms there are three basic
building blocks (constructs) that can be used:sequencing, selection and
iteration.

Sequencing - creating a set of instructions to complete a task.


Selection - when the program decides to move on based on the results of an event.
Iteration - a single pass through a set of instructions.

IF statements
1. Ask how old you are

2. IF you are 70 or older, say “You are aged to perfection!”

The selection comes in step 2. If you are aged 70 or over, one message is displayed.

As a flowchart:
IF...ELSE statements
In programming, selection is usually represented by the statements IF and ELSE:

IF represents the question

ELSE points to what to do if the answer to the question is false

IF…ELSE
Ask how old you are

IF you are 70 or older, say “You are aged to perfection!”

ELSE say “You are a spring chicken!”

ELIF statements

In programming, selection is implemented using IF statements.


Using IF and ELSE gives two possible choices (paths) that a program
can follow. However, sometimes more than two choices are wanted. To do this, the
statement ELSE IF is used.

This simple algorithm prints out a different message depending on how old you are. Using
IF, ELSE and ELSE IF, the steps are:

• Ask how old you are

• IF you are 70 or older, say “You are aged to perfection!”

• ELSE IF you are exactly 50, say “Wow, you are half a century old!”

• ELSE say “You are a spring chicken!”

1. A diagram that shows a process, made up of boxes


representing steps, decision, inputs and outputs.
2. To put into effect.
3. A single action that can be performed by a
computer processor.
4. In computer programming, this is a single pass
through a set of instructions.
5. Sequences of instructions for a computer.
6. The process of writing computer software.
7. A high-level programming language.
8. A decision within a computer program when the
program decides to move on based on the results of an
event.
9. Creating a set of instructions to complete a task.
10. The smallest element of a programming language
which expresses an action to be carried out.

Sorting Data

There are many types of sorting algorithms. Here is an example of one such algorithm,
bubble sort.

Bubble sort

A bubble sort algorithm goes through a list of data a number of times, comparing two items
that are side by side to see which is out of order. It will keep going through the list of data
until all the data is sorted into order. Each time the algorithm goes through the list it is
called a ‘pass’.

Example

Imagine that you have a list of people who you want to sort by age, from youngest to
oldest. A bubble sort can do this.

The list of ages is:


41, 15, 17, 32, 18, 28, 77 and 54

First pass

The highlighted numbers are the numbers that are being compared.

41, 15, 17, 32, 18, 28, 77, 54

This is the list before it is sorted.

41, 15, 17, 32, 18, 28, 77, 54

The first two numbers are compared. 15 is smaller than 41 so they switch places.

15, 41, 17, 32, 18, 28, 77, 54

The next two numbers are compared. 17 is smaller than 41 so they switch places.

15, 17, 41, 32, 18, 28, 77, 54

The next two numbers are compared. 32 is smaller than 41 so they switch places.

15, 17, 32, 41, 18, 28, 77, 54

The next two numbers are compared. 18 is smaller than 41 so they switch places.

15, 17, 32, 18, 41, 28, 77, 54

The next two numbers are compared. 28 is smaller than 41 so they switch places.

15, 17, 32, 18, 28, 41, 77, 54

The next two numbers are compared. 41 is smaller than 77 so no change occurs.

15, 17, 32, 18, 28, 41, 77, 54

The next two numbers are compared. 54 is smaller than 77 so they switch places.

15, 17, 32, 18, 28, 41, 54, 77

This is what the list looks like after the first pass.

The list is now more ordered than it was originally, but it isn’t yet fully in order of youngest
to oldest. The list needs to go through another pass to compare the numbers again, so it
can be sorted further.

Second pass

15, 17, 32, 18, 28, 41, 54, 77

This is what the list looks like after the first pass.

15, 17, 32, 18, 28, 41, 54, 77

The first two numbers are compared. 15 is smaller than 17 so no change occurs.
15, 17, 32, 18, 28, 41, 54, 77

The next two numbers are compared. 17 is smaller than 32 so no change occurs.

15, 17, 32, 18 28, 41, 54, 77

The next two numbers are compared. 18 is smaller than 32 so they switch places.

15, 17, 18, 32, 28, 41, 54, 77

The next two numbers are compared. 28 is smaller than 32 so they switch places.

15, 17, 18, 28, 32, 41, 54, 77

The next two numbers are compared. 32 is smaller than 41 so no change occurs.

15, 17, 18, 28, 32, 41, 54, 77

The next two numbers are compared. 41 is smaller than 54 so no change occurs.

15, 17, 18, 28, 32, 41, 54, 77

The next two numbers are compared. 54 is smaller than 77 so no change occurs.

15, 17, 18, 28, 32, 41, 54, 77

This is what the list looks like after the second pass.

The set of data is now in order from youngest to oldest, but the algorithm does not know
this yet as it made some changes in the second pass. The algorithm will only recognise
that the list is in order if it makes no changes in a pass. The algorithm therefore needs to
run for a third pass to compare the numbers again. As no changes will be made, the
algorithm will then recognise that the data is in order.

If the data being sorted is a large set of data, it may take several passes to get the data
sorted.

One of the main advantages of a bubble sort is that it is a very simple algorithm to
describe to a computer. There is only really one task to perform (compare two values and,
if needed, swap them). This makes for a very small and simple computer program.

The biggest problem with a bubble sort is that it takes a very long time to run. For
example, if there are 100 values to sort, each pass through the list will take 99
comparisons – and you might have to repeat it 99 times.

You might also like