- I learnt some built-in functions which I have not used thus far. - sum, min, max, len
- How to use import to tap on certain functions within Python libraries
- Attributes of different data types. - append, extend, pop, join for lists; split for strings
What I learnt:
1. More type of built-in functions
- sum( ), min( ), max( ), len( ) functions
practice_list=[3, 4, 5, 6, 7] sum([3, 4, 5, 6, 7]) min(practice_list) max(practice_list) print(len(practice_list))
practice_string='this is a string' print(len(practice_string))
Here, I learnt how to use some more basic built-in functions.
- sum( ) adds all the values within the list.
- min( ) of practice_list takes the lowest numbered element within the list
- max( ) of practice_list takes the highest numbered element within the list
- print(len(practice_list)) first uses len( ) to take the length of the practice_list, in this case which is 5 (the number of elements in list), and print( ) then prints the length, 5
- print(len(practice_string)) first uses len( ) to take the length of the practice_string, in this case which is 16 (the number of characters in string), and print( ) then prints the length, 16
2. Import
There are many libraries that we can import from Python to use the functions within. Some examples are math, datetime, etc.
- We can import function from library, e.g.import pi from math
- We can import library e.g. import math
- We can assign them to a variable, using as
import math as m from math import sqrt as s s(m.pi)
- we imported the math library and assigned it a variable m
- from math we have the function square root and assigned it a variable s
- the last line, s(m.pi), gives the square root of pi, which we imported the value of pi from the library called math
3. Attributes
For different data types, there are different attributes.
- For lists, I learnt how to use append, extend, pop attributes
practice_list=[3, 4, 5, 6, 7] practice_list.append(10) practice_list.append(11) practice_list.append([12, 13, 14]) practice_list.extend([12, 13, 14]) practice_list.pop() print(practice_list)
- .append( ) attribute helps to insert the value(s) indicated within .append( ) at the back of a list.
- The 1st to 4th lines of code appends values and a list to practice_list, & in the end, the list of [3, 4, 5, 6, 7, 10, 11, [12, 13, 14]] is assigned to the variable practice_list
- The 5th line of code uses .extend( ) to add the values within the list (without [ ]) into the practice_list. Now, the list of [3, 4, 5, 6, 7, 10, 11, [12, 13, 14], 12, 13, 14] is assigned to the variable practice_list
- 6th line: .pop( ) removes last object from the list. The value 14 is removed. [3, 4, 5, 6, 7, 10, 11, [12, 13, 14], 12, 13] is now assigned to the variable practice_list
- Last line: print( ) prints the practice_list which is now [3, 4, 5, 6, 7, 10, 11, [12, 13, 14], 12, 13]
- For lists (of strings), I also learnt how to use ’ ‘.join( )
string_list=['this', 'is', 'a', 'string'] ' '.join(string_list)
- the join attribute joins multiple strings in a list into one longer string. Insert the delimiter within ‘ ‘.join, in this case it is a space, and join the strings (with the delimiter in between each of them) within the long string.
- For strings, I learnt how to use .split(‘ ‘) attributes
practice_string='this is a string' print(practice_string.split('i'))
- the split attribute splits up the string into a list of multiple smaller strings by the delimiter indicated within .split(‘ ‘), and removes the delimiter, in this case it is i
- better to assign a variable to it e.g. texts = sentence.split(‘ ‘)
- print( ) gives us output of [‘th’ ‘s ‘ ‘s a str’ ‘ng’]
Thoughts
The basic built-in functions seem quite straightforward, fortunately.
I probably would need to brush up on knowing more Python libraries as there are so many libraries to tap on.
I think it’s cool that lists and strings have so many attributes and I would definitely practise more to be familiar with all the different types of attributes!