About

Swaroop C H is 29 years of age. He is a coder and startupper. He has previously worked at Yahoo!, Adobe, his own startup and Infibeam.

Read more about him

Views
Personal tools
COLLECTION
Collection

Talk:Python en:Data Structures

From Notes

Jump to: navigation, search

Maybe there should be some recursion (or what's it called?) in the example for Tuple, in the part that says:

print('Number of animals in the new zoo is', len(new_zoo))

The line in the output says: " Number of animals in the new zoo is 3"

new_zoo = ('monkey', 'dolphin', zoo)

The number of animals at the new zoo should not be 3, but 5, since zoo has 3 animals (ie. you should add the contents of the zoo tuple to the number, instead of just counting the items with len(new_zoo).

Since I'm a newbie I don't know how to do this myself yet, unfortunately. /regards Mikko.


Swaroop said:

Hi Mikko,

Thanks for the suggestion.

I've modified the example slightly for this situation, but the explanation needs some more work, I think.

Contents

Cannot compile - Using_dict.py

Dear Sir, I am using Linux Mint 6. "python -V" shows Python 2.5.2

I cannot compile this code due to following error;

File "using_dict.py", line 19 for name, address in ab.items(): ^ SyntaxError: invalid syntax

I cannot figure out this error. Please help.

thanks.

Cannot compile - Using_list.py

Dear Sir, I am using Linux Mint 6. "python -V" shows Python 2.5.2

I cannot compile this code due to following error;

File "using_list.py", line 9 print('These items are:', end=' ') ^ SyntaxError: invalid syntax

If I remove "end=", then the following error occurs;

File "using_list.py", line 11 print(item, end=' ') ^ SyntaxError: invalid syntax

I cannot figure out above errors. Please help.

thanks.


Set Theory Taught in School

Hey there,

Whilst I got the example fine, we were never taught set theory in school, so you might want to elaborate a little for others who may not know what it is.

Sets

 bri = set(['brazil', 'russia', 'india'])

now can be:

 bri = {'brazil', 'russia', 'india'}

and will be clearer:)

Zoo code error

 File "./python_tuple.py", line 7
   new_zoo = 
            ^

SyntaxError: invalid syntax

This is what I get when I copy and paste the zoo code and try to run it (as a script). I've been working through your book and following the same procedure for all your other code examples, and it's always worked. I'm running 3.1 on a OSX. If I comment out the new_zoo portion it runs fine.

Are you sure you don't need parentheses? Or is the definition of new_zoo missing? There's no value directly after "new_zoo = "

Honestly it looks like there's code missing.


UPDATE: o.k. looks like this is what the new_zoo line should be:

new_zoo = ('monkey', 'camel', zoo)

then it works.