Notes for List, Tuple, Set and Dict in Python with Functions

Syntax

set: ([]), dict: {}, list:[],  tuple:()

Only List is mutable.

def calc(*nnnn):
	Total=0
	for n in nnnn:
		Total=Total+n*n
	return Total
r=calc(3,9)
print(r)

To insert python code into wordpress with original Tab Space, we have to go to HTML version to edit the code

def fib(max):
    n, a, b = 0, 0, 1
    while n < max:
        yield b
        a, b = b, a + b
        n = n + 1
    return 'done'
for n in fib(7):
	print(n)

The following is a quick example of higher order function(or compound function)

def fact(n):
    return fact_iter(n, 1)
def fact_iter(num, product):
    if num == 1:
        return product
    return fact_iter(num - 1, num * product)

The following is a quick example of list with format checking(or compound function)

L1 = ['Hello', 'World', 18, 'Apple', None]
for e in L1:
    if isinstance(e, str) == False:
        L1.remove(m)
print(L1)

The following is a quick example of dictionary with default argument, mutable argument, keyword argument and named keyword argument

def staff(name,age,*,add,city="Beijing",job):
	print(name,age,add,city,job)
staff("Charles",24,add="135th",job="Scientist")
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s