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?

2
  • 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). Commented Apr 24, 2017 at 3:33
  • Also that is not is also an operator. Related: stackoverflow.com/questions/18275616/… Commented Apr 24, 2017 at 3:38

2 Answers 2

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}:
Sign up to request clarification or add additional context in comments.

6 Comments

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. <br/>your example worked fine.<br/> if size not in {13, 15, 16}: <br/>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?
It's not a dictionary, it's a set
You can also use a list. if size not in [13, 15, 16]
I thought { } meant dictionary. [ ] was a list and ( ) was a tuple
Thanks. I have not seen sets yet. I am up to dictionaries in the tutorial i am taking.
|
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.