• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What are the square brackets around the print line used for?

 
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a Python Newbie.  What are the square brackets surrounding the print line being used for?

 
Marshal
Posts: 4514
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:What are the square brackets surrounding the print line being used for?


I don't code in Python, but I believe that it is equivalent to:
 
Ranch Hand
Posts: 32
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a list comprehension, which is nice Pythonic (and functional) way of processing the items in a list to produce another list, just as Ron says.  Using a print() command inside the comprehension is a bit of a hack, as usually you would use a comprehension to create a new list.  List comprehensions are very useful, as you can do lots with them in very little code e.g. filter items, transform items etc, and they do not modify the original list.


If you run the original comprehension in your Python shell, you get results like this:


The command has printed each element from the original list, and also returned a list containing three "None" items - the values returned from the print() command each time.  Because print() returns nothing, the comprehension returns a list of nothings:




 
kevin Abel
Ranch Foreman
Posts: 883
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron and Christopher,

I'm going back through all 800 of the posts I started.    I missed thanking you for this.  I'll know what comprehension is when I start to learn Python.

Thanks,

Kevin
 
Saloon Keeper
Posts: 27809
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a lot of languages, there exists a standard syntax for declaring (and displaying) complex objects.

The "[]" bracket an array or list (sequential collection)

The "{}" bracket a dictionary entry or object with properties: {name: value} or {name, v1= value1, v2=value2, …} The exact format depends on language and usage.

And, of course in Python, a tuple is bounded by "()".

The syntax is designed to be embeddable, so for example, an array of name/value entries:

   
reply
    Bookmark Topic Watch Topic
  • New Topic