I have recently started learning Python programming and will be sharing my experiences (the highs & lows included!) with it. I am a R user and find it to be a wonderful open source programming package. Right now eager to see what Python has in store for me.
I found an interesting feature in Python. It starts counting from ZERO!
Okay, so far I have used R. For accessing say, the first element from a vector in R, I did something like this,
x <- c(‘R’, ‘is’, ‘open source’)
print(x[1])
The output that I get is,
[1] ”R”
Say, I want the third element of the same vector, then,
print(x[3])
And the output is,
[1] ”open source”
Well this was fine with me because, I learnt counting from 1!
But, Python has different rules for counting. As mentioned earlier, the counting begins at Zero. If I write a similar code in Python,
Whereas,
print(x[2])
gives me,
open source
which is the last element of the list.
Also, noticed that Python won’t print a stuff unless the stuff is an argument of the print() command.
Another thing!
There is a module/ library called as os in Python.
I imported this library in Python and used it along with the command listdir to get a list of all files in a specific directory in my device!
import os
a = [file for file in os.listdir(path)]
print(a)
Here, path is the path of the specific folder. Python displays the names of files included in it when the above code is executed. If I want only files ending with specific extensions, (say, .pdf), then the following helps!
import os
a = [file for file in os.listdir(path) if file.endswith(‘.pdf’)]
print(a)
That’s it for the day!