Advertising (This ad goes away for registered users. You can Login or Register)

A few questions regarding Python?

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.
Locked
Tonakai
Posts: 594
Joined: Sun Aug 05, 2012 3:00 am
Location: United Kingdom.

A few questions regarding Python?

Post by Tonakai »

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 all

for 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
Acid_Snake
Retired Mod
Posts: 3100
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: A few questions regarding Python?

Post by Acid_Snake »

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 all

x, y, z = "this", "that", "whatever"
or for functions that return more than one thing:

Code: Select all

def any_function():
	return 1, 2, 3
and when calling it:

Code: Select all

x, 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 all

x = "Hello" + 1
python will throw an error, so the correct way would be to convert that integer into a string:

Code: Select all

x = "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
onnz423
Posts: 147
Joined: Sat Dec 29, 2012 4:25 pm
Location: Finland

Re: A few questions regarding Python?

Post by onnz423 »

I really want to practice python but im stuck in HTML :D
Image
Image
Image
Image
Xian Nox
Retired Mod
Posts: 2744
Joined: Fri Nov 05, 2010 5:27 pm
Location: Over the hills and far away

Re: A few questions regarding Python?

Post by Xian Nox »

onnz423 wrote:I really want to practice python but im stuck in HTML :D
This makes no sense.
onnz423
Posts: 147
Joined: Sat Dec 29, 2012 4:25 pm
Location: Finland

Re: A few questions regarding Python?

Post by onnz423 »

Xian Nox wrote:
onnz423 wrote:I really want to practice python but im stuck in HTML :D
This makes no sense.
yea i know im very tired :D
Image
Image
Image
Image
Locked

Return to “Programming and Security”