python basics - cs.ucf.eduxiaoman/spring/learnpython/easypython1.pdfpython basics presented by jun...

31
Python Basics Presented by Jun Ding Aug, 2015

Upload: others

Post on 14-Mar-2020

74 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Python Basics

Presented by Jun Ding

Aug, 2015

Page 2: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Why Python?

Easy 2 LearnCoding FastSimple Syntax…..

Page 3: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Install it? Simple!(1) For Linux Users (e.g. Ubuntu)

The python is installed by default for most Linux OS.

If not, you can easily install it by using the following command:

sudo apt-get install python (Debian/Ubuntu)

sudo yum install python (ReaHat/CentOS/Fedora)

(2) Windows and Mac OS Users

Just download the python from it’s website

http://www.python.org/download/releases/2.7.6/

Double click to install it. That’s it.

Page 4: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Is there any books/tutorials very helpful?

(1) OnLine tutorial

http://www.learnpython.org/

(2) Book

Dive into Python

Page 5: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Where to start?Let’s try a few simple examples to show how simple

python is.

(1) Print “Hello World!”

(2) Calculate 10+26,26.2/15

Page 6: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-DataType

Too boring to learn the grammar?

No! you don’t have to do that. Let’s just see a few

examples. You will find there is no “learning” grammar

at all.

Data type:

int e.g. 7

float e.g. 7.1

string “I am a string”

Page 7: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic container:

list (*) A=[1,2,3,4]

Tuple (a special case of list, immutable list)

dictionary D={‘A’:0,’C’:1,’G’:2,’T’:3}

(Example1.py)

Page 8: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed
Page 9: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

From the above example, we have seen :

(1) int : lengthA=6

(2) float: Element in A is float A[0]=4.0

(3) Boolean: AGB –True of False

(4) List

Page 10: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-ListList is almost the most important DataType in Python.

Almost, for all kinds of data, you can store them in List.

A=[4.0,6.0,7.0,9.7,1.2]

There are many important List functions, which you

need to know. We will study them in the following

example. (UseList.py)

Page 11: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-List

Page 12: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

*Things to remember*:

• A[x], get element in list A at position x

• A.append(x), append x to A list

• A.insert(p,x), insert x to A at position p

• A. index(x), get the first position of x in A, error it not found

• A.count(x), get the occurrence of x in A

• A.sort(reverse=True/False), sort list A. True/False set the

sorting order.

• A[x:y] , get the subset of list A from position x to position y.

Page 13: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-StringThere are lots of operations to string. We will discuss

about some important string operations.

We will also learn them from an example

Page 14: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-String

Page 15: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-String

*Things to remember*:

• A[x], get the element at position x of A

• len(A), get the length of string A

• A.upper(), get all uppercase of string A

• A.lower(), get all lowercase of string A

• A.split(), split string A with specific separator.

• .join(Asplit), join the list of strings with the separator

-> string

• A[x:y] the substring of A from x to y

Page 16: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-flow control

There are 3 important types of flow-control

(1) If

(2) for

(3) While

Please see example code(controlFlow.py)

Page 17: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-flow control

Page 18: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-flow control

*Things need to remember*:

(1) if-else

if (condition A):

statement A

else:

statement B

(1) For

for i in A:

statement A

(1) While

while(condition A):

statement B

Page 19: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax- List comprehension

This is a useful technique regarding to python list

A=[1,2,3,4,5]

If we want to get the sublets of A, in which all

elements >3. How did we do this

See example code listcomprehension.py

Page 20: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax- List comprehension

Page 21: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-mathBasic math in python

+,-,*,/,**,sqrt(),and etc.

See the following example code of math

(math.py)

Page 22: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-math

Page 23: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-Function

Python function is very important, it can help you to

organize your code well and improve the efficiency.

Please see the code example

(UseFunction.py)

Page 24: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-Function

Page 25: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-Function

*Things to remember*:

Please try to use function as possible as you can.

It can improve the re-usability of your code.

You don’t need to code the same function twice if it

already implemented.

Page 26: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-Files

Handling files is also important.

Unlike in java/c++, handling files in python is easy.

(1) read in files

(2) write files

Please see the example code in HandleFile.py

Page 27: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-Files

Page 28: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Basic Syntax-Files

*Things to remember*:

(1) Input

f=open(“filename”,’r’)

lf=f.readlines()

f.close()

(2) Output

f=open(“filename”,’w’)

f.write(String)

f.close()

Page 29: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Congrats!, you are done with

Python Basics

Page 30: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Do your homework:

(1) Try toy example codes from

http://www.learnpython.org/

(2) Get familiar with the functions mentioned in this

slides

(3) In the next class, we will study on solving real

problems by using python. ( You might be asked to

write some simple code, so please be prepared).

Page 31: Python Basics - cs.ucf.eduxiaoman/spring/learnPython/EasyPython1.pdfPython Basics Presented by Jun Ding ... Install it? Simple! (1) For Linux Users (e.g. Ubuntu) The python is installed

Questions ?