Cam Allen [email protected] Based on slides by Zhenyu …Sequence Types Operation Result x in s True...
Transcript of Cam Allen [email protected] Based on slides by Zhenyu …Sequence Types Operation Result x in s True...
![Page 1: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/1.jpg)
PythonCam Allen
Based on slides by Zhenyu Zhou, Richard Guo
![Page 2: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/2.jpg)
What is Python?
![Page 3: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/3.jpg)
Language Principles
• Beautiful is better than ugly
• Explicit is better than implicit
• Simple is better than complex
• Complex is better than complicated
• Readability counts
—The Zen of Python
![Page 4: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/4.jpg)
The Interpreter
![Page 5: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/5.jpg)
Running Scripts
![Page 6: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/6.jpg)
Indentation
![Page 7: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/7.jpg)
Indentation Errors
![Page 8: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/8.jpg)
Dynamic Typing
![Page 9: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/9.jpg)
Strings
![Page 10: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/10.jpg)
Lists
![Page 11: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/11.jpg)
Tuples
![Page 12: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/12.jpg)
Sequence Types
Type Example
String s = "Don’t touch that dial!"
List L = [1, 2, 3, 4, 5]
Tuple t = ('Check', 1, 2)
(more) ...
![Page 13: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/13.jpg)
Sequence TypesOperation Resultx in s True if an item of s is equal to x, else False
x not in s False if an item of s is equal to x, else Trues + t Concatenation of s and t
s * n, n * s Equivalent to adding s to itself n timess[i] The i th item of s, starting with index 0
s[i:j] Slice of s from i to js[i:j:k] Slice of s from i to j, with step klen(s) Length of smin(s) Smallest item of smax(s) Largest item of s
s.index(x) Index of the first occurrence of x in ss.count(x) Total number of occurrences of x in s
![Page 14: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/14.jpg)
Dictionaries
![Page 15: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/15.jpg)
Quick Recap
• Python: popular language, for good reasons
• Interactive mode and script mode
• Language basics
• Next up: control flow, functions, classes, modules
![Page 16: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/16.jpg)
Control Flow Statements
![Page 17: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/17.jpg)
Functions
![Page 18: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/18.jpg)
Default Arguments
![Page 19: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/19.jpg)
Classes
![Page 20: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/20.jpg)
Inheritance
![Page 21: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/21.jpg)
Importing Modules
![Page 22: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/22.jpg)
Summary
• Why we’re using Python
• How to use Python
• Language basics
• Building blocks
![Page 23: Cam Allen cam@cs.duke.edu Based on slides by Zhenyu …Sequence Types Operation Result x in s True if an item of s is equal to x, else False x not in s False if an item of s is equal](https://reader034.fdocuments.in/reader034/viewer/2022051911/6001019a3d1deb13132b1dc2/html5/thumbnails/23.jpg)
References• Content is based on slides by Zhenyu Zhou, Richard Guo
• python.org - Official Python website
• Berkeley Python/UNIX tutorial - Available on course webpage
• learnpython.org - Basic tutorials, examples
• A Byte of Python - Beginner’s tutorial
• Oliver Fromme - Python Information and Examples
• tiobe.com - Language popularity index