Skip to content

Instantly share code, notes, and snippets.

@RedGhoul
Created December 7, 2017 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RedGhoul/ca9119de76e061bcb2fff034678e89c7 to your computer and use it in GitHub Desktop.
Save RedGhoul/ca9119de76e061bcb2fff034678e89c7 to your computer and use it in GitHub Desktop.
#LISTS
# Can have mixed data types
# lists are mutable, they can have any sort of data type, or a mixture of different data types
mylist = [1,2,3]
mylist = ['random',3,4,2,32323,True,23232323,[2,3,2]]
print(mylist) # output "['random', 3, 4, 2, 32323, True, 23232323, [2, 3, 2]]""
print(mylist[1]) # output "3"
print(mylist[:3]) # also supports slicing, also has the same rules as slicing
#output "['random', 3, 4]"
# can have assignemnts
mylist[0] = "Love you"
print(mylist) # output "['Love you', 3, 4, 2, 32323, True, 23232323, [2, 3, 2]]"
mylist.append("NewItem1") # adds a enity to the end
print(mylist) # output "['Love you', 3, 4, 2, 32323, True, 23232323, [2, 3, 2], 'NewItem1']"
# can also extend the list aka merging another list to the end
mylist.extend([1,2,3,4,22,1,4])
print(mylist) # output "['Love you', 3, 4, 2, 32323, True, 23232323, [2, 3, 2], 'NewItem1', 1, 2, 3, 4, 22, 1, 4]"
#taking things off the list
print(mylist.pop())# pop modifys the list
# output "4"
print(mylist.pop(0))# pop also taking in where you want to take something out
# output "Love you"
print(mylist) # now the list is one value short of what it used to be
# output "[3, 4, 2, 32323, True, 23232323, [2, 3, 2], 'NewItem1', 1, 2, 3, 4, 22, 1]"
print(mylist.reverse())# reverse modifys the list permenantly as well. It does not return anything
print(mylist)
# output "[1, 22, 4, 3, 2, 1, 'NewItem1', [2, 3, 2], 23232323, True, 32323, 2, 4, 3]"
mylist2 = [1,4,"55555",56,73,23,4,[1,2,3]]
print(mylist2.sort()) # by default sorts from lowest to highest. It does not return anything
print(mylist2)
# output "[1, 4, 4, 23, 56, 73, [1, 2, 3], '55555']"
#DICTIONARIES
# Key is always first, value is always second
# Have no order
# Can have different data types in other data types
my_stuff = {"key1":"Value","key2":"Value2","key3":{"123":[1,2,3]}}
print(my_stuff["key1"]) # output "Value"
print(my_stuff["key3"]["123"][0]) # output "1"
# can reassign different values to different pairs
my_stuff2 = {'lunch':'pizza'}
my_stuff2['lunch'] = 'burger'
print(my_stuff2['lunch']) # output "burger"
# tuples are "immutable" sequences
# the syntax difference between this, and a list is that it uses "()"
# rather then "[]"
t = (1,2,30,30)
print(t) # output "(1,2,3)"
#sets are unordered collections of "unique" elements
# if they are the same, then it does not include them
x = set()
x.add(1)
x.add(2)
x.add(3.4)
x.add(4)
x.add(4)
x.add(4)
x.add(5)
print(x) #output "set([1, 2, 3.4, 5, 4])"
# every time you look at them, they come in a new order
# converting a list into a set, takes out all the same numbers
converted = set([1,2,3,4,5,6,7,3,3,3,3,32,"2",2,2]) # cannot do this -> set([1,2,3,4,5,6,7,3,3,3,3,32,2,2,2,2],[12])
print(converted) # outputs "set([32, 1, 2, 3, 4, 5, 6, 7])"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment