5. Sequences: Strings, Lists, and Files Computer Concepts and Practice 2015 Spring Practical Exercises 2015/03/25 Intelligent Data Systems Lab. Seoul National University String An important data type in python Supports many useful string operations What we have learnt so far +, *, [], [:], len, for … in …, split capitalize find isalpha join split replace upper lower count strip isdigit index https://docs.python.org/2/library/string.html IDS Lab. 컴퓨터의 개념 및 실습 String Operators (Examples) Lower Capitalize Replace IDS Lab. 컴퓨터의 개념 및 실습 isalpha strip Join IDS Lab. 컴퓨터의 개념 및 실습 Practice 1: Palindrome • Examples - Do geese see God? - Was it Eliot's toilet I saw? - Are we not drawn onward, we few, drawn onward to new era? - Dennis and Edna sinned. - Dennis, Nell, Edna, Eva, Dave, and Ellen sinned. IDS Lab. H e l l o 0 1 2 3 4 5 컴퓨터의 개념 및 실습 B e H 6 7 8 Practice 1: Palindrome • Examples - Do geese see God? - Was it Eliot's toilet I saw? - Are we not drawn onward, we few, drawn onward to new era? - Dennis and Edna sinned. - Dennis, Nell, Edna, Eva, Dave, and Ellen sinned. IDS Lab. H e l l o 0 1 2 3 4 5 컴퓨터의 개념 및 실습 B e H 6 7 8 Practice 1: Palindrome • Examples - Do geese see God? - Was it Eliot's toilet I saw? - Are we not drawn onward, we few, drawn onward to new era? - Dennis and Edna sinned. - Dennis, Nell, Edna, Eva, Dave, and Ellen sinned. IDS Lab. H e l l o 0 1 2 3 4 5 컴퓨터의 개념 및 실습 B e H 6 7 8 Practice 1: Palindrome • Examples - Do geese see God? - Was it Eliot's toilet I saw? - Are we not drawn onward, we few, drawn onward to new era? - Dennis and Edna sinned. - Dennis, Nell, Edna, Eva, Dave, and Ellen sinned. IDS Lab. H e l l o 0 1 2 3 4 5 컴퓨터의 개념 및 실습 l e H 6 7 8 Practice 1: Palindrome • Examples - Do geese see God? - Was it Eliot's toilet I saw? - Are we not drawn onward, we few, drawn onward to new era? - Dennis and Edna sinned. - Dennis, Nell, Edna, Eva, Dave, and Ellen sinned. IDS Lab. H e l l o 0 1 2 3 4 5 컴퓨터의 개념 및 실습 l e H 6 7 8 Practice 1: Palindrome • Examples - Do geese see God? - Was it Eliot's toilet I saw? - Are we not drawn onward, we few, drawn onward to new era? - Dennis and Edna sinned. - Dennis, Nell, Edna, Eva, Dave, and Ellen sinned. IDS Lab. H e l l o 0 1 2 3 4 5 컴퓨터의 개념 및 실습 l e H 6 7 8 ASCII Table IDS Lab. 컴퓨터의 개념 및 실습 String Formatting Formatting Non-strings <String> + str(<value>) “<string>{0}<string>”.format(<value>) “<string>%d/f/s<string>” % (<value>) Formatting Rules {<index>:<width>.<precision><type>} <type> can be b, c, d, e, E, f, F, g, G, n, o, s, x, X, % <type> meaning Example e Exponent form 2.34e+06 f Fixed point 3.15 g General Both n General with locale Both IDS Lab. 컴퓨터의 개념 및 실습 String Formatting <type> meaning Example b binary 1101 c character '媿' d decimal 9832 o octal 350 x hex e8 X HEX E8 s string Hello World % percentage 3.2% You should at least remember f, d, x, s IDS Lab. 컴퓨터의 개념 및 실습 String Formatting (Examples) Printing hh:mm:ss Printing hex IDS Lab. 컴퓨터의 개념 및 실습 File I/O Files must be closed after opened <var> = open(“<filename>”, “<mode>”) close(<var>) You can omit close using ‘with’ statement with open(“<filename>”, “<mode>”) as <var> <statements> …. File Modes depend on Operating System For windows, “b” mode exists “rb” = reading in binary mode “wb” = writing in binary mode The difference exists because of how line endings (\n, \r\n) work. IDS Lab. 컴퓨터의 개념 및 실습 File Modes Mode Meaning Pointer Overwrite? r Reading only At the beginning No r+ Reading and Writing At the beginning No w Writing only At the beginning Yes w+ Reading and Writing At the beginning Yes a Appending At the end No a+ Reading and Appending At the end No rb rb+ wb wb+ Binaries. ab ab+ IDS Lab. 컴퓨터의 개념 및 실습 Reading Lines For <var> in <file>.readlines(): For <var> in <file>: IDS Lab. 컴퓨터의 개념 및 실습 Practice 2 Plotting Earthquakes IDS Lab. 컴퓨터의 개념 및 실습
© Copyright 2024