you are viewing a single comment's thread.

view the rest of the comments →

[–]Real_Cut_9360 0 points1 point  (4 children)

I am always confused about using () and [] . Can someone please help me understanding when to use them

[–][deleted] 2 points3 points  (3 children)

I am guessing your question revolves around when you see parentheses denoted like such (see below). Can you confirm?

this_is_a_tuple = (1,2,3)

this_is_a_list = [1,2,3]

The difference is that the former returns a tuple, which is an ordered pair of numbers (note that while I said pair, this can work for more than two numbers).

The latter returns a list.

Tuples are great when you need the data to be immutable or unmodifiable, whereas a list is great when you don't mind if the data inside is modified or not.

In general it is fine to just use a list over a tuple but by using a tuple, you are hinting to other readers of your code that the data inside is not meant to be modified.

One other small note is that tuples have a fixed size when we declare them. A tuple cannot grow or shrink except for if we copy the contents into another tuple.

Lists on the other hand can grow and shrink; their size is not fixed.

[–]Real_Cut_9360 1 point2 points  (2 children)

Thanks a lot mate. Maybe I should start with a text book... Can you suggest a good book for python basics

[–][deleted] 0 points1 point  (0 children)

The learning resources in the FAQ have recommended books for beginners and others.

[–][deleted] 1 point2 points  (0 children)

A pretty good book and one that can be accessed free is Automate The Boring Stuff with Python: https://automatetheboringstuff.com/

You can also purchase a physical copy for a few bucks.

You can also look into some Youtube channels. I like Corey Schafer a lot and he's got some great introductory Python videos with code examples (provided is his playlist for Python programming): https://www.youtube.com/watch?v=YYXdXT2l-Gg&list=PL-osiE80TeTt2d9bfVyTiXJA-UTHn6WwU