Len / print issues

sam_dict = {
    "mouth": "Mund",
    "finger": "Finger",
    "leg": "Bein",
    "hand": "Hand",
    "face": "Gesicht",
    "nose": "Nase"
}
sam_dict['Mouth'] = 'Pie Hole'
#   sam_dict.append = sam_dict = {}
# !!! THIS DOES NOT WORK BECAUSE DICTIONARY keys ARE IMMUTABLE #CAN'T BE CHANGED!!!!
#  (HINT: GOOGLE IMMUTABLE AND RESEARCH IT :))
#   THIS IS WHAT THE ERROR LOOKS LIKE:
# Traceback (most recent call last):
#   File "C:\Users\mitzl\PycharmProjects\pythontraining\Dict.py", line 10, in <module>
#     sam_dict.append = '{}'
# AttributeError: 'dict' object has no attribute 'append'
# *** YOU cannot HAVE DUPLICATE KEYS ***
# *** YOU can HAVE DUPLICATE VALUES ***
# *** YOU cannot CALL A KEY BY THE VALUE example. print('Mund')  ***
# RATHER THAN CALLING AN INDEX OF A DICTIONARY,
# YOU CALL THE KEY BY PUTTING IT IN SQUARE BRACKETS ['Mouth']



# edit dictionaries by:

sam_dict['john'] = 'male'
sam_dict['ben'] = 'male'
print(sam_dict)
# {'john': 'male', 'ben': 'male'}

# or update this way

sam_dict.update({'john': 'they'})
print(sam_dict)
#{'john': 'they', 'ben': 'male'}

len(sam_dict)

this is the result:
{‘mouth’: ‘Mund’, ‘finger’: ‘Finger’, ‘leg’: ‘Bein’, ‘hand’: ‘Hand’, ‘face’: ‘Gesicht’, ‘nose’: ‘Nase’, ‘Mouth’: ‘Pie Hole’, ‘john’: ‘male’, ‘ben’: ‘male’}
{‘mouth’: ‘Mund’, ‘finger’: ‘Finger’, ‘leg’: ‘Bein’, ‘hand’: ‘Hand’, ‘face’: ‘Gesicht’, ‘nose’: ‘Nase’, ‘Mouth’: ‘Pie Hole’, ‘john’: ‘they’, ‘ben’: ‘male’}

I’m trying to understand why it won’t give he the ‘len’ of ‘sam-dict’.

Maybe you have forgotten to print() it?

print( len(sam_dict) )
2 Likes

This worked, thank you. It also opened up another question though because my instructor video only has:

grades = {'John': 'A', 'Ben': 'C'}
print(grades)
{'John': 'A', 'Ben': 'C'}
len(grades)
2

He is using Jupyter Notebook and I’m using Pycharm, so now I’m wondering if everything i’m learning is slightly different depending on the program I’m typing it into… Please say no :frowning: my brain can’t handle that LOL.

It could very well be that what you’re seeing is the Python shell, rather than a script…

>>> grades = {'John': 'A', 'Ben': 'C'}
>>> print(grades)
{'John': 'A', 'Ben': 'C'}
>>> len(grades)
2

In that case, the len() function is being run directly, rather that being passed to the print() function in a script.

edit to add…

It’s a little confusing, if/when the two are mixed, as in your example, because simply entering grades in the shell, will output the exact same thing as passing that variable to the print() function.

When you use Jupyter Notebook, JupyterLab or a similar environment you will see code cells followed by their output. The output is either the printed text or the value of the last expression in the code cell. So it would look for example like this:


grades = {'John': 'A', 'Ben': 'C'}
print(grades)
{'John': 'A', 'Ben': 'C'}

len(grades)
2

Or in real JupyterLab it looks like this:
image

I have never used PyCharm but VS Code provides interface for Jupyter Notebooks. So you can have similar experience there as in the web interface of JupyterLab.

1 Like

Just to make it clear:

Interactive shells(or consoles ) like Idle Shell automatically execute your code and print out what it returns[1]. So that you don’t need to print your result manually. (Please note that you can enter only one expression[2] at a time in usual* interactive shells.)

But; when it comes to “classic executing” or “file executing” (both are not technical terms), your computer does EXACTLY what your code file says. So, it doesn’t make extra print() calls.

Video owner was coding in a kind of interactive shell(a Jupyter Notebook); but you were working with a FILE. That is why there is a difference between two outputs.

Note: If you have installed Python from its official site(Download Python | Python.org), you may access Idle Shell via typing “idle” to your computer’s search bar. Also, you can visit this page to learn how to to open the PyCharm Console.


*: To me, an “unusual interactive shell” is a shell which behaves like a Jupyter Notebook: it lets you to enter multiple expresions at a time but prints (only) the result of the last one. (there is not a massive difference, though)


  1. or what your code is equal to; but it’s not that simple in some cases ↩︎

  2. this expression may be a function call, a function definition, an if block, a for loop etc. ↩︎

1 Like