Blog
Most people struggle while learning new things, me myself included. And I have encountered 3 very confusing things (at least to me!) during my first week of learning Python.
1. ( ) vs [ ] vs { }
(1) The first confusing thing (to me) is the differences between parentless, bracket and brace. Typically, in math, we always use () first and if we need another(), we then use[] and then followed by {}. However, it’s not how it works in the Python world.
Math example:
1 + 4 = 5
(1 + 4) / 5 = 1
[(1 + 4) / 5] / 5 = 0.2
{[(1 + 4) / 5] / 5} * 5 = 1
(2) In Python, we use () in 3 conditions.
Ⅰ. To call function or method:
-
- int()
- print()
- str()
- bool()
Ⅱ. To group expressions:
-
- (1+4)/5
III. To construct/disconstruct ‘tuples’:
-
- a = (1 , 2)
- a = (‘Hello’ , ‘World’)
- a = (‘Hello’ , 1 , ‘World’, 2)
(3) We use [] in 2 conditions.
Ⅰ. To index specific items in a tuple or list:
-
- print(a[1])
Ⅱ. To construct/deconstruct lists:
-
- a = [1, 2, 3]
- a = [‘Hello’, 1, ‘World’, 2]
(4) We use {} for dictionary.
-
- a = {1: ‘Hello’, 2: ‘World’}
- a = {‘Hello’: ‘World’, 1: [2, 3, 4], (1,2): 1}
More examples:
2. ‘Return’ vs ‘Print’
“ ‘Print’ just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. ‘Return’ is how a function gives back a value. This value is often unseen by the human user, but it can be used by the computer in further functions.” (Kagle, 2014)
‘Print’ is a convenient way for code writers to check status, examine variable values, and overview data without interrupting the code in the background.
‘Return’ is for a function to return its value. With the value returned, we can then further apply it to different functions, store it as a variable, or print it out.
3. ‘.append’ vs ‘.extend’ vs ‘+=’
‘.append()’ adds an element to a list, and ‘.extend()’ pastes another list to the original list.
Ex:
One element:
A list:
len(a) = 6 (extend) while len(a) = 4(append)
(a += 1) means (a = a + 1)
Conclusion:
These are the 3 most confusing Python things I have encountered as a beginner. By looking definitions online and trying to create examples myself for each concept I am able to approach and understand new concepts thoroughly. I Hope every programming rookie like me can enjoy the process of digging deeper into Python knowledge and eventually have fun with it and become a coding expert!