-2

I want to create a list from 1 to 52 inclusive. (E.g. for a deck of cards) I was just wondering how do you make a list inclusive?

I know how to make a list, just am not sure how to make it inclusive. I searched on here for awhile and couldn't really find anything helpful.

ntg
  • 12,950
  • 7
  • 74
  • 95
Wiggs
  • 51
  • 1
  • 2
  • 10

2 Answers2

5

Inclusive is a term that is often used when expressing bounds. It means that the range of this bound includes all values including the inclusive value. An example of this would be:

Print all integers 1-5 inclusive. The expected output would be:

1 2 3 4 5

The antonym of this term would be exclusive which would include all values excluding the exclusive value. An example of this would be:

Print all integers 1-5 exclusive. The expected output would be:

1 2 3 4

It is important to note, that in programming generally the first value is inclusive unless otherwise noted. In mathematics this can differ. Inclusive and exclusive ranges are often denoted with [] and ().

What does this square bracket and parenthesis bracket notation mean [first1,last1)?

Community
  • 1
  • 1
Preston Martin
  • 2,789
  • 3
  • 26
  • 42
1

This will create list with [0,51] values

list_of_cards = [i for i in range(52)] 

This will create list with [1,52] values (This is what you want to use):

list_of_cards = [i for i in range(1,53)]
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Tuc3k
  • 1,032
  • 9
  • 12
  • 1
    Good use of list comprehension. Thank you for the help. – Wiggs Oct 06 '16 at 16:32
  • Why *list comprehension* when range itself returns list? – Moinuddin Quadri Oct 06 '16 at 16:40
  • 1
    @MoinuddinQuadri: range returns a list only in Python 2.x. In Python 3.x it returns a range object, which is more like a generator than a list in that it takes less memory. It can be converted to a list with `list(range(1,53))` or a list comprehension. (The tag in the OP is Python 3.x.) – Rory Daulton Oct 06 '16 at 16:55