Basic question: can someone explain to me when to use () and when to use []?

python

#1

I'm about halfway through the Python lessons and I keep getting confused between the use of the two different kinds of brackets. Any simple tips to remember?


#2

This is a very broad question which I don't think can be answered by anything less than understanding what you want to do and how you are trying to do it.

The use of brackets depends on a lot of things, from usage to the version of python. (in Python 2.7 print needs no brackets vs Python 3 where it requires "print (' ') ")

Below are examples of some of the ways that brackets can become a bit counter intuitive:
a) calling a module:" hello(varx) or randrange(start, stop, [step]) # we are already starting to mix the brackets here.
b) defining a list: listA = [ ] and defining a list containing tuples: ListB = [(1,3),(4,3),(5,6)] is mixed again.

The use of brackets and types of brackets, as you can see, is not easy, but it can be simple.
Just split your code into individual elements and those elements will require a specific application which will tell you the way it needs to be implimented.

Learn the content in the excercises in Codeacademy Python, remembering how to correctly use strings / lists / dicts / classes (without having to look at a reminder each time) by then, you will be know what brackets you need to use for your task.

(and https://docs.python.org is your friend as well. Hope this helps get you to where you want to be.)


#3

@socially_right,

== discussions / opinions ==
http://stackoverflow.com/questions/9405322/python-array-v-list
http://stackoverflow.com/questions/626759/whats-the-difference-between-list-and-tuples


#4

thanks for these discussions. lots of good points of clarification.


#5

excellent advice. i was thinking it was something more definitive that i was missing, but i see now that it's not so black and white and taking each step in turn will help to point me in the right direction.