Do you get content of oper right before trying it out?

oper = ['plus', 'minus', 'times']
oper.extend('divide')

How about do you know what happens when you run this? Big bang? ;)

print('a'
      'b'
      'c')

And this is same, isn't it`?

c = 'c'
print('a'
      'b'
      c)

Dani AI

Generated

Two small, separate gotchas are behind the original examples. As hinted, list.extend expects an iterable — a string is iterable, so its characters are appended one by one. The string-print quirk seen by and explained by is different: adjacent string literals in source are concatenated at compile time; variables are not joined automatically.

A quick illustration of list behavior:

lst = [1, 2]
lst.extend([3])      # -> [1, 2, 3]
lst.append([3])      # -> [1, 2, [3]]
lst.extend('hi')     # -> [1, 2, 'h', 'i']

Notes: passing a dict to extend will iterate the dictionary (yielding keys). To add a whole string as a single element, wrap it in a sequence (for example, lst.extend(['hello']) or use lst.append('hello')).

String concatenation in source vs runtime can be handled in a few safe ways:

name = 'X'
print('hello' 'world')          # literal concat at compile time -> 'helloworld'
print('hello' + name)           # runtime concatenation -> 'helloX'
print(''.join(['hello', name])) # robust when joining many pieces
print(f"hello{name}")           # modern Python (3.6+)

Troubleshooting tips from ’s direction: inspect what an iterable produces before extending — list(some_iterable) or a simple loop printing repr(item) makes the behavior obvious. help(list.extend) or dir() quickly confirms expected signatures. Finally, be mindful of Python 2 vs 3 print differences (statement vs function) when reading older examples.

Recommended Answers

All 3 Replies

I dont know why but extend adds 'devide' as another list to oper.
I couldn't find "extend" function for lists!

Strange and amazing, using new line in second example somehow works like a "+" opperator. but fails when we use a variable instead of a string.

the below works as the fist one:

c = 'c'
print('a'
         'b'+
         c)

Here's the extend part from the Python Documentation:
Extend.
As for

print ('a'
       'b'
       'c')

it's the same with

print ('a' 'b' 'c')

as for the

c='c'
print ('a'
       'b'
        c)

think of it as

c='c'
print ('a' 'b' c)

where c is a variable, and not a given character. Even if you declared c as a string/character, you can't print them together as for

c='c'
print ('a' 
       'b' 
       '{}'
       ).format(c)

#or
c='c'
print ('a' 'b' '{}').format(c)
#it's the same with the first print.

I dont know why but extend adds 'devide' as another list to oper

No.

I couldn't find "extend" function for lists

Have you even looked?
Help from interactive interpeter.

>>> help(oper.extend)
Help on built-in function extend:

extend(...)
    L.extend(iterable) -- extend list by appending elements from the iterable

The keyword here is iterable.
Here is an other one with extend,this one is harder to guess and is it possible to guess exact output?

l = [1, 2, 3]
d = {4: 5, 6: 7, 8: 9}
l.extend(d)
print l #?
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.