Learn Python the Hard Way - 1
I am coming to Python from an R background. Read about the similarities between the two languages on a few forums . R is good for mixed data , data visualization and stat models. In building a system that interacts with live data, its better to learn a language that can be used for operations like data cleaning, feed handling, etc. One of my friends recommended Python to me and told me that it is a very powerful language for data cleaning operations. I have programmed in C, C#, C++, Java , Ruby, R before but I have never programmed in Python. I firmly believe that it is never late to learn anything in life. My short term objective is to develop Python skills so that I have at least a working knowledge of the language.
\%d , \%s, \%r are used for substituting stuff in the string
\%() is the used to substitute the respective variable in the string
script, first, second, third = argv to read the arguments in to variables
Learnt about close , read , write , readline , truncate functionsfrom sys import argv and from ops.path import exists are used to import functions from respective modules.
raw_input is used to get input from the user
Functions appear similar to functions defined in ruby.
Functions should start with def
You need not put an open parenthesis right after the function name
You can leave spaces after the parenthesis (
You can leave spaces after the closing parenthesis ) and colon
You got back to usual code environment from a functional environment by writing with no indent. Unlike ruby there is no need to put an end at the end of every def
At the end of def statement , there is a need to put colon
You have to indent all the lines of code in a function with 4 spaces, no more , no less.
Duplicate argument names are not allowed
Variables in script are not connected to variables in the function
The variables in the function are not connected to variables in the script.
I keep forgetting the colon sign after the def keyword in the function
seek function in python is to go to a specific place in the file
f.seek(0) takes you back to start of the line
print linecount, f.readline() reads a specific line in the text return at the end of function can be be used to return something from a function.
Exercise 23 was really awesome as it asked me to go and visit bitbucket.org and then browse a random python project, click on source and write about whatever I could find interesting about the project.
This exercise says this “ When you do this exercise, think of yourself as an anthropologist, trucking through a new land with just barely enough of the local language to get around and survive." Despite
hardly knowing any aspects of Python, I looked up bitbucket.org and started randomly browsing a source program. I stumbled on bootstrap-py3k.py le from pyquery. This is the first professional python file that I am reading in my life. I have learnt the followingYou can import a ton of libraries by listing them down separated by comma
import X imports the module X, and creates a reference to that module in the current name space. Or in the other words, after you’ve run this statement, you can use X.name to refer to things defined
in module Xfrom X import * imports the module X, and creates references in the current name space to all the public objects defined by that module. X in itself is not defined. So X.name will not work but name
will work{ from X import a, b, c imports the module X and creates references in the current namespace to the given objects
try Except. I always thought try would be associated with catch.
ImportError - appears like a Java naming convention based class
if loop has no bracket and a terminating colon
There are commands like bool() , int() stdout.strip()
for x in list() - This is similar to what you find in R.
The above program is overwhelming. I will always remember this program and this date Feb 7, 2012 that I was totally stunned by the complex Python code. As I reflect on the path ahead, it is definitely
a very steep learning curve. Learning some basic things is alright but beyond that, if you have to be proficient at coding Python, you got to be really really good.