listings-ext

Similar documents
Python Speed Learning

Python Speed Learning

tuat1.dvi

(Basic Theory of Information Processing) Fortran Fortan Fortan Fortan 1

PowerPoint プレゼンテーション

Boo Boo 2 Boo 2.NET Framework SDK 2 Subversion 2 2 Java 4 NAnt 5 Boo 5 5 Boo if 11 for 11 range 12 break continue 13 pass

10 (1) s 10.2 rails c Rails 7 > item = PlanItem.new => #<PlanItem id nil, name nil,...> > item.name = "" => "" > item.valid? => true valid? true false

or a 3-1a (0 b ) : max: a b a > b result a result b ( ) result Python : def max(a, b): if a > b: result = a else: result = b ret

untitled

Ruby Ruby ruby Ruby G: Ruby>ruby Ks sample1.rb G: Ruby> irb (interactive Ruby) G: Ruby>irb -Ks irb(main):001:0> print( ) 44=>

解きながら学ぶJava入門編

haskell.gby

num2.dvi

: gettoken(1) module P = Printf exception End_of_system (* *) let _ISTREAM = ref stdin let ch = ref ( ) let read () = (let c =!ch in ch := inp

JEB Plugin 開発チュートリアル 第4回

main.dvi

Java演習(4) -- 変数と型 --

Java updated

r02.dvi

ohp02.dvi

: Shift-Return evaluate 2.3 Sage? Shift-Return abs 2 abs? 2: abs 3: fac

ohp1.dvi

K227 Java 2

8 if switch for while do while 2

Microsoft PowerPoint - C_Programming(3).pptx

PowerPoint Presentation

parser.y 3. node.rb 4. CD-ROM

永和システムマネジメント SP 安井力 東京支社 Python 勉強会実習資料 2005/12/28 この資料の使い方最初から順に試してください 初心者は 書かれたとおり入力して ほかにも似た例を試してみてください 初級者は 書かれたとおり入力して なぜそれで動くのか調べてください 中級者は 設問か

r07.dvi

ohp07.dvi


Ruby 50 Ruby UTF print, \n [Ruby-1] print("hello, Ruby.\n") [Ruby-2] Hello, Ruby. [Ruby-3] print("hello, \"Ruby\".\n") 2 [Ruby-4] seisuu = 10 pr

コンピュータ概論

Microsoft PowerPoint L07-Imperative Programming Languages-4-students ( )

6-1

Condition DAQ condition condition 2 3 XML key value

10/ / /30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20 6. http, CGI Perl 11/27 7. ( ) Perl 12/ 4 8. Windows Winsock 12/11 9. JAV

1 Ex Ex. 2 2

C言語によるアルゴリズムとデータ構造

導入基礎演習.ppt

Java (9) 1 Lesson Java System.out.println() 1 Java API 1 Java Java 1

ストラドプロシージャの呼び出し方

120802_MPI.ppt

3 Java 3.1 Hello World! Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World");

piyo0705B.rtfd

JavaScript 1.! DOM Ajax Shelley Powers,, JavaScript David Flanagan, JavaScript 2


新コンフィギュレータのフレームワークについて

橡j_Oracle_whitepaper.PDF

新版明解C言語 実践編


AtCoder Regular Contest 073 Editorial Kohei Morita(yosupo) A: Shiritori if python3 a, b, c = input().split() if a[len(a)-1] == b[0] and b[len(

# let rec sigma (f, n) = # if n = 0 then 0 else f n + sigma (f, n-1);; val sigma : (int -> int) * int -> int = <fun> sigma f n ( : * -> * ) sqsum cbsu

untitled

PYTHON 資料 電脳梁山泊烏賊塾 PYTHON 入門 ゲームプログラミング スプライトの衝突判定 スプライトの衝突判定 スプライトの衝突判定の例として インベーダーゲームのコードを 下記に示す PYTHON3 #coding: utf-8 import pygame from pygame.lo

r1.dvi


( ) ( ) lex LL(1) LL(1)

IDL_lecture_12Jan.ppt

COINS 5 2.1

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

Microsoft PowerPoint - Compiler06note.pptx

main

新・明解C言語 実践編

syspro-0405.ppt

Microsoft Word - SU1202教本(Disp)原稿.docx

2) TA Hercules CAA 5 [6], [7] CAA BOSS [8] 2. C II C. ( 1 ) C. ( 2 ). ( 3 ) 100. ( 4 ) () HTML NFS Hercules ( )

cards.gif from Tkinter import * root = Tk() c0 = Canvas(root, width = 400, height = 300) c0.pack() image_data = PhotoImage(file = c1.gif ) c0.create_i

PBASIC 2.5 PBASIC 2.5 $PBASIC directive PIN type New DEBUG control characters DEBUGIN Line continuation for comma-delimited lists IF THEN ELSE * SELEC

compiler-text.dvi

r03.dvi

つくって学ぶプログラミング言語 RubyによるScheme処理系の実装

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

CodeIgniter Con 2011, Tokyo Japan, February

新・明解Java入門

Java学習教材

ohp03.dvi

18 C ( ) hello world.c 1 #include <stdio.h> 2 3 main() 4 { 5 printf("hello World\n"); 6 } [ ] [ ] #include <stdio.h> % cc hello_world.c %./a.o

exec.dvi

明解Javaによるアルゴリズムとデータ構造

bdd.gby

# let st1 = {name = "Taro Yamada"; id = };; val st1 : student = {name="taro Yamada"; id=123456} { 1 = 1 ;...; n = n } # let string_of_student {n

2

(ver. 1.3 (2018) ) yacc 1 1 yacc yacc (Yet Another Compiler Compiler) UNIX yacc yacc y *.y yacc ) yacc *.tab.h *.tab.c C C yacc yacc UNIX yacc bison 2

Program Design (プログラム設計)

I117 II I117 PROGRAMMING PRACTICE II SOFTWARE DEVELOPMENT ENV. 1 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (


untitled

from Tkinter import * root = Tk() c0 = Canvas(root, width = 400, height = 300) c0.pack() image_data = PhotoImage(file = c1.gif ) c0.create_image(200,

Java Java Java Java Java 4 p * *** ***** *** * Unix p a,b,c,d 100,200,250,500 a*b = a*b+c = a*b+c*d = (a+b)*(c+d) = 225

やさしいJavaプログラミング -Great Ideas for Java Programming サンプルPDF

JavaScript の使い方

卒 業 研 究 報 告.PDF

test.gby

paper.pdf

OpenCV IS Report No Report Medical Information System Labratry

SCM (v0201) ( ) SCM 2 SCM 3 SCM SCM 2.1 SCM SCM SCM (1) MS-DOS (2) Microsoft(R) Windows 95 (C)Copyright Microsoft Corp

from tkinter import * root = Tk() # variable teban = IntVar() teban.set(1) # def start(): canvas.create_rectangle(0, 0, 560, 560, fill= white ) for k

Transcription:

(6) Python (2) ( ) ohsaki@kwansei.ac.jp 5 Python (2) 1 5.1 (statement)........................... 1 5.2 (scope)......................... 11 5.3 (subroutine).................... 14 5 Python (2) Python 5.1 (statement) Python Python Python 1

(statement) Python (simple statement) (compound statement) 2 (expression) (assignment statement) 1 >>> n = 123 2 >>> n += 1 3 >>> s = Hello, World! 4 >>> print ( s ) 5 Hello, World! 1 Python (free-form) C Java (whitespace) ( ) ( ) / Python ( ) ( ) 1 >>> n = 10 2 >>> while n > 0 : 1»> Python 2

3... i f n % 2 == 0 : 4... print ( n ) 5... n = 1 6... 7 10 8 8 9 6 10 4 11 2 1 >>> n = 10 2 >>> while n > 0 : 3... i f n % 2 == 0 : 4... print ( n ) 5... n = 1 6... 7 10 8 8 9 6 10 4 11 2 2 1 >>> n = 10 2 >>> while n > 0 : i f n % 2 == 0 : print ( n ) 3 F i l e "<stdin >", l i n e 1 4 while n > 0 : i f n % 2 == 0 : print ( n ) 5 ^ 6 SyntaxError : i n v a l i d syntax Python (1) C Java (;) (2) 3

( ) 1 >>> n = 1 2 3 ; m = 4 5 6 ; print ( n ) ; print (m) 2 123 3 456 (flow) Python (conditional brach) 2 (two-way branch) if if EXPR: STATEMENT : if EXPR: STATEMENT : else: STATEMENT : if EXPR: STATEMENT : elif EXPR: STATEMENT : 4

else: STATEMENT : EXPR STATEMENT (statement) 2 if 1 i f n == 0 : 2 print ( zero ) 3 4 i f n % 2 == 0 : 5 print ( even ) 6 else : 7 print ( odd ) 8 9 i f temperature < 1 8 : 10 print ( cold ) 11 e l i f temperature > 2 8 : 12 print ( hot ) 13 else : 14 print ( comfortable ) else if elsif elsif Python n 100 n n 100 1 i f n >= 1 0 0 : 2 print ( n ) 3 n = 100 2 if EXPR: STATEMENT if n = 0: print(n) if n = 0: print(n); n += 1 ( ) 5

n 100 n n (if ) n 100 1 i f n >= 1 0 0 : 2 print ( n ) 3 n = 100 Python (multiway branch) C switch Python Python if 1 >>> item = l e a f 2 >>> i f item == f i r e : 3... print ( red ) 4... e l i f item == l e a f : 5... print ( green ) 6... e l i f item == sea : 7... print ( blue ) 8... else : 9... print ( unknown ) 10... 11 green elif if/elif N ( ) N/2 O(N) elif 1 >>> item = l e a f 2 >>> c o l o r = { f i r e : red, l e a f : green, sea : blue } 3 >>> t r y : 4... print ( c o l o r [ item ] ) 5... except KeyError : 6

6... print ( unknown ) 7... 8 green ( ) O(1) (loop) for while for for TARGET_LIST in EXPR_LIST: STATEMENT : if STATEMENT TARGET_LIST ( ) EXPR_LIST ( ) 3 i 0 N - 1 1 >>> N = 5 2 >>> for i in range (N) : 3... print ( i ) 4... 5 0 6 1 7 2 8 3 9 4 range(n) 0 n - 1 [0, 1, 2,..., 9] i 4 3 Python for else ( break / ) 4 7

EXPR_LIST 5 1 >>> names = [ John, Bob, Michael ] 2 >>> f o r s in names : 3... print ( s ) 4... 5 John 6 Bob 7 Michael 1 >>> c o l o r = { f i r e : red, l e a f : green, sea : blue } 2 >>> for item in c o l o r : 3... print ( item ) 4... 5 f i r e 6 l e a f 7 sea 8 >>> for c in c o l o r. values ( ) : 9... print ( c ) 10... 11 red 12 green 13 blue Python TARGET_LIST EXPR_LIST 1 >>> c o l o r = { f i r e : red, l e a f : green, sea : blue } 2 >>> for k, v in c o l o r. items ( ) : 3... print ( k + i s + v ) 4... (iterator object) 5 (iterable) iter ( ) 8

5 f i r e i s red 6 l e a f i s green 7 sea i s blue 8 >>> for i, item in enumerate ( c o l o r ) : 9... print ( i, item ) 10... 11 0 f i r e 12 1 l e a f 13 2 sea 14 >>> points = [ ( 1 2, 34), ( 5 6, 78), ( 9 0, 10) ] 15 >>> for x, y in points : 16... print ( x, y ) 17... 18 12 34 19 56 78 20 90 10 for C Java continue break while while EXPR: STATEMENT : if EXPR STATEMENT 1 >>> while True : 2... t r y : 3... s = input ( $ ) # 4... except EOFError : 5... break 6... print ( s ) 9

7... 8 $ Hello 9 Hello 10 $ Welcome! 11 Welcome! 12 $ >>> # Ctrl D 6 break 1 >>> while s = input ( ) : 2 F i l e "<stdin >", l i n e 1 3 while s = input ( ) : 4 ^ 5 SyntaxError : i n v a l i d syntax C Java if while ( ) Python if while (1) = == (2) Python C Java Python Python 4 Python 6 input ( ) EOF EOFError 10

Beautiful is better than ugly. Explicit is better than implicit. Readability counts. In the face of ambiguity, refuse the temptation to guess. Python Python ( ) 5.2 (scope) Python (valid) (scope) Python (local variable) ( ) (= ) ( ) (= ) Python (block) C Java if while Python ( ) Python C Java if n 456 if n 123 11

1 >>> n = 123 2 >>> print ( outer n =, n ) 3 outer n = 123 4 >>> i f True : 5... n = 456 6... print ( inner n=, n ) 7... 8 inner n= 456 9 >>> print ( outer n =, n ) 10 outer n = 456 n 1 ( ) n f(x) y (= 123) y (= 456) 1 >>> def f ( x ) : 2... y = 123 3... return x 4... 5 >>> y = 456 6 >>> x = f ( 1 ) 7 >>> x 8 1 9 >>> y 10 456 f(x) y (= 456) 1 >>> def f ( x ) : 2... return y 3... 12

4 >>> y = 456 5 >>> x = f ( 1 ) 6 >>> x 7 456 8 >>> y 9 456 Python 1 >>> def f ( x ) : 2... y += 1 3... return y 4... 5 >>> y = 456 6 >>> x = f ( 1 ) 7 Traceback ( most r e c e n t c a l l l a s t ) : 8 F i l e "<stdin >", l i n e 1, in <module> 9 F i l e "<stdin >", l i n e 2, in f 10 UnboundLocalError : l o c a l v a r i a b l e y referenced before assignment global 1 >>> def f ( x ) : 2... global y 3... y += 1 4... return y 5... 6 >>> y = 456 7 >>> x = f ( 1 ) 8 >>> x 9 457 10 >>> y 11 457 13

C Java Python 1 >>> def f ( x ) : 2... i f x >= 0 : 3... s = non negative 4... else : 5... s = negative 6... print ( s ) 7... 8 >>> f ( 1 2 3 ) 9 non negative 10 >>> f ( 456) 11 negative type f f ( f ) if s s if (3 5 ) f 1 >>> def f ( x ) : 2... s = None 3... i f x >= 0 : 4... s = non negative 5... else : 6... s = negative 7... print ( s ) 8... ( ) 5.3 (subroutine) (procedure) (function) 14

(subroutine) (subprogram) (module) ( ) Python (function) 1 >>> def say_hello ( ) : 2... print ( Hello, World! ) 3... 4 >>> say_hello ( ) 5 Hello, World! 1 2 say_hello 4 say_hello def ( ) (parameter) Python 4 1 >>> def is_even ( n ) : 2... i f n % 2 == 0 : 3... return True 4... else : 5... return False 6... 7 >>> is_even ( 1 2 3 ) 8 False 9 >>> is_even ( 4 5 6 ) 10 True is_even True ( ) False Python 0 (parameter) 15

(call by value) (call by reference) (call by name) C 1 1 void inc ( i n t n ) { 2 n += 1 ; 3 } 4 5 i n t main ( void ) { 6 i n t n = 1 2 3 ; 7 inc (&n ) ; 8 return ( n ) ; 9 } Python 7 Python 1 >>> def inc ( n ) : 2... return n + 1 3... 4 >>> n = 123 5 >>> n = inc ( n ) 6 >>> n 7 124 Python ( ) ( ) 8 n m 7 C ( int n ) C 8 ( ) Python ( ) ( ) None 16

1 >>> def i s _ m u l t i p l e ( n, m=2, debug=false ) : 2... i f debug : 3... print ( i s _ m u l t i p l e : n = { }, m= { }, debug = { }. format ( n, m, debug ) ) 4... i f n % m == 0 : 5... return True 6... else : 7... return False 8... 9 >>> i s _ m u l t i p l e ( 1 2, 2, F a lse ) 10 True 11 >>> i s _ m u l t i p l e ( 1 3, 2, F a l s e ) 12 False 13 >>> i s _ m u l t i p l e ( 1 2, 2, True ) 14 i s _ m u l t i p l e : n=12, m=2, debug=true 15 True 16 >>> i s _ m u l t i p l e ( 1 2 ) 17 True 18 >>> i s _ m u l t i p l e ( 1 3 ) 19 False 20 >>> i s _ m u l t i p l e ( 1 3, debug=true ) 21 i s _ m u l t i p l e : n=13, m=2, debug=true 22 False 23 >>> i s _ m u l t i p l e ( 1 3, 3) 24 False 25 >>> i s _ m u l t i p l e ( 1 5, 3) 26 True m 2 debug False m debug is_multi(12, 3, True) is_multi(12, m=3, debug=true) = Python Python 17

is_multiple 1 >>> i s _ m u l t i p l e ( 1 2, 3, True ) 1 >>> i s _ m u l t i p l e ( 1 2, True, 3) 1 >>> i s _ m u l t i p l e ( 1 2, True ) Python ( ) Python Python return return (return value) return None return Python 1 >>> def geometry ( ) : 2... l a t i t u d e = 35 3... longitude = 135 4... return l a t i t u d e, longitude 5... 6 >>> l a t, long = geometry ( ) 7 >>> l a t 8 35 9 >>> long 10 135 C Python ( ) 18

1 >>> def f ( ) : 2... def g ( ) : 3... return 123 4... return g ( ) + 256 5... 6 >>> f ( ) 7 379 8 >>> g ( ) 9 Traceback ( most r e c e n t c a l l l a s t ) : 10 F i l e "<stdin >", l i n e 1, in <module> 11 NameError : name g i s not defined f g f f g 19