1 / 16

Lists

Lists. [] First = [1, 2, 3, 4] Second = list[range(1,5)] >>>First [1, 2, 3, 4] >>>Second [1, 2, 3, 4]. Tuples. () A tuple is a type of sequence that resembles a list, except that, unlike a list, a tuple is immutable. Tuples. >>>fruits=(“ apple ”,“ banana ”) >>>fruits

kiet
Télécharger la présentation

Lists

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. Lists • [] • First = [1, 2, 3, 4] • Second = list[range(1,5)] • >>>First • [1, 2, 3, 4] • >>>Second • [1, 2, 3, 4]

  2. .

  3. Tuples • () • A tuple is a type of sequence that resembles a list, except that, unlike a list, a • tuple is immutable.

  4. Tuples • >>>fruits=(“apple”,“banana”) • >>>fruits • ('apple','banana') • >>>meats=(“fish”,“poultry”) • >>>meats • ('fish','poultry') • >>>food=meats+fruits • >>>food • ('fish','poultry','apple','banana') • >>>veggies=[“celery”,“beans”] • >>>tuple(veggies) • ('celery','beans')

  5. Tuples • You can use most of the operators and functions used with lists in a similar fashion with tuples. For the most part, anytime you foresee using a list whose structure will not change, you can, and should, use a tuple instead. • range!

  6. Dictionaries • Don’t care about order – care about the association • A dictionary organizes information by association, not position. • Sometimes called hash tables, association lists, key/value or name/value pairs • In Python, a dictionary associates a set of keys with data values. • key/value pairs separated by commas: these pairs are sometimes called entries. • The entire sequence of entries is enclosed in curly braces ({ and }). A colon (:) separates a key and its value.

  7. Adding Keys and Replacing Values • You add a new key/value pair to a dictionary by using the subscript operator []. • The form of this operation is the following: • <adictionary>[<akey>]=<avalue> • The next code segment creates an empty dictionary and adds two new entries: • >>>info={} • >>>info[“name”]=“Sandy” • >>>info[“occupation”]=“hacker” • >>>info • {'name':'Sandy','occupation':'hacker'}

  8. The subscript is also used to replace a value at an existing key, as follows: • >>>info[“occupation”]=“manager” • >>>info • {'name':'Sandy','occupation':'manager'} • >>>

  9. You can also use the subscript to obtain the value associated with a key. However,if the key is not present in the dictionary, Python raises an error. Here are someexamples, using the info dictionary, which was set up earlier: • >>>info[“name”] • 'Sandy' • >>>info[“job”] • Traceback(mostrecentcalllast): • File“<stdin>”,line1,in<module> • KeyError:'job'

  10. Removing Keys • >>>print(info.pop(“job”,None)) • None • >>>print(info.pop(“occupation”)) • manager • >>>info • {'name':'Sandy'} • >>>print(info.get(“job”,None)) • None

  11. Traversing a Dictionary • For key ininfo: • print(key,info[key]) • >>>grades={90:”A”,80:”B”,70:”C”} • >>>grades.items() • [(80,'B'),(90,'A'),(70,'C')] • For (key,value) in grades.items(): • print(key, value)

  12. Accessing the keys • theKeys=list(info.keys()) • theKeys.sort() • For key in theKeys: • print(key,info[key])

  13. .

  14. .

More Related