Download presentation
Presentation is loading. Please wait.
Published byNorman Daniels Modified over 9 years ago
1
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures
2
Review Some things we saw in lab –String slice is like Java substring, e.g. s[2:7] –Range is a function that returns a list, and we can iterate over this list: for j in range(5, 10): … –Formatting real numbers print “Answer is {0:4.1f} feet.”.format(ans) –Syntax error: cursor takes you location –Initialize your variables: Interpreter remembers value from previous run! (Even if you earlier ran a different program)
3
Chapter 2 ** and // operators We have += but not ++ Boolean operators are words List : dynamic array. –Commonly used functions are: append, insert, remove –Can use [ ] to access/change single element. –See handout
4
Strings Some operations –Length: use len(stringName) integer –indexOf: use stringName.find(pattern) integer –charAt: just use brackets –Substring: just use brackets Note that you can leave one of the arguments empty: s[4:] means to go to the end –String compare: can use ==, > and <.
5
File I/O File output outFile = open(“output.txt”, “w”) outFile.write(“help me\n”) outFile.close() File input (see CountLines.py) inFile = open(“input.txt”, “r”) for line in inFile:... inFile.close() # Good idea to close file, as interpreter may # remember where you left off next time!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.