【coursera 学习笔记】An Introduction to Interactive Programming in Python--week0
发布时间
阅读量:
阅读量
课程位置:
课程位置:
时间:2014.09
简介:coursera 的课,ps 在学德语中,Python德文指大蟒蛇,难怪他的图标……
在线实验平台:http://www.codeskulptor.org/viz/index.html(由 rice university 的老师自行搭建)
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 zeroAssistance
Comments — CodeSkulptor
- Textual descriptions within a program that detail its non-computational functionality.
- Code introduced using
#is disregarded by Python. - It's imperative to include ample comments to ensure clarity for readers.
- Lecture materials: CodeSkulptor
- Additional resources: Comments, Strings, and Print
- Code introduced using
Strings — CodeSkulptor
- 字符对(使用单个或双个引号)所包围的一系列字符
- 示例包括'cats hate dogs'和'Strings are fun!'
- 字符串是Python中的一种数据类型。其数据类型通常表示为
str。 - 讲座示例 - Hello World
- 更多示例 - 评论、字符串与打印
这是一个展示字符串、注释和Python代码片段的Markdown页面
1. # Comments, strings and print
2.
3. # Number sign(#)后面的语句都是注释.
4.
5. # A string is a collection of characters enclosed by quotation
6. # marks (引号)or apostrophes(撇号)
7.
8. "This is a string"
9. 'This is also a string'
10.
11. # To print these strings and other things to the box on the
12. # right, the word print is used.
13.
14. print "Output Number One"
15. print 'Output Number Two'
16.
17. # print statements on their own can be used to print blank(打印空白行)
18. # lines to the screen, which separates output and makes it
19. # easier to read.
20.
21. print
22. print "Hello"
23. print
24.
25. # Multiple strings can be printed on the same line by using
26. # commas to separate them.(多个输出时 用逗号隔开) This automatically inserts a
27. # space in between the two strings.(这样字符串之间会自动生成一个空格)
28.
29. print "One", "Two"
30. print "One", "Two", "Three"
31. print
32.
33. # If you want to include a quotation mark or apostrophe in
34. # your string, you need to make the symbols around the
35. # string be the opposite type.(如果有双重引用,要用不同的号)
36.
37. print "You're awesome!"
38. print '"Thank you!" I replied.'
39.
40. # Congratulations!!! You are off to a great start :)
Numbers — Arithmetic Expressions
- In Python, there are two primary types of numeric data: integers and decimal values.
- Integers belong to the
intdata type. Decimal values are represented as floating-point figures associated with thefloatdata type. - The precision of floating-point numbers is typically about 15 decimal digits.
- Within CodeSkulptor’s environment, every number, including integers, is stored as a floating-point value.
- Lecture examples can be found at Arithmetic Expressions
- Additional resources are available at Floats and Ints
- Integers belong to the
Arithmetic Operators — Arithmetic Expressions
- 五种基本算术运算符:加法(+)、减法(-)、乘法(*)、除法(/)和指数运算(**)。
- CodeSkulptor实现了Python 2的一部分内容。在Python 2中,除法操作符(/)若任一操作数为浮点数,则返回一个近似值;若两个操作数均为整数,则返回向下取整的精确结果。
- 整数除法操作符(//)用于计算两个数值的商。
- 讲座示例 - Arithmetic Expressions
- 更多示例 - Arithmetic Operations, Division
Arithmetic Expressions — Arithmetic Expressions
- 一个算术表达式要么是一个数字,要么是两个算术表达式上的一个运算符执行运算。
- 算术表达式的输入遵循数字和算术运算符的顺序排列。
- 表达式的形成通过优先级完成:根据优先级形成"Please excuse my dear Aunt Sallie";随后是括号、指数、乘法、除法、加法、减法。
- 讲座示例:Arithmetic Expressions
- 更多示例:Order of Operations for Arithmetic Expressions, [Possible Errors for Arithmetic Expressions](http://www.coders curriculum.org/#examples-more-1a_arithmetic_expressions-errors.py "Link: http://www.coders curriculum.org/#examples-more-1a_arithmetic_expressions-errors.py")
Variables — Variables
- 变量名由字母、数字以及下划线(
_)组成。 - 变量名必须以字母或下划线开头且区分大小写。
- 单个等号(
=)用于变量赋值操作;而双个等号(==)则用于判断等式成立情况。 - 讲解示例 - 变量
- 更多示例 - 变量命名, 赋值操作, 变量运算, 公式应用
# 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 *
# 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
# 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
print 2 + 12
# always manually group using parentheses when in doubt
print 1 * (2 + 3)
print 1 * 5
# 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
print "Ex. 6:", 2 * 3
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)
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."
全部评论 (0)
还没有任何评论哟~
