Python Forum
Quotes vs. no quotes around numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quotes vs. no quotes around numbers
#1
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!
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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.
Reply
#4
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"?
Reply
#5
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).
Reply
#6
'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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help on how to include single quotes on data of variable string hani_hms 5 1,892 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  Reading list items without brackets and quotes jesse68 6 4,525 Jan-14-2022, 07:07 PM
Last Post: jesse68
  Why does absence of print command outputs quotes in function? Mark17 2 1,342 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 6,930 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  Two types of single quotes Led_Zeppelin 2 1,863 Mar-15-2021, 07:55 PM
Last Post: BashBedlam
  Remove double quotes from the list ? PythonDev 22 8,559 Nov-05-2020, 04:53 PM
Last Post: snippsat
  Correct syntax for a variable inside a quotes: MP4Box command JonnyDriller 2 2,710 Feb-02-2020, 01:22 AM
Last Post: JonnyDriller
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 6,994 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  Quotes and variables in print statement Mark17 4 2,997 Sep-13-2019, 04:07 PM
Last Post: Mark17
  removing quotes from a list and keep type list evilcode1 3 2,279 Aug-03-2019, 11:07 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020