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