Last active
April 15, 2020 12:38
-
-
Save lizettepreiss/d370be343e821bb82a20881384288650 to your computer and use it in GitHub Desktop.
Python List, Tuple, Dictionary syntax - simple examples.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# LIST | |
list = ["apple","orange","pear"] | |
for s in list: | |
print(s) | |
print(list[1]) | |
# TUPLE | |
# A tuple is a collection which is ordered and unchangeable | |
thistuple = ("linda", "mary", "jane") | |
print(thistuple) | |
print(thistuple[1]) | |
thistuple = ("jon", "lucy", "andrew", "greg", "fred", "sue", "kevin") | |
print(thistuple[2:5]) | |
thistuple = ("sky", "sun", "cloud") | |
for x in thistuple: | |
print(x) | |
# DICTIONARY | |
class Object1: | |
field1 = "strVal" | |
obj1 = Object1() | |
dict = { | |
'key1':'val1', | |
2:'val2', | |
obj1.field1:"val3" | |
} | |
dict['key4'] = "val4" | |
dict[obj1] = Object1() | |
print(dict['key1']) | |
print(dict[2]) | |
print(dict[obj1.field1]) | |
print(dict['strVal']) | |
print(dict['key4']) | |
print(dict[obj1]) | |
for aKey in dict: | |
print(aKey) | |
print(dict[aKey]) | |
for aKey in dict.keys(): | |
print(aKey) | |
print(dict[aKey]) | |
for aVal in dict.values(): | |
print(aVal) | |
for item in dict: | |
print(item) | |
print(type(item)) | |
for k,v in dict.items(): | |
print(k, " ", v) | |
# Python dictionary method get() returns a value for the given key. | |
dict = {'Name': 'Elke', 'Age': 7} | |
print(dict.get('Age')) | |
print(dict['Age']) | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment