4275浏览
查看: 4275|回复: 3

【Python入门系列1】arithmetic_expressions、variables & function

[复制链接]
本帖最后由 iooops 于 2016-3-13 23:07 编辑


上课笔记!


至于楼主为啥要这么费心还要去学Python?
因为 Python非常适合为树莓派编程。

要在树莓派里面做点啥好玩的交互(引脚的编程一般用Python写)吧?用Python!
要为树莓派做个啥小游戏吧?用Python!
当然用其他语言,如Java也是可以的!

【工具篇】
介绍一个对于初学者而言非常实用的网站:
http://www.codeskulptor.org
【Python入门系列1】arithmetic_expressions、variables & function图1
有文件保存(云)、下载、打开、撤销、运行【左】,以及docs(Python的说明教程)、Demos、Viz Mode(这个很神奇,可以亲自试试)【右】。
这个网站是这节课上用到的工具之一。

当然你也可以在shell(比如终端)上运行Python,或者写.py文档并用终端命令执行。


【笔记篇】
下面是第一二周的课程内容(编程语言大都长得差不多)。

http://www.codeskulptor.org/#examples-arithmetic_expressions.py
# 后面的是注释
  • 数据类型、强制类型转换、取近似值、int/float运算、运算优先级
  1. # Arithmetic expressions - numbers, operators, expressions
  2. print 3, -1, 3.14159, -2.8
  3. # numbers - two types, an integer or a decimal number
  4. # two corresponding data types int() and float()
  5. print type(3), type(3.14159)
  6. print type(3.0)
  7. # we can convert between data types using int() and float()
  8. # note that int() takes the "whole" part of a decimal number and doesn't round
  9. # float() applied to integers is boring
  10. print int(3.14159), int(-2.8)
  11. print float(3), float(-1)
  12. # floating point number have around 15 decimal digits of accuracy
  13. # pi is 3.1415926535897932384626433832795028841971...
  14. # square root of two is 1.4142135623730950488016887242096980785696...
  15. # approximation of pi, Python displays 12 decimal digits
  16. print 3.1415926535897932384626433832795028841971
  17. # appoximation of square root of two, Python displays 12 decimal digits
  18. print 1.4142135623730950488016887242096980785696
  19. # arithmetic operators
  20. # +                plus                addition
  21. # -                minus                subtraction
  22. # *                times                multiplication
  23. # /                divided by         division
  24. # **    power                exponentiation
  25. print 1 + 2, 3 - 4, 5 * 6, 2 ** 5
  26. # Division in Python 2
  27. # If one operand is a decimal (float), the answer is decimal
  28. print 1.0 / 3, 5.0 / 2.0, -7 / 3.0
  29. # If both operands are ints, the answer is an int (rounded down)
  30. print 1 / 3, 5 / 2, -7 / 3
  31. # expressions - number or a binary operator applied to two expressions
  32. # minus is also a unary operator and can be applied to a single expression
  33. print 1 + 2 * 3, 4.0 - 5.0 / 6.0, 7 * 8 + 9 * 10
  34. # expressions are entered as sequence of numbers and operations
  35. # how are the number and operators grouped to form expressions?
  36. # operator precedence - "please excuse my dear aunt sallie" = (), **, *, /, +,-
  37. print 1 * 2 + 3 * 4
  38. print 2 + 12
  39. # always manually group using parentheses when in doubt
  40. print 1 * (2 + 3) * 4
  41. print 1 * 5 * 4
复制代码

variables:
  • 注意命名规范


function:

  • 注意缩进(function的body部分)
  • 有head和body(做computation)两部分
  • function可嵌套使用(在一个function的body里使用另一个function)
  • 有return值的function返回变量,可赋值
  • 无return值的function返回none
  1. # compute the area of the triangle
  2. def triangle_area(base, height):                   //function - head
  3.         area = (1.0 / 2) * base * height             //body starts
  4.         return area                                          //body ends
  5. a1 = triangle_area(3, 8)
  6. print a1
  7. a2 = triangle_area(14, 2)
  8. print a2
  9. # convert fahrenheit to celsius
  10. def ftoc(fah):
  11.      cel = (5.0 / 9) * (fah -32)
  12.         return cel
  13. # test
  14. c1 = ftoc(32)
  15. c2 = ftoc(212)
  16. print c1, c2
  17. # convert fahrenheit to keivin
  18. def ftok(fah):
  19.     c = ftoc(fah)
  20.     kelvin = c + 273.15
  21.     return kelvin
  22. # test
  23. k1 = ftok(32)
  24. k2 = ftok(212)
  25. print k1, k2
  26. # prints hellp, world!
  27. def hello():
  28.         print "Hello, world!"
  29.    
  30. # test
  31. hello()
  32. h = hello()
  33. print h
复制代码

一个非常实用的算法(求余(%)的妙用!):
有些懒得敲代码了 - - 直接截课程图

如果我们要实现时钟的进位要怎么做呢?
如果用12小时制,8:00 p.m. 到4:00 a.m. 要进位直接用Python写会很麻烦有木有!不然你可以试试?
如果用24小时制,20:00 到 4:00要进位直接用Python写……还是很麻烦有木有……
【Python入门系列1】arithmetic_expressions、variables & function图2

但是!如果我们用了求余的时钟算法,问题就会变得异常简单……
  1. num = 49                 
  2. tens = num // 10                 #  //是求商数
  3. ones = num % 10                #  %试求余数
  4. #不知道商数和余数是啥的请翻阅小学一年级课本
复制代码
求余的时钟算法是这样的:
  1. hour = 20
  2. shift = 8
  3. print (hour + shift) % 24        #这就是我们要的4了
复制代码
哈哈哈哈哈哈哈!!有木有觉得这个算法很神奇!!!

其实这个算法用途还蛮广的。
有的时候我们写游戏嘛 = = 我们有个scrren width(比如是1280)还有height的嘛。有个object的width position要超界了(跑到3800去了)咋办?
用这个算法:
  1. width = 1280
  2. position = 2
  3. move = 5
  4. position = (position + move) % width
  5. print position
复制代码
当然还有很多其他应用,不一一列举。


下面的例子是讲将number转换为string, 合并输出,就不会两个number之间有空格了。
【Python入门系列1】arithmetic_expressions、variables & function图3

下面的例子是讲bool类型(True/False)之间的逻辑运算(and/or/not):
【Python入门系列1】arithmetic_expressions、variables & function图4

下面是讲comparison operator(+ - * / ==  !=)的bool运算……再结合逻辑运算:
【Python入门系列1】arithmetic_expressions、variables & function图5




一些细节性的使用要查阅文档:
http://www.codeskulptor.org/docs.html#tabs-Python
这是非常适合于这门课的查阅文档。


以上。两天刷完……还是觉得进度有点慢……
Screen Shot 2016-03-12 at 1.42.22 PM.png
Screen Shot 2016-03-12 at 2.04.27 PM.png
Screen Shot 2016-03-13 at 3.30.43 PM.png
Screen Shot 2016-03-13 at 4.06.57 PM.png

丄帝De咗臂  高级技匠

发表于 2016-3-14 17:49:30

哇咔咔
回复

使用道具 举报

iooops  中级技匠
 楼主|

发表于 2016-3-14 18:58:03

回复

使用道具 举报

yoyojacky  初级技匠 来自手机

发表于 2016-3-24 08:16:01

哇咔咔猴塞雷
image.jpg
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail