1 / 9

Python Camp

Python Camp. Session 6: Data Structures and File Handling Lists File Handling Edit: We actually spent this session looking at for loops and the Turtle module – no slideshow but see Mark’s Introduction to Python book or many and various corners of the Internet for more on Turtle.

laney
Download Presentation

Python Camp

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Camp Session 6: Data Structures and File Handling • Lists • File Handling • Edit: We actually spent this session looking at for loops and the Turtle module – no slideshow but see Mark’s Introduction to Python book or many and various corners of the Internet for more on Turtle.

  2. What’s wrong with this code? player1 = “Dave” player2 = “Angelica” player3 = “Steve” player4 = “Claire”print(player1) print(player2) print(player3) print(player4)

  3. A better version player = [“Dave”, “Angelica”, “Steve”, “Claire”]print(player)This is called a listIt is similar (though subtly different) to an arrayPython doesn’t support arrays, lists are very close and fine for GCSE

  4. Key Rules and Terms • Liste.g. names = [“Graham” , “Eric” , “Terry” , “John” , “Michael”] • Indexe.g. names[1]n.b. Remember to count from 0! • Appende.g. names.append(“Mark”) • ine.g. if “Terry” in names: • positione.g. position = names.index(“Terry”)

  5. File Handling - Reading • Open a file:file = open(“filename” , “mode”)e.g. file = open(“data.csv” , “r”)print(file)input()line = file.readline() # Read just the first lineprint(line)input()data = line.split() # Split the line into a listprint(data)input()

  6. Top Tips • Using readline(), Python keeps track of position • Try:for line in file: print(line) # Or some other processing • Remember to use file.close() to tidy up • Think database – storing data in records is an efficient way to store data

  7. Writing data • file = open(“newTextFile.txt” , “w”) • This will create a blank, empty file (deleting any existing data) • Use file.write() to add data to it • Add your own newline characters (“\n”) – like <br> in HTML • Remember to use file.close()! • file = open(“exisitngTextFile.txt” , “a”) • This will preserve existing data and append to the end of the file • There is no trivial way to edit a file in the middle

  8. Further topics to explore • 2D arrays / lists • Sorting lists • Python specific: • Tuples • Dictionaries

More Related