1

I leaned C of the last summer from K&R book from 1989. I am now learning python3.

I am a little confused about something.

In C if i do a test

if !(.....) The '!' changes the value in the '( )' to the opposite, so if it was true, it becomes false and vis versa.

So what i was trying to do, is change this from C to python.

int card(long long number, int size){
    if(!(size == 13 || size == 15 || size == 16)) { // test to see if valid size.
        printf("INVALID\n");
        return 0;
    }

When i tried

def card(number, size):
    if !(size == 13 or size == 15 or size == 16):
        print("INVALID")
        return 0

I got a syntax error.

So after searching on-line I found "is not" in the python3 doc.

so i try it on the interactive terminal.

   >>> if is not (size == 13 or size == 15 or size == 16):
  File "<stdin>", line 1
    if is not (size == 13 or size == 15 or size == 16):
        ^

The interpreter was saying the 'is' is a syntax error. So i decided to remove it and see what happens. And it worked!

I searched on-line and i do not find the 'not' without the 'is'?

Is it OK to use 'if not ( ......)' as the equivalent of 'if !(......)' in C? Or will i get into problems? Or is there a different python way of doing this?

Manny_Mar
  • 398
  • 2
  • 13
  • 1
    Just simply do `if not (...):`; In python `is` is the identity comparison operator (i.e. check whether the value assigned to a given variable is literally the same as the other). – metatoaster Apr 24 '17 at 03:33
  • Also that `is not` is also an operator. Related: https://stackoverflow.com/questions/18275616/what-does-is-operator-do-in-python – metatoaster Apr 24 '17 at 03:38

2 Answers2

1

Is it OK to use 'if not ( ......)' as the equivalent of 'if !(......)' in C?

Sure. Try it.


However, I think you're missing the in keyword.

You can write this

if(!(size == 13 || size == 15 || size == 16)) { 

As this

if size not in {13, 15, 16}:
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Never thought of using a dictionary to do the comparison. In C there is no such class similar to a dictionary. If i used an array, it would only compare the first element, unless i used a "for loop" and index to test each one item, since an array is really just a pointer to the beginning of a continues block of memory, and using it name without an index, get you to index 0.
    your example worked fine.
    if size not in {13, 15, 16}:
    I thought python dictionaries where value pairs such as: dict = {'Name': 'Zara'} Your answer did work, but it's not value pairs, can you explain that?
    – Manny_Mar Apr 24 '17 at 15:18
  • It's not a dictionary, it's a set – OneCricketeer Apr 24 '17 at 15:19
  • You can also use a list. `if size not in [13, 15, 16]` – OneCricketeer Apr 24 '17 at 15:19
  • I thought { } meant dictionary. [ ] was a list and ( ) was a tuple – Manny_Mar Apr 24 '17 at 15:22
  • Thanks. I have not seen sets yet. I am up to dictionaries in the tutorial i am taking. – Manny_Mar Apr 24 '17 at 16:02
  • I'd recommend that official tutorial rather than something like "learn python the hard way" – OneCricketeer Apr 24 '17 at 16:20
0

"is" means two variables point the same object,the same Memory address U can see the link Is there a difference between `==` and `is` in Python?

Community
  • 1
  • 1
Yang MingHui
  • 380
  • 4
  • 14