This code works:
if '2011' in baby_names:
print('Found 2011')
if 1 in baby_names[2012]:
print('Found Rank 1 in 2012')
else:
print('Rank 1 missing from 2012')
if 5 in baby_names[2013]:
print('Found Rank 5')
Why is 2011 in quotes (Line 1) whereas 1 and 5 (Lines 4 and 9, respectively) are not? This trips me up frequently. Thanks!
what is baby_names? Probably list or dict
'2011'
- in this case its just string '2011' (i.e. str
object) and you look to find it in baby_names
if baby_names is a list, baby_names[2012]
will be element with index 2012. Note that in this case 2012 is int
if baby_names is a dict - baby_names[2012]
is the value for key 2012. Again, in this case 2012 is int
, but the key as well may be str
and then the use will be baby_names['2012']
but this all is best guess as we don't know what baby_names
is
(Aug-05-2020, 01:40 PM)Mark17 Wrote: [ -> ]Why is 2011 in quotes (Line 1) whereas 1 and 5 (Lines 4 and 9, respectively) are not? This trips me up frequently. Thanks!
Do you understand that values have different types? As stated by buran,
'2011'
(or
"2011"
for that matter) is of type string, while the values 1 and 5 are of type integer. Different types have different operations - strings can be concatenated, sliced, searched, etc., while integers can be added, multiplied, etc. Values of different types won't be equal, so for example,
'2011' == 2011
evaluates to
False
.
I don't think you code works. In this example I use integers as keys to a dictionary.
numbers = {1:'one', 2:'two', 3:'three'}
print(1 in numbers, '1' in numbers)
print(numbers[2])
print(numbers['2'])
When I run the program I get a KeyError exception when trying to use a string as a key because the string '2' is not a key in the dictionary, the integer 2 is. If I used strings for keys instead of integers I would also get a key error, just in a different place. Integers and strings are not the same even if they look very similar.
Output:
True False
two
Traceback (most recent call last):
File "C:\python_musings\junk.py", line 5, in <module>
print(numbers['2'])
KeyError: '2'
Notice that
'1' in numbers
does not raise an exception, but it is False even though the dictionary contains an entry for 1. Do you ever see the message "Found 2011"?
Thanks for the feedback, everyone. I'll study and make sense out of it.
baby_names is a dictionary. 2011 is the year, and I tend toward thinking of that as integer even though it's string. 1 and 5 are name ranks (by popularity).
'2011 in baby_names will be True if there is key
'2011' (i.e.
str) in keys of baby_names. The keys are
int` (based on next examples), so this will be False.
1 in baby_names[2012]
- the value for key 2012
will be again dict, so it will look for key 1 in the keys of that dict.
simplest answer... as stated by many already. quotes around the number make it a string of that number. no quotes and the number is an int.
so print('11') is the same as print(str(11))
There's no way for us to know why those statements evaluate as true without knowing what's contained in baby_names and what type of object baby_names is