My early experiences with Python programming

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,

x = [‘R’, ‘is’, ‘open source’]
print(x[1])

This is what I get,

is
Right, I get the second element of the list. So if I want the first element, I should pass this command to Python,
print(x[0])
To which the output is
R

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!

One thought on “My early experiences with Python programming”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s