上课地址:https://class.coursera.org/interactivepython-005/wiki/coursestaff
时间:2014.09
简介:coursera 的课,ps 在学德语中,Python德文指大蟒蛇,难怪他的图标……
平台:http://www.codeskulptor.org/viz/index.html (rice uni 的老师自己搭建的)
Syllabus
Week | Topics | Mini-project |
---|---|---|
0 | Expressions, variables and assignments | "We want... a shrubbery!" |
1 | Functions, logic, conditionals | "Rock-Paper-Scissors-Lizard-Spock" game |
2 | Event-driven programming, local and global variables, buttons and input fields | "Guess the Number" game |
3 | The canvas, static drawing, timers, interactive drawing | Stopwatch: The Game |
4 | Lists, keyboard input, motion, positional/velocity control | "Pong" game |
5 | Mouse input, more lists, dictionaries, images | "Memory" game |
6 | Classes, tiled images | "Blackjack" game |
7 | Acceleration and friction, spaceship class, sprite class, sound | Spaceship from "RiceRocks" game |
8 | Sets, groups of sprites, collisions, sprite animation | Full "RiceRocks" game |
concepts:
Week zeroHelp
-
Comments — CodeSkulptor
-
- Non-computational parts of the program that textually describe the behavior of the program.
- Comments begin with
#
, everything to right of the hash is ignored by Python. - Comments should be frequent so you and others can understand the code.
- Lecture examples - CodeSkulptor
- More examples - Comments, Strings, and Print
Strings — CodeSkulptor
-
- Sequence of characters enclosed by a pair of single or double quotes
- Examples are
"cats hate dogs"
and'Strings are fun!'
. - Strings are one kind of data in Python. Their data type is denoted
str
. - Lecture examples - Hello World
- More examples - Comments, Strings, and Print
# Comments, strings and print # Number sign(#)后面的语句都是注释. # A string is a collection of characters enclosed by quotation # marks (引号)or apostrophes(撇号) "This is a string" 'This is also a string' # To print these strings and other things to the box on the # right, the word print is used. print "Output Number One" print 'Output Number Two' # print statements on their own can be used to print blank(打印空白行) # lines to the screen, which separates output and makes it # easier to read. print print "Hello" print # Multiple strings can be printed on the same line by using # commas to separate them.(多个输出时 用逗号隔开) This automatically inserts a # space in between the two strings.(这样字符串之间会自动生成一个空格) print "One", "Two" print "One", "Two", "Three" print # If you want to include a quotation mark or apostrophe in # your string, you need to make the symbols around the # string be the opposite type.(如果有双重引用,要用不同的号) print "You're awesome!" print '"Thank you!" I replied.' # Congratulations!!! You are off to a great start :)
Numbers — Arithmetic Expressions
-
- There are two kinds of numerical data in Python: integers and decimal numbers.
- Integers correspond to the data type
int
. Decimal numbers are represented by floating-point numbers corresponding to the data typefloat
. - Floating-point numbers have around 15 decimal digits of accuracy.
- In CodeSkulptor, all numbers (even integers) are represented internally as floating-point numbers.
- Lecture examples - Arithmetic Expressions
- More examples - Floats and Ints
Arithmetic Operators — Arithmetic Expressions
-
- Five basic arithmetic operators; addition (
+
), subtraction (-
), multiplication (*
), division (/
) and exponentiation (**
) - CodeSkulptor implements a subset of Python 2. In Python 2, the division operator (
/
) returns a float approximation to the exact answer if either of the operands is a float. If both operands are integers, division returns the exact answer round down to the nearest integer. - The integer division operator
//
returns the quotient of two numbers.. - Lecture examples - Arithmetic Expressions
- More examples - Arithmetic Operations, Division
Arithmetic Expressions — Arithmetic Expressions
- Five basic arithmetic operators; addition (
-
- An arithmetic expression is either a number or an operator applied to two arithmetic expressions.
- Arithmetic expressions are entered as a sequence of numbers and arithmetic operators.
- Expressions are formed by grouping operators and operands via precedence: "Please excuse my dear Aunt Sallie"; parentheses, exponentiation, multiplication, division, addition, subtraction.
- Lecture examples - Arithmetic Expressions
- More examples - Order of Operations for Arithmetic Expressions, Possible Errors for Arithmetic Expressions
Variables — Variables
-
- Variable names consist of a sequence of letters, number and underscores (
_
). - Variable names start with a letter or underscore and are case sensitive.#以字母或者下划线打头。区分大小写
- Single equals (
=
) is used for assignment to variables. Double equals (==
) is used for testing equality. - Lecture examples - Variables
- More examples - Variable Naming, Vabiable Assignment, Variable Operations, Formulas
- Variable names consist of a sequence of letters, number and underscores (
# Arithmetic expressions - numbers, operators, expressions
print 3, -1, 3.14159, -2.8
# numbers - two types, an integer or a decimal number
# two corresponding data types int() and float()
print type(3), type(3.14159)#查看函数类型
print type(3.0)
# we can convert between data types using int() and float()
# note that int() takes the "whole" part of a decimal number and doesn't round
# float() applied to integers is boring。int()截取浮点数的整数部分,不取舍
print int(3.14159), int(-2.8)
print float(3), float(-1)
# floating point number have around 15 decimal digits of accuracy
# pi is 3.1415926535897932384626433832795028841971...
# square root of two is 1.4142135623730950488016887242096980785696...
# approximation of pi, Python displays 12 decimal digits Python只显示12位
print 3.1415926535897932384626433832795028841971
# appoximation of square root of two, Python displays 12 decimal digits
print 1.4142135623730950488016887242096980785696
# arithmetic operators
# + plus addition
# - minus subtraction
# * times multiplication
# / divided by division
# ** power exponentiation
print 1 + 2, 3 - 4, 5 * 6, 2 ** 5
# Division in Python 2
# If one operand is a decimal (float), the answer is decimal
print 1.0 / 3, 5.0 / 2.0, -7 / 3.0
# If both operands are ints, the answer is an int (rounded down)
print 1 / 3, 5 / 2, -7 / 3
# expressions - number or a binary operator applied to two expressions
# minus is also a unary operator and can be applied to a single expression
print 1 + 2 * 3, 4.0 - 5.0 / 6.0, 7 * 8 + 9 * 10
# expressions are entered as sequence of numbers and operations
# how are the number and operators grouped to form expressions?
# operator precedence - "please excuse my dear aunt sallie" = (), **, *, /, +,-
print 1 * 2 + 3 * 4
print 2 + 12
# always manually group using parentheses when in doubt
print 1 * (2 + 3) * 4
print 1 * 5 * 4
# Addition
print "Ex. 1:", 4 + 5
print "Ex. 2:", 3 + 4 + 7
print
# Subtraction
print "Ex. 3:", 5 - 2
print "Ex. 4:", 3 - 7
print
# Multiplication
print "Ex. 5:", 6 * 4
print "Ex. 6:", 2 * 3 * 5
print
# Division
print "Ex. 7:", 10 / 2
print "Ex. 8:", 5 / 3
print "--------"
# Decimals, negative numbers, and fractions can also be used
# Decimals
print "Ex. 9:", 1.5 + 2.75
print "Ex. 10:", 2.0 * 1.75
print "Ex. 11:", 5.0 / 2.0 + .5
print
# Negative numbers
print "Ex. 12:", -4 + 8
print "Ex. 13:", 6 * -5
print "Ex. 14:", 4 - -3
print "Ex. 15:", -5.0 / -.75
print
# Fractions (include parenthesis)
print "Ex. 16:", (3.0 / 4.0) + (5.0 / 4.0)
print "Ex. 17:", 5 * (1.0 / 2.0)
print "Ex. 18:", -(1.0 / 2.0) * 3
print "Ex. 19:", (3.0 / 4.0) + .75
print
</pre><pre name="code" class="python">
#字符串连接:
print “sher”+“rry”
# 数字变成字符串
print "Joe Warren" + " is " + str(52) + " years old."