Python Learning - Three
1. Set
Set is a collection which is unordered and unindexed. No duplicate members
In Python sets are written with curly brackets { }
set1 = {'apple', 'banana', 'cherry'}
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
print(set1)
print(list_set, type(list_set))
The set( ) constructor
# Note the double round-brackets
set_constructor = set(('apple', 'cherry', 'mango'))
print(set_constructor, type(set_constructor))
(1) .intersection( )
set1 = {'apple', 'banana', 'cherry'}
'''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
'''
set2 = set(['mango', 'cherry', 'apple', 'orange'])
print(set1, set2)
print(set1.intersection(set2))
(2) .union( )
set1 = {'apple', 'banana', 'cherry'}
'''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
'''
set2 = set(['mango', 'cherry', 'apple', 'orange'])
print(set1, set2)
print(set1.intersection(set2))
print(set1.union(set2))
(3) .difference( )
A.difference(B) means those values that are in A and not in B
# Author: Alan FUNG
# A&F TECH HK Co,LTD. set1 = {'apple', 'banana', 'cherry'} '''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
''' set2 = set(['mango', 'cherry', 'apple', 'orange']) print(set1.difference(set2)) print(set2.difference(set1))
(4) .issubset( ) and .issuperset( )
set1 = {'apple', 'banana', 'cherry'}
'''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
'''
set2 = set(['mango', 'cherry', 'apple', 'orange'])
print(set1.issubset(set2))
print(set1.issuperset(set2))
set3 = set(['apple', 'cherry'])
print(set3.issubset(set1))
print(set1.issuperset(set3))
(5) .symmetric_difference( )
set1 = {'apple', 'banana', 'cherry'}
'''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
'''
set2 = set(['mango', 'cherry', 'apple', 'orange'])
print(set1.issubset(set2))
print(set1.issuperset(set2))
set3 = set(['apple', 'cherry'])
print(set3.issubset(set1))
print(set1.issuperset(set3))
print(set1.symmetric_difference(set3))
(6) .isdisjoint( )
# Return True if two sets have a null intersection
set1 = {'apple', 'banana', 'cherry'}
'''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
'''
set2 = set(['mango', 'cherry', 'apple', 'orange'])
set3 = set(['apple', 'cherry'])
set4 = set(('mango', 'pineapple','orange' ))
print(set3.isdisjoint(set4))
(7) & and .intersection( )
et1 = {'apple', 'banana', 'cherry'}
'''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
'''
set2 = set(['mango', 'cherry', 'apple', 'orange'])
set3 = set(['apple', 'cherry'])
set4 = set(('mango', 'pineapple','orange' ))
print(set1 & set2)
print(set1.intersection(set2))
print(set2.intersection(set1))
(8) | and .union( )
set1 = {'apple', 'banana', 'cherry'}
'''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
'''
set2 = set(['mango', 'cherry', 'apple', 'orange'])
set3 = set(['apple', 'cherry'])
set4 = set(('mango', 'pineapple','orange' ))
print(set1 | set2)
print(set1.union(set2))
print(set2.union(set1))
(9) - and .difference( )
set1 = {'apple', 'banana', 'cherry'}
'''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
'''
set2 = set(['mango', 'cherry', 'apple', 'orange'])
set3 = set(['apple', 'cherry'])
set4 = set(('mango', 'pineapple','orange' ))
print(set1 - set2)
print(set1.difference(set2))
print(set2-set1)
print(set2.difference(set1))
(10) ^ and .symmetric_difference( )
set1 = {'apple', 'banana', 'cherry'}
'''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
'''
set2 = set(['mango', 'cherry', 'apple', 'orange'])
set3 = set(['apple', 'cherry'])
set4 = set(('mango', 'pineapple','orange' ))
print(set1 ^ set2)
print(set1.symmetric_difference(set2))
print(set2 ^ set1)
(11) .add ( )
set1 = {'apple', 'banana', 'cherry'}
'''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
'''
set2 = set(['mango', 'cherry', 'apple', 'orange'])
set3 = set(['apple', 'cherry'])
set4 = set(('mango', 'pineapple','orange' ))
print(set3.add('mango'))
print(set3)
(12) .update( )
Add more than one item to the set
set1 = {'apple', 'banana', 'cherry'}
'''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
'''
set2 = set(['mango', 'cherry', 'apple', 'orange'])
set3 = set(['apple', 'cherry'])
set4 = set(('mango', 'pineapple','orange' ))
print(set3.add('mango'))
print(set3)
set3.update(['pineapple', 'banana', 'orange'])
print(set3)
(13) .remove( )
et1 = {'apple', 'banana', 'cherry'}
'''
list1 = [1, 2, 3, 4, 5]
list_set = set(list1)
'''
set2 = set(['mango', 'cherry', 'apple', 'orange'])
set3 = set(['apple', 'cherry'])
set4 = set(('mango', 'pineapple','orange' ))
set4.remove('orange')
print(set4)
2. Files
The key function for working with files in Python is the open() function.
The open() function takes two parameters; filename, and mode.
There are four different methods (modes) for opening a file:
"r" - Read - Default value. Opens a file for reading, error if the file does not exist.
"a" - Append - Opens a file for appending, creates the file if it does not exist.
"w" - Write - Opens a file for writing, creates the file if it does not exist.
"x"- Create - Creates the specified file, returns an error if the file exists.
In addition, you can specify if the file should be handled as binary or text mode
"t" - Text - Default value. Text mode
"b"- Binary - Binary mode (e.g. images)
(1) "w"
file_w = open('testing_file', 'w', encoding='utf-8')

file_w = open('testing_file', 'w', encoding='utf-8')
file_w.write('Hello World! \n')
print(file_w)

(2) "a"
'''
file_w = open('testing_file', 'w', encoding='utf-8') file_w.write('Hello World! \n') print(file_w)
''' file_a = open('testing_file', 'a', encoding='utf-8') file_a.write('Hi, this is Alan \n') print(file_a)

(3) "x"
file_x = open('testing_file', 'x', encoding='utf-8')

file_x = open('testing_file_for_create', 'x', encoding='utf-8')

file_x = open('testing_file_for_create_2', 'x', encoding='utf-8')
file_x.write('Hi, this is a new file')
print(file_x)


(4) .read( ) and .readline( )
1) .read( ) print all the content
file_read = open('testing_file', 'r', encoding='utf-8')
content = file_read.read()
print(content)


2) .readline( ) only return one line
file_read = open('testing_file', 'r', encoding='utf-8')
content = file_read.readline()
print(content)


3) Use the for loop
file_read = open('testing_file', 'r', encoding='utf-8')
for i in range(5):
print(file_read.readline())

(5) .readline( ) and .readlines( )
1) .readlines( )
file_read = open('testing_file', 'r', encoding='utf-8')
for line in file_read.readlines():
print(line)

2) .readlines( ) with enumerate( )
file_read = open('testing_file', 'r', encoding='utf-8')
for index, line in enumerate(file_read.readlines()):
if index == 3:
print('-----------------------This is a dashline ----------------------------')
continue
print(line.strip())

(6) .tell( )
file_read = open('testing_file', 'r', encoding='utf-8')
print(file_read.tell())
print(file_read.readline())
print(file_read.tell())
file_read.seek(0)
print(file_read.readline())

(7) .encoding( )
ile_read = open('testing_file', 'r', encoding='utf-8')
print(file_read.encoding)
(8) .flush( )
import sys, time
for i in range(20):
sys.stdout.write('#') sys.stdout.flush() time.sleep(0.5)
(9) read and write -- r+
ile_rwrite = open('testing_file', 'r+', encoding='utf-8')
print(file_rwrite.readline())
print(file_rwrite.readline())
print(file_rwrite.readline())
file_rwrite.write('--------------------- This is a dash line ----------------------')
print(file_rwrite.readline())


(10) Write and read -- w+
file_wread = open('testing_file', 'r+', encoding='utf-8')
file_wread.write('----------------------- This is a dash line -------------------------- \n')
file_wread.write('----------------------- This is a dash line -------------------------- \n')
file_wread.write('----------------------- This is a dash line -------------------------- \n')
file_wread.write('----------------------- This is a dash line -------------------------- \n')
print(file_wread.tell())
file_wread.seek(0)
print(file_wread.readline())

(11) file modification
file_mod = open('yesterday', 'r', encoding='utf-8')
file_new = open('yesterday_new.bak', 'w', encoding='utf-8')
for line in file_mod:
if 'Alan' in line:
line = line.replace('Alan','FUNG')
file_new.write(line)
else:
file_new.write(line)
file_mod.close()
file_new.close()



(12) with statement
with open('yesterday', 'r', encoding='utf-8') as file_read :
print(file_read.readline())

with open('yesterday', 'r', encoding='utf-8') as file_read :
for line in file_read:
print(line)

3. Python Functions
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.
(1) Function Basics

(2) Creating a Function
In Python a function is defined using the def keyword:


(3) Calling a Function
To call a function, use the function name followed by parenthesis:

(4) Parameters
Information can be passed to functions as parameter.
Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
The following example has a function with one parameter (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:


(5) Default Parameter Value
The following example shows how to use a default parameter value.
If we call the function without parameter, it uses the default value:
Default Value:

def my_function(country = "Norway"):
print("I am from " + country) my_function() my_function("Hong Kong")


(6) Multiple Arguments
Asterisk ['æstərɪsk] 星號, 星號鍵 Multiple Arguments

The asterisk * means multiple arguments. Receive positional parameters (位置參數), and convert into tuples. Positional parameters must be placed before the keyword parameters.
def test(*args):
print(args) test(1,2,3,4,5,6) test(*[1,2,3,4,5,6])

1) ** == > dictionay
Receive keyword parameters (關鍵字參數) and Convert into dictionary.
def test2(**kwargs):
print(kwargs) test2(name = "alan", age = 28, gender = 'male') test2(**{'name':'alan', 'age':28, 'gender': 'male'})

def test2(**kwargs):
print(kwargs)
print(kwargs['name']) test2(name = "alan", age = 28, gender = 'male') test2(**{'name':'alan', 'age':28, 'gender': 'male'})

def test3(name, **kwargs):
print(name)
print(kwargs)
test3('alan')
test3("alan", age = 28, gender = 'male')

def test4(name, age = 18, *args, **kwargs):
print(name)
print(age)
print(args)
print(kwargs)
test4('Alan', 27, gender = 'malre', jod = 'analyst')
def test4(name, age = 18, *args, **kwargs):
print(name)
print(age)
print(args)
print(kwargs)
test4('Alan', 27, 23,4,54, 76, gender = 'malre', jod = 'analyst')

def logger(source):
print('From {}'.format(source))
print("From %s" %(source)) def test4(name, age = 18, *args, **kwargs):
print(name)
print(age)
print(args)
print(kwargs)
logger('TEST-4') test4('Alan', 28, gender = 'male', job = 'analyst')

(7) Return Values (返回值)
To let a function to return a value, use the return statement:
def my_function(x):
return x * 5 print(my_function(3)) print(my_function(5)) print(my_function(9))
def my_function(x):
print(x * 5) my_function(3) print(my_function(3))
VS 
def func1():
print("This is a function!") def func2():
print('This is another function!')
return 0 def func3():
print('This is also a function!')
return 1, 'function', ['alan', 'fung'], {'Name': 'AlanFUNG'} func1()
print(func1())
func2()
print(func2())
func3()
print(func3())


(8) Scopes
1) Python Scope Basics
Besides packaging code for reuse, functions add an extra namespace layer to your programs to minimize the potential for collisions among variables of the same name—by default, all names assigned inside a function are associated with that function’s namespace, and no other. This rule means that:
- Names assigned inside a def can only be seen by the code within that def. You cannot even refer to such names from outside the function.
- Names assigned inside a def do not clash with variables outside the def, even if the same names are used elsewhere. A name X assigned outside a given def (i.e., in a different def or at the top level of a module file) is a completely different variable from a name X assigned inside that def.
Variables may be assigned in three different places, corresponding to three different scopes:
- If a variable is assigned inside a def, it is local to that function.
- If a variable is assigned in an enclosing def, it is nonlocal to nested functions.
- If a variable is assigned outside all defs, it is global to the entire file.
Scope Example
# global scope
X = 99 # X and func assigned in module: global def func(Y): #Y and Z assigned in function: locals
# local scope
Z = X + Y # X is a global
return Z print(func(1))

The Built-in Scope
import builtins
print(dir(builtins)) print(zip) print(zip is builtins.zip)



2) The global Statement
We’ve talked about global in passing already. Here’s a summary:
- Global names are variables assigned at the top level of the enclosing module file.
- Global names must be declared only if they are assigned within a function.
- Global names may be referenced within a function without being declared
X = 88
def func():
global X
X = 99 print(func()) print(X)

Program Design: Minimize Global Variables
X = 99
def func1():
global X
X = 88 def func2():
global X
X = 77 print(func1())
print(X) print(func2())
print(X)

(9) Recursion
Python also accepts function recursion, which means a defined function can call itself.
Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.
The developer should be very careful with recursion as it can be quite easy to slip into writing a function which never terminates, or one that uses excess amounts of memory or processor power. However, when written correctly recursion can be a very efficient and mathematically-elegant approach to programming.
In this example, tri_recursion() is a function that we have defined to call itself ("recurse"). We use the k variable as the data, which decrements (-1) every time we recurse. The recursion ends when the condition is not greater than 0 (i.e. when it is 0).
To a new developer it can take some time to work out how exactly this works, best way to find out is by testing and modifying it.
def tri_recursion(k):
if (k > 0):
result = k + tri_recursion(k-1)
print(result)
else:
result = 0
return result print("\n\n Recursion Example Results")
tri_recursion(6)
1) Recursive Functions
Summation with Recursion
def mysum(L):
if not L:
return 0
else:
return L[0] + mysum(L[1:]) # Call myself recursively print(mysum([1,2,3,4,5]))

def mysum(L):
print(L)
if not L:
return 0
else:
return L[0] + mysum(L[1:]) # Call myself recursively print(mysum([1,2,3,4,5]))

Coding Alternatives
def mysum(L):
return 0 if not L else L[0] + mysum(L[1:]) print(mysum([1])) print(mysum([1,2,3,4,5]))

def mysum(L):
if not L: return 0
return nonempty(L) def nonempty(L):
return L[0] + mysum(L[1:]) print(mysum([1,2,3,4,5]))

Loop Statements Versus Recursion
L = [1,2,3,4,5]
sum = 0
while L:
sum += L[0]
L = L[1:] print(sum)

L = [1,2,3,4,5]
sum = 0
for x in L: sum += x
# sum += x print(sum)

Handling Arbitrary Structures
def sumtree(L):
tot = 0
for x in L:
if not isinstance(x,list):
tot += x
else:
tot += sumtree(x)
return tot L = [1, [2, [3,4], 5], 6, [7, 8]] print(sumtree(L))

Recursion versus queues and stacks
def sumtree(L):
tot = 0
items = list(L)
while items:
front = items.pop(0)
if not isinstance(front, list):
tot += front
else:
items.extend(front)
return tot L = [1, [2, [3,4], 5], 6, [7, 8]] print(sumtree(L))
4 Function Objects: Attributes and Annotations
(1) Indirect Function Calls: “First Class” Objects
def echo(message):
print(message) echo("Direct Call")
def echo(message):
print(message) x = echo
x("Indirect Call")

def echo(message):
print(message) def indirect(func, arg):
func(arg) indirect(echo, 'Argument Calls!')

def echo(message):
print(message) schedule = [(echo, 'Spam!'), (echo, 'Ham!')] for (func, arg) in schedule:
func(arg)

def make(label):
def echo(message):
print(label + " :" + message)
print(label, ":", message)
return echo F = make("Spam") F("Ham!") F("Eggs!")

(2) Function Introspection
def func(a):
b = "spam"
return b * a print(func(8))

def func(a):
b = "spam"
return b * a print(func.__name__) print(dir(func)) print(func.__code__) print(dir(func.__code__)) print(func.__code__.co_varnames) print(func.__code__.co_argcount)

(3) Function Attributes
def func(a):
b = "spam" return b * a print(func)

def func(a):
b = "spam" return b * a
print(func) func.count = 0 func.count += 1 print(func.count) func.handles = "Button-press" print(func.handles) print(dir(func))

def f(): pass print(dir(f)) print(len(dir(f)))

(4) Function Annotations in 3.X
def func(a, b, c):
return a + b + c print(func(1, 2, 3))

def func(a:'spam', b:(1,10), c:float) ->int:
return a + b + c print(func(1, 2, 3))

5. Anonymous Functions: lambda
(1) lambda Basics
Besides the def statement, Python also provides an expression form that generates function objects. Because of its similarity to a tool in the Lisp language, it’s called lambda.
The lambda’s general form is the keyword lambda, followed by one or more arguments (exactly like the arguments list you enclose in parentheses in a def header), followed by an expression after a colon:
lambda argument1, argument2, ... argumentN : expression using arguments
- lambda is an expression, not a statement.
- lambda’s body is a single expression, not a block of statements.
def func(x, y, z):
return x + y + z print(func(2, 3, 4)) f = lambda x, y, z : x + y + z print(f(2,3,4))

x = lambda a = "fee", b = "fie", c = "foe": a + b + c
y = (lambda a = "fee", b = "fie", c = "foe": a + b + c) print(x("wee"))
print(y(b ="wee"))

def knights():
title = "sir"
action = (lambda x : title + ' ' + x)
return action act = knights()
msg = act('robin')
print(msg)

(2) Scopes: lambdas Can Be Nested Too
def action(x):
return (lambda y: x + y) act = action(99)
print(act) print(act(2))

action = (lambda x: (lambda y: x + y)) act = action(99) print(act(3)) print((lambda x: (lambda y: x + y))(99)(4))

6. Functional Programming Tools
(1) Mapping Functions over Iterables: map
counters = [1, 2, 3, 4] updated = [] for x in counters:
updated.append(x + 10) print(updated)

counters = [1, 2, 3, 4]
def inc(x): return x + 10 print(list(map(inc, counters)))

counters = [1, 2, 3, 4] print(list(map((lambda x: x + 3), counters)))

def inc(x): return x + 10 def mymap(func, seq):
res = []
for x in seq: res.append(func(x))
return res print(list(map(inc, [1, 2, 3])))
print(mymap(inc, [1, 2, 3]))

print(pow(3,4)) print(list(map(pow,[1,2,3], [2,3, 4])))

def inc(x): return x + 10 print(list(map(inc, [1, 2, 3, 4]))) print([inc(x) for x in [1, 2, 3, 4]]) print(list(inc(x) for x in [1, 2, 3, 4]))

(2) Selecting Items in Iterables: filter

print(list(range(-5, 5))) print(list(filter((lambda x: x > 0), range(-5, 5))))

res = [] for x in range(-5, 5):
if x > 0:
res.append(x) print(res) print([x for x in range(-5, 5) if x > 0])

(3) Combining Items in Iterables: reduce
from functools import reduce # import in 3.x, not in 2.x print(reduce((lambda x, y: x + y), [1,2,3,4])) print(reduce((lambda x, y: x * y), [2,3,4,5])) L = [1, 2, 3, 4]
res = L[0]
for x in L[1:]:
res = res + x
print(res)

Coding your own version of reduce is actually fairly straightforward. The following function emulates most of the built-in’s behavior and helps demystify its operation in general:
def myreduce (function, sequence):
tally = sequence[0] for next in sequence[1:]:
tally = function(tally, next) return tally print(myreduce((lambda x, y : x + y), [1,2,3,4,5])) print(myreduce((lambda x, y : x * y), [1,2,3,4,5]))

import operator, functools print(functools.reduce(operator.add,[2,4,6])) print(functools.reduce((lambda x, y: x + y), [2,4,6]))

Python Learning - Three的更多相关文章
- python learning Exception & Debug.py
''' 在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因.在操作系统提供的调用中,返回错误码非常常见.比如打开文件的函数open(),成功时返 ...
- Python Learning Paths
Python Learning Paths Python Expert Python in Action Syntax Python objects Scalar types Operators St ...
- Python Learning
这是自己之前整理的学习Python的资料,分享出来,希望能给别人一点帮助. Learning Plan Python是什么?- 对Python有基本的认识 版本区别 下载 安装 IDE 文件构造 Py ...
- How to begin Python learning?
如何开始Python语言学习? 1. 先了解它,Wiki百科:http://zh.wikipedia.org/zh-cn/Python 2. Python, Ruby等语言来自开源社区,社区的学法是V ...
- Experience of Python Learning Week 1
1.The founder of python is Guido van Rossum ,he created it on Christmas in 1989, smriti of ABC langu ...
- Python Learning: 03
An inch is worth a pound of gold, an inch of gold is hard to buy an inch of time. Slice When the sca ...
- Python Learning: 02
OK, let's continue. Conditional Judgments and Loop if if-else if-elif-else while for break continue ...
- Python Learning: 01
After a short period of new year days, I found life a little boring. So just do something funny--Py ...
- Python Learning - Two
1. Built-in Modules and Functions 1) Function def greeting(name): print("Hello,", name) g ...
随机推荐
- 【汇总目录】C#
[2019年04月29日] C# textbox 自动滚动 [2019年02月07日] C#利用VUDP.cs开发网络通讯应用程序 [2019年02月06日] C#利用VINI.cs操作INI文件 [ ...
- CDH5.16.1启动报错:Invalid value set for db.setupType, the valid values are EMBEDDED or EXTERNAL
1 自己的配置文件已经添加了配置,但是还是报错,由于是自建数据库 com.cloudera.cmf.db.setupType=EXTERNAL 2,索性注释掉这个参数,启动就ok了
- vue知多少,你对vue的认识比别人高在哪?
1.beforeCreated/created区别? beforeCreated钩子能干什么 2.data中使用props 3.get/set依赖收集 get收集依赖(观察者) set 观察者重新求值 ...
- 线程的start方法和run方法的区别
run方法及结果 public class MyThread extends Thread { @Override public void run() { System.out.println(&qu ...
- 【easy】225. Implement Stack using Queues
用队列实现栈.这个实现方法十分的简单,就是在push这一步的时候直接变成逆序. class MyStack { private: queue<int> q; queue<int> ...
- Python基础【第二篇】
一.Python的标准数据类型 Python 3中主要有以下6中数据类型: Number(数字).String(字符串).List(列表).Tuple(元组).Sets(集合).Dictionary( ...
- Windows 下安装Git工具及基础使用
Git简介 git是很好一个工具使用,可以执行liunx命令,有git环境后windows系统就可以进行shell命令操作,就可以添加其他liunx辅助软件进行执行,git也代码库管理工具,无论是上传 ...
- vue-i18n国际化在data中切换不起作用
vue-i18n是一个针对于vue的国际化插件,使用非常简单,具体使用方式看我细细道来. 实现方式 1. 下载包 npm install vue-i18n 2. 配置 在main.js文件中加入如下配 ...
- [原创]SVN使用
在企业中,SVN环境,由企业已经搭建好,并提供相关技术支持.对于个人,如果想在个人PC上实现版本管理,亦可以实现.安装Visual SVN及其相关工具 如何使用,可见下链接 http://www.cn ...
- Swagger-概述
前言 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一些常见问题.如果想深入分析项目源码,了解更多内容,见参考资料. S ...


