python basics - cs.ucf.eduxiaoman/spring/learnpython/easypython1.pdfpython basics presented by jun...
TRANSCRIPT
Python Basics
Presented by Jun Ding
Aug, 2015
Why Python?
Easy 2 LearnCoding FastSimple Syntax…..
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.
Is there any books/tutorials very helpful?
(1) OnLine tutorial
http://www.learnpython.org/
(2) Book
Dive into Python
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
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”
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)
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
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)
Basic Syntax-List
*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.
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
Basic Syntax-String
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
Basic Syntax-flow control
There are 3 important types of flow-control
(1) If
(2) for
(3) While
Please see example code(controlFlow.py)
Basic Syntax-flow control
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
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
Basic Syntax- List comprehension
Basic Syntax-mathBasic math in python
+,-,*,/,**,sqrt(),and etc.
See the following example code of math
(math.py)
Basic Syntax-math
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)
Basic Syntax-Function
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.
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
Basic Syntax-Files
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()
Congrats!, you are done with
Python Basics
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).
Questions ?