What does it mean when you use for-in and if-in

It’s a for loop.
The variable1 is an ascending integer number. I got that much.
The variable2 is a string with a number that variable1 inject itself as the number?

I have this script and I’m trying to replace assigning shape keys with assigning bones instead.

shapekeys = ob.data.shape_keys.key_blocks
for j in shapekeys:
j.name = names
if shape.name in names

If you look at https://docs.blender.org/api/current/bpy.types.Key.html#bpy.types.Key you see that shapekeys in the code you cite is a bpy_prop_collection of ShapeKey.
So the code “for j in shapekeys:” will be a loop where j gets assigned each element ShapeKey of the collection in turn. That ShapeKey element has a “name” field (a string), so it can be assigned, as it is here.

The rest of the code is hard to understand without context. What is “names”? It looks like it should be a string, since it assigned to j.name. But the code “if shape.name in names” is hard to figure out. What is “shape” in this code, and consequently what is “shape.name”?

The expression “x in y” in Python can mean a number of things, depending on what y is. If y is a string, as it seems to be here, then it treats the string as a vector of characters, and asks “is the character shape.name one of the characters in names?”. I rather doubt that shape.name is a character, so I am just left confused by this code.

1 Like

for/in =
select object X within the defined

if/in =
if X is included within Y, then

Am I getting this right?

Your description is a bit short to say if you really got it.

The main aspect is that the IN makes a sequences content accessible easily. So you can loop through all of them and do something with it with FOR or do a presence check for the sequences elements with IF.

Have a look here:

1 Like

It sounds like you have it.

Besides sequences and sets, you can also use for/in and if/in with the second thing being a dictionary.

If d is a dictionary then

for x in d =
do the body for each key in the dictionary d

2 Likes

I was looking for this but no good luck to me who is using Google to find “if” and “in” and “for”
I never heard of w3school.com is but is seems to make learning Python the way it should be thought.
Thanks

1 Like

I think nothing better than plain English with indentation to explain it.

for each egg in the eggtray:
    if egg is rotten:
        throw egg away
    else:
        put egg in frying pan

if there are too many eggs in pan:
    eat anyway
else:
    eat

if stomach is aching:
    while egg in stomach:
        vomit egg

edit:
okay, thinking a bit more, maybe thats not a good example.

1 Like