Python: Arrays Damian Gordon. Arrays In Python arrays are sometimes called “lists” or “tuple” but we’ll stick to the more commonly used term “array”.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Searching Arrays Linear search Binary search small arrays
Geography 465 Assignments, Conditionals, and Loops.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Chapter 8 ARRAYS Continued
Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information.
Asymptotic Notations Iterative Algorithms and their analysis
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Applications of Arrays (Searching and Sorting) and Strings
Iteration: WHILE Loop Damian Gordon. WHILE Loop Consider the problem of searching for an entry in a phone book with only SELECTION:
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching Damian Gordon. Google PageRank Damian Gordon.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Neal Stublen Computer Memory (Simplified)  Remember, all programming decisions came down to a true or false evaluation  Consider.
Python: Print Damian Gordon. Your first Python program When learning a new computer programming language, the first thing typically taught is how to write.
Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.
Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:
Data Structures: Arrays Damian Gordon. Arrays Imagine we had to record the age of everyone in the class, we could do it declaring a variable for each.
September 26, 2011 Sorting and Searching Lists. Agenda Review quiz #3 Team assignment #1 due tonight  One per team Arcade game Searching  Linear  Binary.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
1.1 Data Structure and Algorithm Lecture 1 Array Record Sequential Search Binary Search Bubble Sort Recursion Complexity Topics.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Python: Selection Damian Gordon. Python: Selection We’ll consider two ways to do selection: The IF statement The CASE statement.
Data Handling in Algorithms. Activity 1 Starter Task: Quickly complete the sheet 5mins!
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Data Structures Arrays and Lists Part 2 More List Operations.
Data Structures: Multi-Dimensional Arrays Damian Gordon.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Python: Revision Damian Gordon. Python: Structured Programming Damian Gordon.
Python: File Management Damian Gordon. File Management We’ve seen a range of variable types: – Integer Variables – Real Variables – Character Variables.
Iteration: FOR, DO, LOOP Loop Damian Gordon. FOR Loop The FOR loop does the same thing as a WHILE loop but is easier if you are using the loop to do a.
Introduction to Python
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Pseudocode (Revision)
Recitation 13 Searching and Sorting.
Lecture 25 The Tao that can be talked about is not the true Tao
Python: Advanced Sorting Algorithms
Basic operators - strings
Linked Lists Damian Gordon.
Boolean Logic Damian Gordon.
كلية المجتمع الخرج البرمجة - المستوى الثاني
Nate Brunelle Today: Repetition, Repetition
searching Concept: Linear search Binary search
Queues: Implemented using Arrays
Python: Algorithms Damian Gordon.
Linear Search Binary Search Tree
Simple Statistics on Arrays
Some Common Issues: Iteration
Sorting Damian Gordon.
Python Basics with Jupyter Notebook
Python: Sorting – Selection Sort
Some Common Issues: Common Algorithms
Data Types and Maths Programming Guides.
COMPUTING.
Presentation transcript:

Python: Arrays Damian Gordon

Arrays In Python arrays are sometimes called “lists” or “tuple” but we’ll stick to the more commonly used term “array”. But if you see it called “list” or “tuple” in books or on the web, they mean an array.

Arrays We’ll remember that an array is a collection of the same type of variables (like a set in maths) ……..… ……..… 34

Arrays To declare an zero-filled array in Python we can do the following: Age = [0 for x in range(8)]

Arrays To declare an array with values in Python: Age = [44, 23, 42, 33, 16, 54, 34, 18]

Arrays To see the first value: print(Age[0])

Arrays To see the first value: print(Age[0]) 44

Arrays To see the second value: print(Age[1])

Arrays To see the second value: print(Age[1]) 23

Arrays To see the last value: print(Age[7])

Arrays To see the last value: print(Age[7]) 18

Arrays To print out all the values in the array:

# PROGRAM SampleArrayProg: Age = [44, 23, 42, 33, 16, 54, 34, 18] for a in range(0,8): # DO print(Age[a]) # ENDFOR; # END.

Arrays To make that print out a bit nicer:

# PROGRAM SampleArrayProg: Age = [44, 23, 42, 33, 16, 54, 34, 18] for a in range(0,8): # DO print("Age[",a,"] =", Age[a]) # ENDFOR; # END.

Arrays Because Python is so cool, I can also just do the following: print(Age)

Arrays Let’s add 1 to each value in the array

# PROGRAM Add1ToArray: Age = [44, 23, 42, 33, 16, 54, 34, 18] for a in range(0,8): # DO print(Age) Age[a] = Age[a] + 1 # ENDFOR; print(Age) # END.

Arrays Let’s get the average value of the array

# PROGRAM AverageArray: Age = [44, 23, 42, 33, 16, 54, 34, 18] total = 0 for a in range(0,8): # DO total = total + Age[a] # ENDFOR; AveValue = total/8 print(AveValue) # END.

Arrays Let’s make that better:

# PROGRAM BetterAverageArray: Age = [44, 23, 42, 33, 16, 54, 34, 18] total = 0 for a in range(0,len(Age)): # DO total = total + Age[a] # ENDFOR; AveValue = total/len(Age) print(AveValue) # END.

# PROGRAM BetterAverageArray: Age = [44, 23, 42, 33, 16, 54, 34, 18] total = 0 for a in range(0,len(Age)): # DO total = total + Age[a] # ENDFOR; AveValue = total/len(Age) print(AveValue) # END.

Arrays To declare an array of real numbers, it’s very similar:

# PROGRAM BetterAverageArrayReal: BankBal = [44.44,423.33,545.23,423.3,121.6,32.4,121.4,13.8] total = 0 for a in range(0,len(BankBal)): # DO total = total + BankBal[a] # ENDFOR; AveValue = total/len(BankBal) print(AveValue) # END.

Arrays To declare an array of characters, it’s very similar:

# PROGRAM BetterAverageArrayChar: letters = ['d','g','e','s','b','j','r','j'] for a in range(0,len(letters)): # DO print(letters[a]) # ENDFOR; # END.

Arrays And the same for strings:

# PROGRAM BetterAverageArrayString: Pets = ["dog","cat","fish","cat","dog","fish","cat","dog"] for a in range(0,len(Pets)): # DO print(Pets[a]) # ENDFOR; # END.

Arrays Here’s an array of Booleans:

# PROGRAM BetterAverageArrayBoolean: IsWeekend = [False, False, False, False, False, True, True] for a in range(0,len(IsWeekend)): # DO print(IsWeekend[a]) # ENDFOR; # END.

Python: Searching Damian Gordon

Searching To search for everyone who is 18 in an integer array:

# PROGRAM SequentialSearch: Age = [44, 23, 42, 33, 18, 54, 34, 18] for a in range(0,len(Age)): # DO if Age[a] == 18: # THEN print("User", a, "is 18") # ENDIF; # ENDFOR; # END.

Searching This is a sequential search, we visit each value, that’s OK for a small array, but for a massive array we might need to try a different approach.

Searching If the data is sorted, we can do a BINARY SEARCH This means we jump to the middle of the array, if the value being searched for is less than the middle value, all we have to do is search the first half of that array. We search the first half of the array in the same way, jumping to the middle of it, and repeat this.

# PROGRAM BinarySearch: Age = [16, 18, 23, 31, 33, 34, 46, 54] SearchVal = int(input("Please input the search value: ")) first = 0 last = len(Age) IsFound = False Part 1 of 3

while first <= last and IsFound == False: # DO index = (first + last) // 2 if Age[index] == SearchVal: # THEN IsFound = True print("Value found") elif Age[index] > SearchVal: # THEN last = index - 1 else: first = index + 1 # ENDIF; # ENDWHILE; Part 2 of 3

if IsFound == False: # THEN print("Value not in array") # ENDIF; # END. Part 3 of 3

etc.