Pin It

Comparing Python Strings Lists Dictionaries Tuples and Sets

 If you are a beginner you would not understand much on this page until you go through quite a few lessons on strings, lists, dictionaries and tuples.

However this is a good reference page for the python course, to double check for proper syntax.

Syntax of a string, list, dictionary, tuples or set

myString = 'abcdefg'

myList = ["string item",2,True, [1,2,3]]

myDictionary ={"key1":"value1","key2":"value2"}

myTuple = ('a',True,123)

mySet = set()

Indexing a string, list, dictionary, tuples or set

myString = 'abcdefg'
print(myString[0])

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py
a
C:\Users\User\Desktop\PROJECT PYTHON>

 

aPythonList = ['a','b','c']
print(aPythonList[0])

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py
a
C:\Users\User\Desktop\PROJECT PYTHON>

myDictionary ={'key1':'value1','key2':'value2','key3':{'subKey1':'subValue1'}}
print(myDictionary['key3'])

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py
{'subKey1': 'subValue1'}
C:\Users\User\Desktop\PROJECT PYTHON>

myTuple = (1,2,3)
print(myTuple[0])

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py
1
C:\Users\User\Desktop\PROJECT PYTHON>

Slicing a string, list, dictionary, tuples or set

myString = 'abcdefg'
print(myString[2:5])

OUTPUT:

C:\Users\User\Desktop\PROJECT PYTHON>index.py
cde
C:\Users\User\Desktop\PROJECT PYTHON>

 

 

myList = ['a','b','c', 'd', 'e']
print(myList[1:])

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py
['b', 'c', 'd', 'e']
C:\Users\User\Desktop\PROJECT PYTHON>


OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py

C:\Users\User\Desktop\PROJECT PYTHON>

 

 


OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py

C:\Users\User\Desktop\PROJECT PYTHON>

 

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py

C:\Users\User\Desktop\PROJECT PYTHON>

To add or change an element of a string, list, dictionary, tuples or set

CANNOT BE DONE 
Strings are immutable (cannot be changed)

 

myList = ['a','b','c']
myList[0] = 'z'
print(myList)
print() # adding a space
myList[0] = 1
print(myList)

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py
['z', 'b', 'c']
[1, 'b', 'c']
C:\Users\User\Desktop\PROJECT PYTHON>

myDictionary ={'door1': 'pizza', 'door2': 'eggs', 'door3': 'fries'}
myDictionary['door1'] = 'burger'
print(myDictionary['door1'])

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py
burger
C:\Users\User\Desktop\PROJECT PYTHON>

 

 

CANNOT BE DONE
Tuples are immutable (cannot be changed)

 

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py

C:\Users\User\Desktop\PROJECT PYTHON>

To find out the length of a string, list, dictionary, tuples or set

myString = 'abcdefg'
print(len(myString))

OUTPUT:

 C:\Users\User\Desktop\PROJECT PYTHON>index.py
7
C:\Users\User\Desktop\PROJECT PYTHON>

 

myList = ['a','b','c','d','e']
print(len(myList))

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py
5
C:\Users\User\Desktop\PROJECT PYTHON>

myDictionary ={'key1':'value1','key2':'value2','key3':{'subKey1':'subValue1'},'key4':'value4'}
print("The length of this tuple is: ", len(myDictionary))

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py
The length of this tuple is: 4
C:\Users\User\Desktop\PROJECT PYTHON>

 

 

myTuple = ('string',True,5)
print("The length of this tuple is: ", len(myTuple))

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py
The length of this tuple is: 3
C:\Users\User\Desktop\PROJECT PYTHON>

 

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py

C:\Users\User\Desktop\PROJECT PYTHON>

To slice a string, list, dictionary, tuples or set

myString = 'abcdefg'
print(myString[2:])

OUTPUT:

 C:\Users\User\Desktop\PROJECT PYTHON>index.py
cdefg
C:\Users\User\Desktop\PROJECT PYTHON>

 

myList = ['a','b','c', 'd', 'e']
print(myList[1:])

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py
['b', 'c', 'd', 'e']
C:\Users\User\Desktop\PROJECT PYTHON>


OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py

C:\Users\User\Desktop\PROJECT PYTHON>

 

 

 

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py

C:\Users\User\Desktop\PROJECT PYTHON>

 

OUTPUT:
C:\Users\User\Desktop\PROJECT PYTHON>index.py

C:\Users\User\Desktop\PROJECT PYTHON>