introduction to pythonintroduction to python sparcs `08 서우석 (pipoket) `09 summer sp arcs...

41
Introduction to Python SPARCS `08 서서서 (pipoket) `09 Summer SPARCS Seminar

Upload: madeline-morrison

Post on 12-Jan-2016

229 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

Introduction to Python

SPARCS `08 서우석 (pipoket)

`09 Summer SPARCS Seminar

Page 2: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Python?

Father Guido Van

Rossum

Birth-year 1989

Born at Amsterdam

Page 3: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Python?

Platform independent

Interpreter Language

Script Language

Supports OOPObject Oriented Program-

ming

Page 4: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Why Python

Easy Syntax Easy Grammar Easy Data Structure Many Packages

Very Readable Very Flexible Usually Short

Code

Easy to Manage CodeEasy to Work Together

Page 5: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Python!

SPARCS Works Together with…

Page 6: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Get Python!

http://www.python.org

DOWNLOAD

Recommended Version 2.6.X <=

On Windows

Page 7: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Get Python!

On Debian Linux

~# apt-get install python

~$ sudo apt-get install python

Page 8: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Run Python!

~$ python

Page 9: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Run Python!

~# apt-get install ipython ~# ipython

Page 10: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Python Syntax

Use python as simple calculator

>>> 1 + 2>>> 6 * 8>>> 6.0 * 8>>> 2 ^ 10>>> 2 ** 10>>> _ / 2>>> __ / 4>>> _

Page 11: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Python Syntax

Print the “Hello world!”

>>> print “Hello World!”

>>> print(“Hello World!”)

Page 12: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Python SyntaxMultiline Text

Single Quote

INDENTATION!!

Page 13: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Python Syntax

Indentation is also Syntax!!!

Page 14: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Python Syntax

You MUST keep the same indenta-

tion!

Page 15: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Python Variables

Type Example

Number (integer, long, float …) 3, 3.141592, 31415928808L

String, Unicode “English”, u”한글” , u”Español”

Tuple (‘First’, ‘Second’, ‘Third’)

List [‘First’, ‘Second’, ‘Third’]

Dictionary {‘First’: 1, ‘Second’: 2, ‘Third’: 3}

But you don’t have to care!

Page 16: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Python Variables

Dynamic Typing

Page 17: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Do It Yourself #1

Your program should do the following1. Calculate the 224

2. Save the result to “result”3. Print the “result”

Page 18: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Python Controls

if

for

while

try, except

Page 19: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ if

“a is three”

Page 20: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ for

Page 21: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ while

Page 22: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ try, except

Page 23: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Do It Yourself #2

Your program should do the following1. Print out the even numbers between 1 to 1000, without line breaks2. Print out the 1000 / n When n is between -100 to 100

Page 24: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Number

7

0

7.0

0.75

5+5j

Page 25: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ String

Page 26: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ String

Page 27: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Do It Yourself #3

Your program should do the following1. Let given=“we are so friend!”2. Using slicing, concatenating, in-dexing, and methods, make following result

- “we are friend!”- “are WE so friend”- “so friend we are”

Page 28: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ List, Tuple

Page 29: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ List, Tuple

Page 30: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Do It Yourself #4

Your program should do the following1. Let given=range(100)2. Using the given, get the following

- Even number between 0~99- Odd number between 0~99- Multiplier of 3 between 0~50,

70~99- List starts from 99 and ends at 1

Page 31: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Dictionary

Page 32: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Dictionary

Page 33: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Dictionary

Page 34: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Simple input

Page 35: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Do It Yourself #5

Your program should do the following1. Get the input Name and Age2. Save the Name and Age to dictio-nary3. If input is empty Change the input mode to search4. On search mode Print the age of given name

Page 36: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ File Operation

Page 37: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Summary

Now you know…How to Install and Run PythonHow to Use Python as CalculatorBasic Python Syntax

(indentation, quote, multiline text…)

Python Data Structure (string, number, list, dictionary, tuple…)

Basic Input, Output to the terminal(input_raw, input, print)

Basic File Operation(open, write, readline, close …)

Page 38: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Summary

You have just learned 50% of python!

Page 39: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Next

FunctionClass with OOP

(Inheritance, Overloading, Overriding …)

PackageAdvanced Python Topics

Simple Format StringList GeneratingGenerator (yield)

Lambda Function (lambda)

Page 40: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ Homework

• You should review all the materials we have discussed

• You can get the codes used in this ppthttp://pipoket.kaist.ac.kr/sp_seminar/week01.tar.gz

• Following command will expand the file on Linux ~# tar –zxvf week01.tar.gz

Page 41: Introduction to PythonIntroduction to Python SPARCS `08 서우석 (pipoket) `09 Summer SP ARCS Seminar`09 Summer SP ARCS Seminar

▶ HomeworkYour program should do the following

1. Open the given file diy6.db2. File is like this

“20080421[TAB]Woosuk Suh[TAB]Computer Science”

“20080719TAB]Chanhee Lee[TAB]Undecided”

3. Read the file and save the data as you wish4. Get the Student ID as input and print out the information of student