Codecademy.com学习Python

编程由Python入门。

第一步Codecademy网站的Python学习课程。(共15小时,预计一周(6.22-6.27)完成,6.28做总结)

Codecademy界面很清爽,语言为英文,正好适合适应英文学习环境,作为入门是不错的选择。

不知道下面的MIT、Harvard的网络课程会怎么样(安排自《编程入门指南v1.4》)。

 

7月1日Coursera.org上的北京大学程序设计专项课程就要开始了。去完成这个系列。

 

---

Python学习笔记

1.Python Syntax(语法)

  Variables(变量)

  Calculator: + - * / % **

 

2.String and Console Output(字符串与控制台输出)

  print(打印)

  datetime

 

3.Conditionals & Control Flow(条件语句与控制流)

  Comparators(比较)

  Boolean Operators(布尔判断)(inlcude and,or,not)

  Conditional Statements(条件语句)(include if,elif,else)

 

  游戏PygLatin程序 (my first Python Program)

  代码:

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    print original
    word=original.lower()
    first=word[0]
    new_word=word+first+pyg
    new_word=new_word[1:len(new_word)]
    print new_word
else:
    print 'empty'

  所学到的新知识

  1.isalpha()函数 用于判断是否为纯字母字符串

  2.字符串中提取从几位到几位的字符 string[x:x] (x代表数字)

 

4.Functions

  Function Syntax

    Fuction Junction

    Parameters and Arguments(参数)

  Importing Modules

    Three Way: 1). import math

           2).from math import sin

             3).from math import *   #including ALL in math

  Build-in Functions

    .upper()  .lower()  len()  str()  max()  min()  type()  abs()

 

 

Taking a Vacation:)

 1 def hotel_cost(nights):
 2     return 140*nights
 3 
 4 def plane_ride_cost(city):
 5      if city=="Charlotte":
 6          return 183
 7      elif city=="Tampa":
 8          return 220
 9      elif city=="Pittsburgh":
10          return 222
11      elif city=="Los Angeles":
12          return 475
13          
14 def rental_car_cost(days):
15     cost=40*days
16     if days>=7:
17         return cost-50
18     elif days>=3:
19         return cost-20
20     else:
21         return cost
22         
23 def trip_cost(city,days,spending_money):
24     return rental_car_cost(days)+hotel_cost(days)+plane_ride_cost(city)+spending_money
25     
26 print trip_cost("Los Angeles",5,600)

 

5. Lists and Dictionaries

  Lists(列表,类似数组)

 

  Dictionaries(字典)

    including keys and things

    

 

  .sort() 排序函数

 

 

  A Day at the Supermarket

 1 shopping_list = ["banana", "orange", "apple"]
 2 
 3 stock = {
 4     "banana": 6,
 5     "apple": 0,
 6     "orange": 32,
 7     "pear": 15
 8 }
 9     
10 prices = {
11     "banana": 4,
12     "apple": 2,
13     "orange": 1.5,
14     "pear": 3
15 }
16 
17 # Write your code below!
18 def compute_bill(food):
19     total=0
20     for it in food:
21         if stock[it]>0:
22             total+=prices[it]
23             stock[it]-=1
24     return total
25     
26 print compute_bill(shopping_list)

 

  Practice:Students and Class Grade

 1 lloyd = {
 2     "name": "Lloyd",
 3     "homework": [90.0, 97.0, 75.0, 92.0],
 4     "quizzes": [88.0, 40.0, 94.0],
 5     "tests": [75.0, 90.0]
 6 }
 7 alice = {
 8     "name": "Alice",
 9     "homework": [100.0, 92.0, 98.0, 100.0],
10     "quizzes": [82.0, 83.0, 91.0],
11     "tests": [89.0, 97.0]
12 }
13 tyler = {
14     "name": "Tyler",
15     "homework": [0.0, 87.0, 75.0, 22.0],
16     "quizzes": [0.0, 75.0, 78.0],
17     "tests": [100.0, 100.0]
18 }
19 
20 # Add your function below!
21 def average(numbers):
22     total=sum(numbers)
23     total=float(total)
24     return total/len(numbers)
25     
26 def get_average(student):
27     homework=average(student["homework"])
28     quizzes=average(student["quizzes"])
29     tests=average(student["tests"])
30     return 0.1*homework+0.3*quizzes+0.6*tests
31     
32 def get_letter_grade(score):
33     if score>=90:
34         return "A"
35     elif score>=80:
36         return "B"
37     elif score>=70:
38         return "C"
39     elif score>=60:
40         return "D"
41     else:
42         return "F"
43         
44 def get_class_average(students):
45     results=[]
46     for student in students:
47         results.append(get_average(student))
48     return average(results)
49         
50 print get_letter_grade(get_average(lloyd))
51 print get_class_average([lloyd,alice,tyler])
52 print get_letter_grade(get_class_average([lloyd,alice,tyler]))

 

6. Lists and Functions

  

  用Python编写的第一个游戏:Battleship!

 1 from random import randint
 2 
 3 board = []
 4 
 5 for x in range(5):
 6     board.append(["O"] * 5)
 7 
 8 def print_board(board):
 9     for row in board:
10         print " ".join(row)
11 
12 print "Let's play Battleship!"
13 print_board(board)
14 
15 def random_row(board):
16     return randint(1, len(board))
17 
18 def random_col(board):
19     return randint(1, len(board[0]))
20 
21 ship_row = random_row(board)
22 ship_col = random_col(board)
23 
24 for turn in range(4):
25     guess_row = int(raw_input("Guess Row:"))
26     guess_col = int(raw_input("Guess Col:"))
27 
28     if guess_row == ship_row and guess_col == ship_col:
29         print "Congratulations! You sunk my battleship!"
30         board[ship_row-1][ship_col-1] = "R"
31         print_board(board)
32         break
33     else:
34         if (guess_row < 1 or guess_row > 5) or (guess_col < 1 or guess_col > 5):
35           print "Oops, that's not even in the ocean."
36         elif board[guess_row-1][guess_col-1] == "X":
37             print "You guessed that one already."
38         else:
39             print "You missed my battleship!"
40             board[guess_row-1][guess_col-1] = "X"
41     print "Turn",turn+1
42     print_board(board)
43     if turn==3:
44         print "---------"
45         print "The right Row:%s"%(ship_row)
46         print "The right Col:%s"%(ship_col)
47         board[ship_row-1][ship_col-1] = "R"
48         print_board(board)
49         print "Game Over"

 

7. Loops

  for  else

  while  else

 

  break

  enumerate(枚举)

 

 

  Practice Makes Perfect

    is_prime()(判断素数模型)(利用标记temp)

 1 def is_prime(x):
 2     temp=True
 3     if x<2:
 4         temp=False
 5     elif x==2:
 6         temp=True
 7     else:
 8         i=2
 9         while i<x:
10             if x%i==0:
11                 temp=False
12             i+=1
13     return temp

 

posted @ 2015-06-21 21:05  magegi  阅读(643)  评论(0编辑  收藏  举报