0人加入学习
(0人评价)
Python录播课
价格 $99,999美元
该课程属于 商业/数据分析师培养计划(2021年5月) 请加入后再学习

Python 4:

一. Dictionary (defined by "{}")

tuple is defined by "()"

list is defined by "[]"

Dictionaries are Python's most powerful data collection.

Dictionaries allow us to do fast database-like operations in Python.

Dictionaries have different names in different languages.

Associative Arrays - Perl / PHP

Properties or Map or HashMap - Java

Property Bag - C# / .Net

1. Lists index their entries based on the position in the list. Dictionaries are like bags - no order, so we index the things we put in the dictionary with a "lookup tag". (在dictionary中,element可以任意被define values, values可以是各种data type,如integer, string, list或者dictionary)如:purse = dict ()

purse ['money'] = 12(类似于list中的append)

purse ['candy'] = 3

purse ['tissues'] = 75

print (purse)   {'money': 12, 'tissues': 75, 'candy': 3}

print (purse ['candy'])       3

purse ['candy'] = purse ['candy'] + 2

print (purse)  {'money':12, 'tissues':75, 'candy':5}

2. Comparing Lists and Dictionaries

Dictionaries are like lists except that they use keys instead of numbers to look up values. 如一下对比:

1st = list()                  ddd = dict()

1st. append(21)           ddd['age'] = 21

1st. append(183)         ddd['course'] = 182

print(1st)   [21.183]     print(ddd){'course':182,'age':21}

1st [0] = 23                ddd['age'] = 23

print(1st)  [23,183]     print(ddd){'c':182,'a':23}

3. Many Counters with a Dictionary

One common use of dictionary is counting how often we "see" something.

4. Dictionary Tracebacks

It is an error to referrence a key which is not in the dictionary. We can use the in operator to see if a key is in the dictionary. 如:

ccc = dict()

print (ccc ['csev'])        报错

print ('csev' in ccc)       False

5. When we see a new name

When we encounter a new name, we need to add a new entry in the dictionary and if this the second or later time we have seen the name, we simply add one to the count in the dictionary under that name. 如:

counts = dict ()

names = ['csev', 'cwen', 'csev', 'zqian', 'cwen']

for name in names :

    if name not in counts :

        counts [name] = 1

    else :

        counts [name] = counts [name] + 1

print (counts).    显示 {'csev':2, 'zqian':1, 'cwen':2}

6. Definite Loops and Dictionaries

Even though dictionaries are not stored in order, we can write a for loop that goes through all the entries in a dictionary - actually it goes through all of the keys in the dictionary and looks up the values. 如:

counts = {'chuck' : 1, 'fred' : 42, 'jan' : 100}

for key in counts :

    print (key, counts [key])

显示 jan 100       chuck 1      fred 42

7. Retrieving lists of Keys and Values

You can get a list of keys, values, or items (both) from a dictionary. 如:

jjj = {'chuck' : 1, 'fred' : 42, 'jan' : 100}

print (list (jjj))      ['jan', 'chuck', 'fred']

print (jjj. keys())   ['jan', 'chuck', 'fred']

print (jjj, values())   [100, 1, 42]

print (jjj, items())  [('jan', 100), ('chuck', 1), ('fred', 42)] (以tuple的方式return)

8. sorted function

sorted (tuple , key = lambda string : string [1], reverse = True) (其含义如果忘了见google colab)

二. Function & class

1. Stored (and reused) Steps

具体展示如下:def (define)

def thing() : (下面为define item的逻辑)

    print ('Hello')

    print ('Fun')

以后每次执行thing时,会自动print出Hello和Fun,define后的代码会被储存起来供以后多次使用

We call these reusable pieces of code "functions".

2. Python Functions

There are two kinds of functions in Python.

(1) Built-in functions that are provided as part of Python, such as raw_input (), type(), float(), int().......

(2) Functions that we define ourselves and then use.

We treat the built-in function names as "new" reserved words, i.e., we avoid them as variable names.

3. Function Definition

In Python a function is some reusable code that takes arguments(s) as input, does some computation, and then returns a result or results.

We define a function using the def reserved word.

We call/invoke the function by using the function name, parentheses, and arguments in an expression.

如: big = max ('Hello world')

       print (big)         显示w  (Max function)

A function is some stored code that we use. A function takes some input and produces an output.

Once we have defined a function, we can call (or invoke) it as many times as we like.

This is the store and reuse pattern.

4. Arguments

An argument is a value we pass into the function as its input when we call the function.

We use arguments so we can direct the function to do different kinds of work when we call it at different times.

We put the arguments in parentheses after the name of the function. 如:

big = max ('Hello world') 中,Hello world为argument

5. Parameters

A parameter is a variable which we use in the function definition. It is a "handle" that allows the code in the function to access the arguments for a particular function invocation. 如:

def greet (lang) :

    if lang == 'es' :

        print ('Hola')

    elif lang == 'fr' :

        print ('Bonjour')

    else:

        print ('Hello')

greet ('en')          显示 Hello

greet ('es')          显示 Hola

greet ('fr')           显示 Bonjour

6. Return Values

Often a function will take its arguments, do some computation, and return a value to be used as the value of the function call in the calling expression. The return keyword is used for this.  如:

def greet () :

    return "Hello"

print (greet (), "Glenn")     显示 Hello Glenn

注:return 出来的value为def的值,这个值可以被print,也可以被assign等;而def后直接print(因为已经直接执行print了),则无法被assign。

A "fruitful" function is one that produces a result (or return value).

The return statement ends the function execution and "sends back" the result of the function. 如:

def greet (lang) :

    if lang == 'es' :

        return 'Hola'

    elif lang == 'fr' :

        return 'Bonjour'

    else:

        return 'Hello'

print (greet ('en'), 'Glenn')       Hello Glenn

print (greet ('es'), 'Sally')       Hola Sally

print (greet ('fr'), 'Michael')   Bonjour Michael

7. Multiple Parameters / Arguments

We can define more than one parameter in the function definition.

We simply add more arguments when we call the function.

We match the number and order of arguments and parameters.

 

 

 

 

 

 

 

[展开全文]

授课教师

课程特色

视频(9)

学员动态

horace 加入学习
Sun0422 完成了 Python 6
Sun0422 完成了 Python 7
Sun0422 完成了 Python 8
Sun0422 开始学习 Python 5