Posts Tagged ‘tuvio’

1:00
07-04-2008

Learning Python 1

   Posted by: clarezoe    in Python

I’m learning python these days. I’ve set up two projects in google code: dicthon , a Chinese-English and English-Chinese dictionary based on dict.cn. Huahua has writen a script for me. It works perfect, it is my default dictionary now. You can download it from the project site. I’m planning to add some more functions in it, such as “read the word”, “my new words”. The second is tuvio, a violin tuner, I need to read some guitar tuners.

I have the ideas, now I need to make them into practice. However, I’m just a beginner, and there are not so many exercises for beginners. So I picked up some exercises and algorythms in C , and rewrite them by python.

And I’ll recommend another editor Geany, it’s just wonderful, better than editra I introduced before. It can complie and run the script, I could say it’s a lightweight IDE. I’m so bad at vim, and eclipse is too powerful for me, geany just fits well.

Now, it is my first exercise:

Input 2 integers from the keyboard, and print out their sum.

#!/usr/bin/python
# Filename : sum.py
x = int(raw_input(”Please enter a integer for x : “))
y = int(raw_input(”Please enter a integer for y : “))
sum = x + y
print “The sum of x and y is “, sum
print “The sum of x and y is “, x+y #You can either declare a new variance or just sum them up.

Run the script:

Please enter a integer for x : 9
Please enter a integer for y : 78
The sum of x and y is  87
The sum of x and y is  87

What did I learn?

  1. Of course, how to run python: first line of the script; file extension; python filename
  2. How to get input from keyboard: raw_input( ” What ever you want to say ” )
  3. There’s no need to use comma, but if more than one command in one line, you need to use comma. I used to use it in Perl and C :)

Tags: , , , , , , , ,

收藏 & 分享

Powered by 17fav.com