Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules: Forum rule Nº 15 is strictly enforced in this subforum.
#216136 by Tonakai
Tue Dec 25, 2012 6:25 pm
Hey guys. I've recently started learning Python from a book, and using the tutorials on this site, but I'm kind of curious about a few things. From the information I've read, I can't quite get a clear answer;

I understand Tuples to be a form of list that isn't modifiable in the same way that a list is (i.e removing or adding items), so what would be the benefit of using them? Surely using a list would be more beneficial most of the time as they can be modified to suit whatever purpose at the time? I also read about turning integers into strings, what would the point of this be when you could easier create a string and keep them separate?

Also, when creating a for loop, would I be correct in assuming that you set the variable during the line of code, so it could be anything at all? for example:

Code: Select allfor variable_possibly in example_list:
    print:(variable_possibly)


Sorry if my questions are ridiculous, but I've only been learning for about a week, so I'm incredibly green. Cheers for your time.

Edit: There may be more questions as I continue learning. :shock:

Edit2: I've done my research and found my answers. Though, I still may ask other questions in future.
Advertising
#217189 by Acid_Snake
Thu Dec 27, 2012 7:21 pm
I know you have found your answers but I'll answer the questions anyways in case someone else reads this

Tonakai wrote:I understand Tuples to be a form of list that isn't modifiable in the same way that a list is (i.e removing or adding items), so what would be the benefit of using them?

mostly to define more than one variable in a line of code:
Code: Select allx, y, z = "this", "that", "whatever"

or for functions that return more than one thing:
Code: Select alldef any_function():
   return 1, 2, 3

and when calling it:
Code: Select allx, y, z = any_function()


Tonakai wrote:I also read about turning integers into strings, what would the point of this be when you could easier create a string and keep them separate?

mainly to concatenate an integer into a string, if you do this:
Code: Select allx = "Hello" + 1

python will throw an error, so the correct way would be to convert that integer into a string:
Code: Select allx = "Hello" + str(1)


Tonakai wrote:Also, when creating a for loop, would I be correct in assuming that you set the variable during the line of code, so it could be anything at all? for example:

yes, when you use the for loop, variable_possibility is defined right there, no need to define it before (and you shouldn't define it before)
Advertising

Who is online

Users browsing this forum: No registered users and 0 guests