'''
ryEasyGuiEx001.py
除了 turle.py 之外,
easygui.py
也值得初學者留意。
呂仁園, 2014/07/24
'''
#
# 準備寫出 easygui_tc.py
#
from easygui import *
def 鈕盒( 鈕群= ('鈕01','鈕02','鈕03'),
訊息= '鈕盒訊息',
標題= '鈕盒標題',
影像= None,
根= None):
'''
傳入: 顯示的訊息、標題、以及一組鈕作為選項。
傳出:鈕上的文字串
'''
f= buttonbox(msg= 訊息,
title= 標題,
choices=鈕群,
image= 影像,
root= 根)
return f
def 輸入盒( 預設= '預設',
訊息= '輸入盒訊息',
標題= '輸入盒標題',
脫掉= True,
影像= None,
根= None):
'''
傳入:
傳出:輸入的文字串
'''
#
# easygui 原作者的 函式引數縮排方式滿有趣的。
#
f= enterbox(msg= 訊息
, title= 標題
, default= 預設
, strip= 脫掉
, image= 影像
, root= 根)
return f
#
# 以上是 easygui 函式庫的轉寫,
# 應該要分出去另成一檔案 easygui_tc.py。
#
# 以後就一行程式碼引入即可。
#
# from easygui_tc import *
#
#-----------------------------------------
# 以下才是 本程式
#-----------------------------------------
from random import random
from turtle_tc import *
def 龜畫圖():
形狀(龜形)
顏色(紅)
蓋印()
寫('你好!')
for i in range(100):
前進(100*random())
左轉(100)
速度(i)
顏色(藍)
寫('再見!')
主迴圈()
def 算數學(式子):
try: 結果= str(eval(式子))
except: 結果= 'Sorry, 我不會算!'
msgbox(式子+' = '+ 結果)
if __name__=='__main__':
你按的鈕= 鈕盒(['龜畫圖','算數學', '結束'])
while 你按的鈕 != '結束':
if 你按的鈕 == '龜畫圖':
龜畫圖()
elif 你按的鈕 == '算數學':
計算式= 輸入盒('1+2-3*4/5')
算數學(計算式)
else:
pass
你按的鈕= 鈕盒(['龜畫圖','算數學','結束'])
Chapter 1
Chapter 1
Python, IDLE and your first program
In this chapter you are going to:
- learn about computer programming and the different languages that you can use
- meet the Python programming language
- learn how to use IDLE, which will help organise your programs and allow you to run them easily
- check that your computer has been set up correctly
- write and run your first program.
Coding
- Coding is writing instructions for a computer to perform a task.
- This code has to be in a form that the computer can understand.
- This is more formally known as computer programming.
- Computers and coding have not been around for a long time but they have sure packed in some interesting history in a short space of time.
- The first machine that stored instructions in a way that future computers could take advantage of was the Jacquard loom that used holes punched in cards and was invented in 1801.
- Charles Babbage is often credited with inventing the first computer which he described in 1837 but was not built until 100 years later.
- In 1989 Guido van Rossum started to create the Python programming language which he named after Monty Python’s Flying Circus, a BBC comedy sketch show.
Programming languages
- There are many programming languages currently used by coders around the world.
- Some are best in one situation, others in another.
- HTML is good for producing web pages.
- SQL is great at making databases do what you want.
- Python is brilliant for writing quick applications, running programming experiments and for building larger applications, including games.
Python
- Python is a typed computer language.
- This makes writing short programs very fast and you can produce almost anything you can imagine.
- Python is a powerful, modern programming language used by many famous organisations
- such as YouTube and NASA.
- It is one of three programming languages that can be used to write Google Apps.
- Python is a great language.
- Enjoy!
IDLE
- You will start programming in IDLE which comes with Python.
- IDLE is a special text editor like Microsoft Word,
- except it understands Python and helps you get your code right.
- IDLE is itself, a Python application.
Hello World!
- Since the dawn of programming, when the first cave-coders booted up their cave-computers, it has been a tradition that your first program when learning a new language is ‘Hello World’.
- The aim is to try to make the computer say ‘hello’ to the world.
- If you can do this you will have tested whether everything that was set up for you is working properly.
Code Box 1.1
print("Hello World!")
Making mistakes
- Syntax errors are very common when typing in code (as are other errors).
- If you make one or two it is not your fault.
- It is because although computers are fast, they can also be a bit stupid.
- If there are any tiny mistakes in your code, they panic and produce error messages.
- These messages try to explain to you what the problem is but they are often difficult to understand.
- Colons, brackets, speech marks, apostrophes and spelling of Python words have to be just right.
- Although we can read imperfect sentences, computers cannot.
Chapter summary
ryTest001
觀自在菩薩, 行深般若波羅蜜多時, 照見五蘊皆空, 度一切苦厄。 舍利子, 色即是空, 空即是色, 色不異空, 空不異色, 受想行識, 亦復如是。
from tkinter import *
def 按鈕後的動作():
global 入, 文
x= 入.get()
文.insert(END, x)
窗= Tk()
布= Canvas(窗, background= 'red')
鈕= Button(窗, background= 'green', text='鈕', command= 按鈕後的動作)
入= Entry(窗, background= 'blue')
文= Text(窗, background= 'yellow')
元件群= [布, 鈕, 入, 文]
r=c=n=0
M= int(len(元件群)**.5)
for 元件 in 元件群:
r= n//M
c= n%M
n += 1
元件.grid(row= r, column= c)
布.create_line(0,0,100,100)
布.create_oval(100,100,200,200)
布.create_rectangle(200,200,300,300)
窗.mainloop()
Chapter 4 Functions, 第四章 函數。
Chapter 4 Functions, 第四章 函數。
In this chapter you are going to:
There are many functions that are built in to Python that we can already use. We can also make our own. We create functions with the def keyword. Here is the code for a counting function.
In interactive mode, type in the above code. You will need to press return twice to get back to the Python prompt (>>>). Then type count(10) and press return.
In this chapter you are going to:
- learn about functions
- write your own functions
- create a number guessing game.
Functions
You have already met and used a few functions. The first one you used was print(). Functions have brackets after their name. This is where we supply arguments separated by commas. Some functions do not need them, they do their jobs without argument!There are many functions that are built in to Python that we can already use. We can also make our own. We create functions with the def keyword. Here is the code for a counting function.
|
|
印= print |
number= 10
print("This is my number: ", number)
|
數= 10
印("這是我的數: ", 數)
|
def count_to(a_number):
n=1
while n <= a_number:
print(n)
n = n+1
|
def 算到(一個數):
n= 1
while n <= 一個數:
印(n)
n= n+1
|
count_to(10) |
算到(10) |
In interactive mode, type in the above code. You will need to press return twice to get back to the Python prompt (>>>). Then type count(10) and press return.
Writing Python-3 program in Chinese. 用中文寫 Python-3 程式。
有沒有試過用 中文 寫程式, Python-3 除了 約 30個 關鍵字不可用中文之外, 其他部分允許程式員使用中文(任何使用 Unicode 編碼的語言皆可)。 中文 是我最流利(fluent)的語言, 推薦你也來試試看。 會有意想不到的收穫喔!
from turtle import *
sayHello= 'Hello, World!'
print(sayHello)
for i in range(100):
forward(100)
left(170)
penup()
goto(0,+100)
pendown()
write(sayHello)
mainloop()
|
from turtle_tc import *
問候語= '你好,全世界!'
印(問候語)
for i in 範圍(100):
前進(100)
左轉(170)
提筆()
前往(0,+100)
下筆()
寫(問候語)
主迴圈()
|
| 執行本程式需要 turtle_tc 模組, You can get turtle_tc.py here https://github.com/renyuanL/pythonTurtleInChinese |
Introducing turtle
Chapter 1
Introducing turtle
In this book, you will use that hard-won knowledge to have some fun making some little applications while re-enforcing your knowledge and learning a few more tricks.
Python 3 comes with some great, ready-built modules some of which we have already used such as tkinter and random.
Another module we can use is turtle. This is an implementation of the turtle graphics part of a complete programming language called Logo which was created for educational use; schools often used it to drive a toy turtle around classrooms.
The commands available in Python’s turtle module are very easy to learn.
The fantastic thing about this Python module is that there is nothing new to install and we can combine the turtle commands with the Python language we have already learned.
In this chapter, you will learn how to:
The original Logo programming language was developed
Introducing turtle
In this book, you will use that hard-won knowledge to have some fun making some little applications while re-enforcing your knowledge and learning a few more tricks.
Python 3 comes with some great, ready-built modules some of which we have already used such as tkinter and random.
Another module we can use is turtle. This is an implementation of the turtle graphics part of a complete programming language called Logo which was created for educational use; schools often used it to drive a toy turtle around classrooms.
The commands available in Python’s turtle module are very easy to learn.
The fantastic thing about this Python module is that there is nothing new to install and we can combine the turtle commands with the Python language we have already learned.
In this chapter, you will learn how to:
- import the turtle module
- make your turtle move around in all directions
- change what the turtle looks like.
The original Logo programming language was developed
by Daniel G. Bobrow, Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967.
用 中文 (及英文) 教小孩 學 Python 程式語言
Teaching Kids Python Programming
in Chinese (and also in English)
用 中文 (及英文) 教小孩
學 Python 程式語言
Part I: Learning to Program
Chapter 1: Not All Snakes Slither
Chapter 2: Calculations and Variables
Chapter 3: Strings, Lists, Tuples, and Maps
Chapter 4: Drawing with Turtles
Chapter 5: Asking Questions with if and else
Chapter 6: Going Loopy
Chapter 7: Recycling Your Code with Functions and Modules
Chapter 8: How to Use Classes and Objects
Chapter 9: Python’s Built-in Functions
Chapter 10: Useful Python Modules
Chapter 11: More Turtle Graphics
Chapter 12: Using tkinter for Better Graphics
Part II: Bounce!
Chapter 13: Beginning Your First Game: Bounce!
Chapter 14: Finishing Your First Game: Bounce!
Part III : Mr. Stick Man Races for the Exit
Chapter 15: Creating Graphics for the Mr. Stick Man Game
Chapter 16: Developing the Mr. Stick Man Game
Chapter 17: Creating Mr. Stick Man
Chapter 18: Completing the Mr. Stick Man Game
機率與統計101
機率與統計
$ f(x, \mu, \sigma) = \frac{1}{\sigma\sqrt{2\pi}} e^{ -\frac{(x-\mu)^2}{2\sigma^2} } $
再論假設檢定:
已知 全校身高標準差 $\sigma$ = 10 cm,
呂教授根據十幾年的經驗,
假設 全校身高平均為 $\mu$ = 160,
令此假設為 $H_0$,
(1)
王同學以自己的身高 x = 170 (cm) 為由,懷疑 $H_0$ 的 真實性,提出 對立假設 $H_1$: $\mu \gt 160$,
王同學採用的「假設檢定」將基於 決策錯誤率(型1) $\alpha$ 的單邊檢定。
其決策法則如下:
- 若 $\overline{x} \gt \theta_0$,則 拒絕 $H_0$ ; 否則,接受 $H_0$
請根據 $\alpha$ = 20%, 10%, 5%, 2.5%, 1%, .5% 分別求出 對應的 決策臨界值 $\theta_0$ = ?
訂閱:
意見 (Atom)