Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Introduction to Computing Science and Programming I
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
An Introduction to Python – Part II Dr. Nancy Warter-Perez April 21, 2005.
Loops – While, Do, For Repetition Statements Introduction to Arrays
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Main task -write me a program
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
CS0007: Introduction to Computer Programming Introduction to Arrays.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Python Control of Flow.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Lists in Python.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
>>> # Fibonacci series:... # the sum of two elements defines the next... a, b = 0, 1 >>> while b < 10:... print b... a, b = b, a+b WHILE.
For. for loop The for loop in Python is more like a foreach iterative-type loop in a shell scripting language than a traditional for conditional loop.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
If statements while loop for loop
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering.
Built-in Data Structures in Python An Introduction.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Introduction to Programming Workshop 6 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Lists COMPSCI 105 S Principles of Computer Science.
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
GCSE Computing: Programming GCSE Programming Remembering Python.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
More Python Proglan Python Session 2. Lists  Lists are like flexible arrays. They can contain as many variables as you wish, and all variable types are.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson UW CSE 160 Spring 2018
Introduction to Strings
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Python Review
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Class code for pythonroom.com cchsp2cs
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Control flow: Loops UW CSE 160.
Introduction to Computer Science
Presentation transcript:

Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d

Part 1 Sequences – arrays, lists etc ‘for’ loops – repeating code in a loop Indexing – into sequences Exercises

Sequences - 1 We need to hold sequences of numbers The main sequences in Python are: Strings EG theName = ‘Monty Python’ Lists EG [1, 2.34, ‘fred’] numpy arrays EG (1.1, 2.2, 3.3) – These can be n-dimensional TypeIndex?Modify?Add elements? Mixed types? TupleYesNo Yes StringYes No ListYes ArrayYes No Dictionary NoYes

Examples of sequences >>> myList = [1,2,3,4,5] >>> print myList[2] [1,2,3,4] >>> myArray = numpy.array(myList) >>> print myArray [ ] >>> myTuple = (1,2,3) >>> print myTuple (1,2,3) >>> myString = “Hello world” >>> print myString Hello world

‘for’ loops - 1 If we want to do something many times, we write the code once and put it in a loop. There are two types of loop: ‘while’ loops terminate on a condition. ‘for’ loops are told how many times to loop. A very common way to build a for loop is to use the built-in ‘range()’ function. Try this out at the python prompt. Type: >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] It returns a list of integers starting at zero and ending at The parameter sent to the range function is one more than the max. range() has other parameters; use help(range) to understand it. Exercise: Make a list starting at 1, ending at 9 in steps of 2. It should contain: [1,3,5,7,9].

‘for’ loops – 2 A for loop needs a list or other sequence to iterate over. We can use the built-in function range() to make a list. The for loop will execute a block of code so remember to start with a colon and indent all of the block Try this from the python prompt: >>> for i in range(10): …print i … It should print the integers from 0 to 9. (Not 0 to 10!) If you want to loop over every element in an array, use the length of the array (len() is a built-in function) as the parameter to range(): >>> a = numpy.array([2,3,4,5]) >>> for i in range(len(a)): …print a[i] …

Indexing - 1 Before we do an example with a ‘for’ loop… How do we put data into and get data out of an array? This process is called indexing. Try this: Create a small 1-d array called myData with 5 real numbers in it Change one of the values then print the array to see the change: >>> myData[2] = >>> print myData Copy one of the values to a variable and print it: >>> x = myData[3] >>> print x Note that indexing always uses square brackets [ ] Later we will see how to read or write larger array parts or ‘slices’.

Indexing - 2 Indexing is very similar for arrays, lists and strings. Try it with a string: >>> theName = ‘Monty Python’ >>> print theName[2] >>> n Remember that indexing starts at zero! The data in any type of sequence are like items in pigeon holes. The index is the label on the pigeon hole: theName: Index:

Exercises Do the following exercises. The details are in the course notes. Solutions will be on DUO after the workshop Exercise 6.3 – Array operations Exercise 6.4 – Find mean of a real array Exercise 7.1 – Use a for loop to find squares and cubes You must show your solution for exercise 7.1 to a demonstrator before you leave. Exercise 7.2 –Find squares and cubes using array operators

Part 2 for loops without ‘range()’ while loops More exercises…

‘for’ loops - 3 We often use range() to generate the object for a for loop to iterate over. But we don’t have to… Try this: >>> values = [1.1, 2.3, 7.1, 4.4] >>> for value in values: …print value, values can actually be any ‘iterable object’! Try it again where values is an array; arrays are iterable. Note: the comma after value causes print to put the data all on one line

While loops We use for loops when we know how many times we want to go round the loop. When we don’t know this, we use a while loop. Exercise: Save this program to a file and run it: x = range(10) index = 0 while x[index] <= 5: index = index +1 print ‘Now x is greater than 5’ This is a pointless example since we know beforehand when the condition will be met; it just shows the syntax Note that there is no automatic increment of a loop variable as there is in a for loop; you must increment it yourself

Exercises Do the following exercises. The details are in the course notes. Solutions will be on DUO after the workshop Finish these first!! Exercises: 6.3, 6.4, 7.1, 7.2 Finish these now if you can or before the next workshop Exercise 7.3 – Generate Fibonacci numbers Exercise 7.4 – Find the golden ratio