Introduction to Programming in Python (1)

Similar documents
Introduction to Programming in Python (2)

Introduction to Python Lecture #3

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Introduction to Python

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

The current topic: Python. Announcements. Python. Python

ECE 364 Software Engineering Tools Lab. Lecture 3 Python: Introduction

Flow Control: Branches and loops

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Lists, Tuples and Dictionaries. HORT Lecture 10 Instructor: Kranthi Varala

CS 3813/718 Fall Python Programming. Professor Liang Huang.

Some material adapted from Upenn cmpe391 slides and other sources

ENGR 102 Engineering Lab I - Computation

Python Class-Lesson1 Instructor: Yao

Course Introduction and Python Basics

Lecture 7: Python s Built-in. in Types and Basic Statements

Senthil Kumaran S

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Python - Variable Types. John R. Woodward

A Brief Python Tutorial

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Part III Appendices 165

BCH339N Systems Biology/Bioinformatics Spring 2018 Marcotte A Python programming primer

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Python Basics. Lecture and Lab 5 Day Course. Python Basics

CS Advanced Unix Tools & Scripting

History Installing & Running Python Names & Assignment Sequences types: Lists, Tuples, and Strings Mutability

PROGRAMMING FUNDAMENTALS

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

CPSC 217 Midterm (Python 3 version)

6. Data Types and Dynamic Typing (Cont.)

Introduction to Python

Data type built into Python. Dictionaries are sometimes found in other languages as associative memories or associative arrays.

Practical Workbook Computer Programming

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

All programs can be represented in terms of sequence, selection and iteration.

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

Python: Short Overview and Recap

Python Language Basics I. Georgia Advanced Computing Resource Center University of Georgia Zhuofei Hou, HPC Trainer

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Introduction to Bioinformatics

Glossary. For Introduction to Programming Using Python By Y. Daniel Liang

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Python: common syntax

18.1. CS 102 Unit 18. Python. Mark Redekopp

COMP1730/COMP6730 Programming for Scientists. Strings

PYTHON CONTENT NOTE: Almost every task is explained with an example

Python Tutorial. CSE 3461: Computer Networking

Python a modern scripting PL. Python

PROGRAMMING FUNDAMENTALS

A Python programming primer for biochemists. BCH364C/394P Systems Biology/Bioinformatics Edward Marcotte, Univ of Texas at Austin

CS S-02 Python 1. Most python references use examples involving spam, parrots (deceased), silly walks, and the like

Introduction to Python

CSE : Python Programming

What Version Number to Install

AI Programming CS S-02 Python

The Dynamic Typing Interlude

Introduction to Python! Lecture 2

CS Summer 2013

Python Crash-Course. C. Basso. Dipartimento di Informatica e Scienze dell Informazione Università di Genova. December 11, 2007

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

Accelerating Information Technology Innovation

EE 355 Unit 17. Python. Mark Redekopp

Numerical Methods. Centre for Mathematical Sciences Lund University. Spring 2015

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Data Structures. Lists, Tuples, Sets, Dictionaries

Python Training. Complete Practical & Real-time Trainings. A Unit of SequelGate Innovative Technologies Pvt. Ltd.

Python Tutorial. Day 1

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Webgurukul Programming Language Course

Introduction to Python

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017

Advanced Python. Executive Summary, Session 1

Working with Lists 4

The Practice of Computing Using PYTHON. Chapter 4. Working with Strings. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Physics 514 Basic Python Intro

Scripting Languages. Python basics

STA141C: Big Data & High Performance Statistical Computing

UNIVERSITÀ DI PADOVA. < 2014 March >

Teaching London Computing

Python Workshop. January 18, Chaitanya Talnikar. Saket Choudhary

ENGR 102 Engineering Lab I - Computation

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

Python in 10 (50) minutes

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

The Pyth Language. Administrivia

ECE 364 Software Engineering Tools Laboratory. Lecture 4 Python: Collections I

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Introduction to Python: Data types. HORT Lecture 8 Instructor: Kranthi Varala

Sequence of Characters. Non-printing Characters. And Then There Is """ """ Subset of UTF-8. String Representation 6/5/2018.

Course Outline - COMP150. Lectures and Labs

Introduction to Python Code Quality

Introduction to Python - Part I CNV Lab

Transcription:

to Programming in Python (1) Steve Renals s.renals@ed.ac.uk ICL 26 September 2005 Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Announcements Lab sessions: Groups listed on the web: http://www.inf.ed.ac.uk/admin/itodb/mgroups/labs/icl.html Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Numbers and variables Strings Lists Dictionaries Conditionals Loops Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Python books Mark Lutz and David Ascher (2004). Learning Python, 2nd Edition, O Reilly. Allen Downey, Jeff Elkner and Chris Meyers (2001), How to Think Like a Computer Scientist: Learning with Python, Green Tea Press. http://www.greenteapress.com/thinkpython/ David Beazley (2001), Python Essential Reference, 2nd edition, New Riders. Mark Lutz (2002). Python Pocket Reference, 2nd Edition, O Reilly. Mark Lutz (2001). Programming Python, 2nd Edition, O Reilly. (O Reilly books available via on the web Safari Books Online: http://proquest.safaribooksonline.com/?uicode=edinburgh) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Python features Free, portable, powerful Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Python features Free, portable, powerful Easy to mix in components from other languages Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Python features Free, portable, powerful Easy to mix in components from other languages Object-oriented (including operator overloading, polymorphism, multiple inheritance) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Python features Free, portable, powerful Easy to mix in components from other languages Object-oriented (including operator overloading, polymorphism, multiple inheritance) Easy to use, easy to learn, easy to understand Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Python features Free, portable, powerful Easy to mix in components from other languages Object-oriented (including operator overloading, polymorphism, multiple inheritance) Easy to use, easy to learn, easy to understand NLTK and NLTK-Lite are Python packages that we will use in ICL (Learning Python, chapter 1) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Using Python interactively The easiest way to use Python initially is interactively: % python2.4 >>> print ICL ICL >>> print 3*4 12 >>> print 2**16 65536 >>> myname = Steve >>> myname Steve (Learning Python, chapter 3) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Using Python interactively The easiest way to use Python initially is interactively: % python2.4 >>> print ICL ICL >>> print 3*4 12 >>> print 2**16 65536 >>> myname = Steve >>> myname Steve (Learning Python, chapter 3) Can also use the IDLE environment: idle2.4 (X)Emacs users have python-mode. Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Modules To save code you need to write it in files Module: a text file containing Python code Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Modules To save code you need to write it in files Module: a text file containing Python code Example: write the following to file foo.py: print 25*3 # multiply by 3 print ICL + lecture 2 # concatenate strings using + myname = Steve (No leading spaces!) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Modules To save code you need to write it in files Module: a text file containing Python code Example: write the following to file foo.py: print 25*3 # multiply by 3 print ICL + lecture 2 # concatenate strings using + myname = Steve (No leading spaces!) Then run it as follows: % python foo.py 75 ICL lecture 2 % Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Importing modules Every file ending in.py is a Python module. Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Importing modules Every file ending in.py is a Python module. Modules can contain attributes such as functions, Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Importing modules Every file ending in.py is a Python module. Modules can contain attributes such as functions, We can import this module into Python: % python >>> import foo 75 ICL lecture 2 >>> foo.myname Steve Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Executable scripts On unix/linux can make normal Python text files executable: The first line is special beginning with #! File has executable privileges (chmod +x file.py) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Executable scripts On unix/linux can make normal Python text files executable: The first line is special beginning with #! File has executable privileges (chmod +x file.py) Example: write the following to file foo.py: #!/usr/bin/python2.4 print 25*3 # multiply by 3 print ICL + lecture 2 # concatenate strings using + myname = Steve Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Executable scripts On unix/linux can make normal Python text files executable: The first line is special beginning with #! File has executable privileges (chmod +x file.py) Example: write the following to file foo.py: #!/usr/bin/python2.4 print 25*3 # multiply by 3 print ICL + lecture 2 # concatenate strings using + myname = Steve % chmod +x foo.py % foo.py 75 ICLlecture 2 % Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Module reloads Importing is expensive after the first import of a module, repeated imports of a module have no effect (even if you have edited it) Use reload for force Python to rerun the file again: Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Module reloads Importing is expensive after the first import of a module, repeated imports of a module have no effect (even if you have edited it) Use reload for force Python to rerun the file again: >>> import foo 75 ICL lecture 2 Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Module reloads Importing is expensive after the first import of a module, repeated imports of a module have no effect (even if you have edited it) Use reload for force Python to rerun the file again: >>> import foo 75 ICL lecture 2 Redit foo.py to print 25*4 and reload >>> reload(foo) 100 ICL lecture 2 <module foo from foo.py > Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Module attributes Consider bar.py: university = Edinburgh school = Informatics Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Module attributes Consider bar.py: university = Edinburgh school = Informatics >>> import bar >>> print bar.school Informatics Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Module attributes Consider bar.py: university = Edinburgh school = Informatics >>> import bar >>> print bar.school Informatics >>> from bar import school >>> print school Informatics Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Module attributes Consider bar.py: university = Edinburgh school = Informatics >>> import bar >>> print bar.school Informatics >>> from bar import school >>> print school Informatics >>> from bar import * >>> print university Edinburgh Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Module attributes Consider bar.py: university = Edinburgh school = Informatics >>> import bar >>> print bar.school Informatics >>> from bar import school >>> print school Informatics >>> from bar import * >>> print university Edinburgh from copies named attributes from a module, so they are variables in the recipient. Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Python program structure Programs are composed of modules Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Python program structure Programs are composed of modules Modules contain statements Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Python program structure Programs are composed of modules Modules contain statements Statements contain expressions Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Python program structure Programs are composed of modules Modules contain statements Statements contain expressions Expressions create and process objects Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Overview Running programs Modules Python program structure Programs are composed of modules Modules contain statements Statements contain expressions Expressions create and process objects (Statements include: variable assignment, function calls, control flow, module access, building functions, building objects, print) (Learning Python, chapter 4) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Python s built-in objects 1. Numbers: integer, floating point, complex 2. Strings 3. Lists 4. Dictionaries 5. Tuples 6. Files Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Numbers (and variables) Usual number operators, eg: +, *, /, **, and, & Usual operator precedence: A * B + C * D = (A * B) + (C * D) (use parens for clarity and to reduce bugs) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Numbers (and variables) Usual number operators, eg: +, *, /, **, and, & Usual operator precedence: A * B + C * D = (A * B) + (C * D) (use parens for clarity and to reduce bugs) Useful packages: math, random Serious users: numeric, numarray Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Numbers (and variables) Usual number operators, eg: +, *, /, **, and, & Usual operator precedence: A * B + C * D = (A * B) + (C * D) (use parens for clarity and to reduce bugs) Useful packages: math, random Serious users: numeric, numarray Variables created when first assigned a value replaced with their values when used in expressions must be assigned before use no need to declare ahead of time Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Strings String handling in Python is easy and powerful (unlike C, C++, Java) (Learning Python, chapter 5) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Strings String handling in Python is easy and powerful (unlike C, C++, Java) Strings may be written using single quotes: This is a Python string or double quotes "and so is this" (Learning Python, chapter 5) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Strings String handling in Python is easy and powerful (unlike C, C++, Java) Strings may be written using single quotes: This is a Python string or double quotes "and so is this" They are the same, it just makes it easy to include single (double) quotes: He said "what?", "He s here" (Learning Python, chapter 5) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Backslash in strings Backslash \ can be used to escape (protect) certain non-printing or special characters \n is newline, \t is tab Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Backslash in strings Backslash \ can be used to escape (protect) certain non-printing or special characters \n is newline, \t is tab >>> s = Name\tAge\nJohn\t21\nBob\t44 >>> print s Name Age John 21 Bob 44 >>> t = "Mary\ s" >>> print t "Mary s" Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Triple quote Use a triple quote (""" or ) for a string over several lines: >>> s = """this is... a string... over 3 lines""" >>> t = so... is... this >>> print s this is a string over 3 lines >>> print t so is this Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries String operations Concatenation (+) Length (len) Repetition (*) Indexing and slicing ([]) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries String operations Concatenation (+) Length (len) Repetition (*) Indexing and slicing ([]) s = computational t = linguistics cl = s + + t # computational linguistics l = len(cl) # 25 u = - * 6 # ------ c = s[3] # p x = cl[11:16] # al li y = cl[20:] # stics z = cl[:-1] # computational linguistic Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries String methods Methods are functions applied associated with objects String methods allow strings to be processed in a more sophisticated way Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries String methods Methods are functions applied associated with objects String methods allow strings to be processed in a more sophisticated way s = example s = s.capitalize() # Example t = s.lower() # example flag = s.isalpha() # True s = s.replace( amp, M ) # exmle i = t.find( xa ) # 1 n = t.count( e ) # 2 Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Lists in Python Ordered collections of arbitrary objects Accessed by indexing based on offset Variable length, heterogenous (can contain any type of object), nestable Mutable (can change the elements, unlike strings) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Lists in Python Ordered collections of arbitrary objects Accessed by indexing based on offset Variable length, heterogenous (can contain any type of object), nestable Mutable (can change the elements, unlike strings) >>> s = [ a, b, c ] >>> t = [1, 2, 3] >>> u = s + t # [ a, b, c, 1, 2, 3] >>> n = len(u) # 6 >>> for x in s:... print x... a b c Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Indexing and slicing lists Indexing and slicing work like strings Indexing returns the object element Slicing returns a list Can use indexing and slicing to change contents Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Indexing and slicing lists Indexing and slicing work like strings Indexing returns the object element Slicing returns a list Can use indexing and slicing to change contents l = [ a, b, c, d ] x = l[2] # c m = l[1:] # [ b, c, d ] l[2] = z # [ a, b, z, d ] l[0:2] = [ x, y ] # [ x, y, z, d ] (Learning Python, chapter 6) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries List methods Lists also have some useful methods append adds an item to the list extend adds multiple items sort orders a list in place Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries List methods Lists also have some useful methods append adds an item to the list extend adds multiple items sort orders a list in place l = [ x, y, z, d ] l.sort() # [ d, x, y, z ] l.append( q ) # [ d, x, y, z, q ] l.extend([ r, s ]) # [ d, x, y, z, q, r, s ] l.append([ v, w ]) # [ d, x, y, z, q, r, s, [ v, w ]] Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Dictionaries Dictionaries are Addressed by key, not by offset Unordered collections of arbitrary objects Variable length, heterogenous (can contain any type of object), nestable Mutable (can change the elements, unlike strings) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Dictionaries Dictionaries are Addressed by key, not by offset Unordered collections of arbitrary objects Variable length, heterogenous (can contain any type of object), nestable Mutable (can change the elements, unlike strings) Think of dictionaries as a set of key:value pairs Use a key to access its value (Learning Python, chapter 7) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Dictionary example level = { icl : 9, nlssd : 11, inf2b : 8} x = level[ nlssd ] # 11 n = len(level) # 3 flag = level.has key( inf2b ) # True l = level.keys() # [ nlssd, inf2b, icl ] level[ dil ] = 11 # { dil : 11, nlssd : 11, inf2b : 8, icl : 9} level[ icl ] = 10 # { dil : 11, nlssd : 11, inf2b : 8, icl : 10} l = level.items() # [( dil, 11), ( nlssd, 11), ( inf2b, 8), ( icl, 10)] l = level.values() # [11, 11, 8, 10] Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Notes on dictionaries Sequence operations don t work: dictionaries are mappings, not sequences Dictionaries have a set of keys: only one value per key Assigning to a new key adds an entry Keys can be any immutable object, not just strings Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Notes on dictionaries Sequence operations don t work: dictionaries are mappings, not sequences Dictionaries have a set of keys: only one value per key Assigning to a new key adds an entry Keys can be any immutable object, not just strings Dictionaries can be used as records Dictionaries can be used for sparse matrices Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Other objects Tuples: like lists, but immutable (cannot be changed) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Other objects Tuples: like lists, but immutable (cannot be changed) emptyt = () T1 = (1, 2, 3) x = T1[1] n = len(t1) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Other objects Tuples: like lists, but immutable (cannot be changed) emptyt = () T1 = (1, 2, 3) x = T1[1] n = len(t1) Files: objects with methods for reading and writing to files Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Other objects Tuples: like lists, but immutable (cannot be changed) emptyt = () T1 = (1, 2, 3) x = T1[1] n = len(t1) Files: objects with methods for reading and writing to files fil = open( myfile, w ) fil.write( hello file\n ) fil.close() Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Numbers and variables Strings Lists Dictionaries Other objects Tuples: like lists, but immutable (cannot be changed) emptyt = () T1 = (1, 2, 3) x = T1[1] n = len(t1) Files: objects with methods for reading and writing to files fil = open( myfile, w ) fil.write( hello file\n ) fil.close() f2 = open( myfile, r ) s = f2.readline() # hello file\n t = f2.readline() # (Learning Python, chapter 7) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Conditionals Loops if tests course = icl if course == icl : print Ewan / Steve elif course == dil : print Phillip else: print Someone else Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Conditionals Loops if tests course = icl if course == icl : print Ewan / Steve elif course == dil : print Phillip else: print Someone else Indentation determines the block structure Indentation to the left is the only place where whitespace matters in Python Indentation enforces readability Tests after if and elif can be anything that returns True/False (Learning Python, chapter 9) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Conditionals Loops while loops A while loop keeps iterating while the test at the top remains True. a = 0 b = 10 while a < b: print a a = a + 1 Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Conditionals Loops while loops A while loop keeps iterating while the test at the top remains True. a = 0 b = 10 while a < b: print a a = a + 1 s = icl while len(s) > 0: print s s = s[1:] (Learning Python, chapter 10) Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Conditionals Loops for loops for is used to step through any sequence object l = [ a, b, c ] for i in l: print i Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Conditionals Loops for loops for is used to step through any sequence object l = [ a, b, c ] for i in l: print i sum = 0 for x in [1, 2, 3, 4, 5, 6]: sum = sum + x print sum Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

Conditionals Loops for loops for is used to step through any sequence object l = [ a, b, c ] for i in l: print i sum = 0 for x in [1, 2, 3, 4, 5, 6]: sum = sum + x print sum range() is a useful function: range(5) = [0, 1, 2, 3, 4] range(2, 5) = [2, 3, 4] range(0, 6, 2) = [0, 2, 4] Steve Renalss.renals@ed.ac.uk to Programming in Python (1)

to Python Python programs and modules Basic objects: numbers, strings, lists, dictionaries Control flow: if, while, for Steve Renalss.renals@ed.ac.uk to Programming in Python (1)