A Meteorologist’s Road to Python (Part 2)

Editor’s note: In this post, Associate Professor of Meteorology Alex DeCaria of Millersville University continues sharing about his experience in learning Python (see Part 1 for the back story), focusing on specific features of Python that he loves, as well as one caution about using Python 3 vs. Python 2.7. If you have comments, please feel free to email him at alex.decaria@millersville.edu.

As I said in Part 1, Python is modern and easy to learn. The syntax is clean, and makes sense! Here are some favorite things that I love about Python:

1) No begin/end statements! Blocks of code are delimited purely by indentation. This is pure genius in my book!

2) The new string formatting paradigm (used in later versions of Python). This is another very cool and easy to use feature which I make use of frequently.

3) The matplotlib plotting library. The plots and graphs are much nicer than anything I’ve ever seen in IDL. They are easy to create and very flexible. I have just scratched the surface of what matplotlib is capable, and am impressed with what I see.

4) The Tkinter GUI library. It works out of the box! No extra installation of Tcl/Tk required (as was needed with Ruby Tk). And, the GUI’s have a very native feel. (I have written two GUI programs so far: an air parcel thermodynamic calculator and a geostrophic wind calculator. If anyone would like to try them out I would be happy to send them to you.)

5) The Python Image Library (PIL). This is another great library that comes as part of the Enthought distribution, and works out of the box. In Ruby the similar library is the RMagick library which requires the separate installation of the ImageMagick software and the RMagick library, along with the headache of sorting out all the dependencies. I always dreaded setting this up on a new system. But with Python and PIL, it is load and go!

6) The extremely simple for-loop syntax.

7) The Enthought Python distribution. Everything works! Installation is painless!

I am a willing and happy Python convert. I plan on writing all future code in Python, except for heavy-duty number crunching in numerical models (thought I do want to write a shallow-water model in Python, just because …). My Ruby books will likely be donated to my local library in the near future, and I won’t shed a tear.

My one nagging concern about switching to Python is the whole Python 2 vs. Python 3 conundrum. I have been using Python 2.7, which is kind of a ‘bridge’ version between the older Python 2 and the new Python 3 (which is not backwards compatible with Python 2). There is the fear that at some point the momentum will switch to Python 3 and my code will no longer work. However, I didn’t want to jump right in to Python 3, since many of its libraries are not mature. Python 2.7 does incorporate many of the newer features from Python 3 and I’ve been taking pains to write my code using the newer syntax whenever possible. Two examples are:

1) Using the print function rather than the print statement: print(stuff) rather than print stuff.

2) Using the newer string formatting syntax:

x = 'World'
hello_string = 'Hello {0:s}'.format(x)

rather than

hello_string = 'Hello %s' %x

Being a newcomer to Python I can easily train myself to use the newer Python 3 style syntax. But, I can certainly feel for the veteran Python programmer who has thousands of line of legacy code that may not work with Python 3.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值