商业/数据分析师培养计划(2021年5月) 扫二维码继续学习 二维码时效为半小时

(0评价)
价格: 99999.00元

Python 6        Pandas

一. Pandas objects

Panda objects can be thought of as enhanced version of arrays. Similar to excel sheet commonly used by analysis which include rows and column. There are 3 fundamental data structure for pandas:

(1) Series

(2) DataFrame

(3) Index

1. Pandas series (类似于list)

One dimensional array of indexed data, including value of pandas series, index of pandas series, and access data via index.

(1) Unlike regular array and list, pandas series could explicitly define index.

(2) Construct pandas series using dictionary , 即类似于dictionary,可以自定义key和对应的value  

如何将dictionary转换成pandas series?如:

math_dict = {'Scarlet' : 80.0,  'Tony' : 96.0 , 'Thor' : 40.0 ,  'Hulk' : '??'}

math = pd. Series (math_dict)

2. Pandas DataFrame (类似于excel,相当于把多个series加在一起,成multiple dimensional table) (详见google colab)

DataFrame can be thought of either as a generalization of array, or as a specialization of python dictinoary. (例见google colab)

DataFrame is similar to series (also has index) , but unique for DataFrame (has columns).

Similar to dictionary, DataFrame map key to value as well. In this case the key is column name, and value is a series.

3. Reading data from files

(1) reading csv file

如: df = pd. read_csv ( '           .csv') (''内为file name)

(2) set name to be the index

如:   df = df. set_index ('name')

(3) set name to be the index when importing file (在reading csv file时设置名字为index)

如: df = pd. read_csv ('file name', index_col = 0) index_col=0表示以数据里的第一个column作为index

(4) reading from other type of files

如:df = pd. read_json ('        .json')

 

 

 

 

 

 

[展开全文]
穿迷彩的金刚狼 · 2021-06-29 · 0

Python 5

一. Review of Programs

1.  Object Oriented Language

A program is made up of many cooperating objects.

Instead of being the "whole program" - each object is a little "island" within the program and cooperatively working with other objects.

A program is made up of one or more objects working together - objects make use of each other's capabilities.

(1) Object

An Object is a bit of self-contained Code and Data.

A key aspect of the Object approach is to break the problem into smaller understandable parts (divide and conquer).

Objects have boundaries that allow us to ignore un-needed detail.

We have been using objects all along : String Objects, Integer Objects, Dictionary Objects, List Objects......

class is a reserved word that tells the object to run the code. 如:

class PartyAnimal :(Party Animal就是object)

  x = 0 (初始值)

  def party (self) :(class里的一个function)

     self. x = self. x + 1

     print ("So far", self. x)

an = PartyAnimal() (执行语句,给PA定义了值为an)

an. party() (附带以上function,即run party() within the object an)

"self" is a formal argument that refers to the object itself.

"self. x" is saying "x within self"

"self" is "global within this object"

(2) Object Lifecycle

Objects are created, used and discarded.

We have special blocks of code (methods) that get called: At the moment of creation (constructor)(如上例的x) and At the moment of destruction (destructor)(指object被删除掉后).

Constructors are used a lot.

Destructors are seldom used.

Constructor - the primary purpose of the constructor is to set up some instance variables to have the proper initial values when the object is created.

The constructor and destructor are optional. The constructor is typically used to set up variables. The destructor is seldom used.

class 中的built-in function: 

def __init__(self) : (相当于constructor的作用,也可以把初始值写到这个function下面),其作用是存储建立object的初始值

In object oriented programming, a constructor in a class is a special block of statements called when an object is created.

We can create lots of objects - the class is the template for the object.

We can store each distinct object in its own variable.

We call this having multiple instances of the same class.

Each instance has its own copy of the instance variables.

object中可以创建多个初始值,并用于之后的function中。 如:

class PartyAnimal :

    x = 0

    name = " "

    def __init__(self, nam) :

       self. name = nam

       print (self. name, "constructed")

    def party(self) :

       self. x = self. x + 1

       print (self. name, "party count", self.x)

s = PartyAnimal("Sally")

s. party()

j = PartyAnimal("Jim")

j.  party()

s. party()

Constructors can have additional parameters. These can be used to set up instance variables for the particular instance of the class (i.e., for the particular object).

Definitions: 

Class - a template - Dog

Method or Message - A defined capability of a class - Bark() 

Object or Instance - A particular instance of a class - Lassie

Constructor - A method which is called when the instance/object is created     

二. Reading and Writing files

Reading files

1. Opening a File

Before we can read the contents of the file, we must tell Python which file we are going to work with and what we will be doing with the file.

This is done with the open() function which returns a "file handle"- a variable used to perform operations on the file.

Similar to "File->Open" in a Word Processor

2. Using open()

handle = open(filename, mode)

如 fhand = open ('mbox. txt', 'r')

returns a handle use to manipulate the file

filename is a string

mode is optional and should be 'r' if we are planning to read the file and 'w' if we are going to write to the file.

3. The newline Character代表上一个string结束,开始了新的string

We use a special character called the "newline" to indicate when a line ends.

We represent it as \n in strings.

Newline is still one character - not two.

4. File Handle as a Sequence

A file handle open for read can be treated as a sequence of strings where each line in the file is a string in the sequence.     

We can use the for statement to iterate through a sequence.

Remember - a sequence is an ordered set

如:xfile = open ('mbox. txt')

      for cheese in xfile :

          print (cheese) 

5. Counting Lines in a File

Open a file read-only.

Use a for loop to read each line.

Count the lines and print out the number of lines.

如: fhand = open ('mbox. txt')

     count = 0

     for line in fhand :

         count = count + 1

     print ('Line Count:', count)

6. Reading the Whole File

We can read the whole file (newlines and all) into a single string

如:  fhand = open ('mbox-short. txt')

      inp = fhand. read()

      print (len (inp))        94626

      print (inp [:20])    From stephen.marquar

9. Searching Through a File

We can put an if statement in our for loop to only print lines that meet some criteria. 如:

fhand = open ('mbox-short. txt')

for line in fhand :

    if line. startswith('From: ') :

        print (line)

注: startswith() 是和string相关的一个function

OOPS!

What are all these blank lines doing here ?

Each line from the file has a newline(\n) at the end.

The print statement adds a newline to each line.

10. Searching Through a File (fixed)

We can strip the whitespace from the right-hand side of the string using rstrip() from the string library.(把找到的每行string后的\n去掉)

The newline is considered "whitespace" and is stripped.  如:

fhand = open ('mbox-short. txt')

for line in fhand :

    line = line. rstrip ()

    if line. startswith ('From: ') :

         print (line)

11. Skipping with continue

We can conveniently skip a line by using the continue statement(显示结果同上例). 如:

fhand = open ('mbox-short. txt')

for line in fhand :

     line = line. rstrip ()

     if not line. startswith ('From: ') :

         continue 

     print (line)

12. Using in to select lines

We can look for a string anywhere in a line as our selection criteria. 如:

fhand = open ('mbox-short. txt')

for line in fhand :

    line = line. rstrip ()

    if not '@uct.ac.za' in line :

        continue

    print (line)

 

 

Writing files

We could write to a file by using write function. 如:

fhand = open ('test. txt', 'w')

fhand. write ("Write some content here")

 

For example:

with open ('test_2. txt', 'r') as file1 :

    with open ('output_2. txt', 'w') as file2 :

        for line in file1 :

            line  = line. rstrip()

            if line. startswith ('From: ') :

                file2. write (line)

                file2. write ('\n')

file1. close ()

file2. close ()

 

 

 

 

[展开全文]
穿迷彩的金刚狼 · 2021-06-28 · 0

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.

 

 

 

 

 

 

 

[展开全文]
穿迷彩的金刚狼 · 2021-06-27 · 0

Python 3:  Data Type

-. String

A string is a sequence of characters.

A string literal uses quotes, 如'hello' or "hello"

For strings, + means "concatenate"

When a string contains numbers, it is still a string.

We can convert numbers in a string into a number using int()

We prefer to read data in using strings and then parse and convert the data as we need(input中输入的所有值,包括数字,type为string,如需与数字进行计算,需先转换为int或float)

This gives us more control over error situations and/or bad user input

(Raw) input numbers must be converted from strings

We can get at any single character in a string using an index specified in square brackets. The index value must be an integer and starts at zero, and the index value can be an expression that is computed. 如:

fruit = 'banana'

letter = fruit [1]

print (letter)    显示a

又如:x = 3

         w = fruit [x - 1]

         print (w)    显示n

You will get a python error if you attempt to index beyond the end of a string, so be careful when constructing index values and slices. (index不能超出string或list的字节长度)

There is a built-in function len() (可以用在string或list或dictionary下)that gives us the length of a string. The function is some stored code that we use and takes some input and produces an output. 如:

fruit = 'banana'

print (len (fruit))    显示6

1. Looping Through Strings

Using a while statement and an iteration variable, and the len function, we can construct a loop to look at each of the letters in a string individually. 如:

fruit = 'banana'

index = 0

while index < len (fruit) :

    letter = fruit [index]

    print (index, letter) (显示index是什么,所对应的letter是什么)

    index += 1 (在while loop中必须要在循环结束前,即print之后,添加一个让loop往前走的指令,跳出loop,否则loop会无限死循环)

显示 0 b; 1 a; 2 n; 3 a; 4 n; 5 a

A definite loop using a for statement is much more elegant. The iteration variable is completely taken care of by the for loop. 如:fruit = 'banana'

      for letter in fruit : 

          print (letter)

2. Slicing Strings

We can also look at any continuous section of a string using a colon operator(:). The second number is one beyond the end of the slice- up to but not including. If the second number is beyond the end of the string, it stops at the end. 如:

s= 'Monty Python'(字符数量从左至右,从0开始数,空格算一个字符)s [:] (:左右都不填数字,默认显示所有字符)

print (s [0:4])     显示 Mont(注意【】 左边的数字是包含本身字符在内的,右边的数字不包含本身字符在内,即本身第几个字符-1,【0:4】即第0个字符至第3个字符)

print (s [6:7])     显示 P

print (s [6:20])   显示 Python(如果:右边的数字超出了最后一个字符的顺序,或不填任何数值,则默认右边数字读取到最后一个字符,即从左边数字对应字符至结尾字符的全部字符

If we leave off the first number or the last number of the slice, it is assumed to be the beginning or end of the string respectively. 如:s = 'Monty Python'

      print (s [:2])     显示 Mo

      print (s [8:])     显示 thon

      print (s [:])       显示 Monty Python

Note:string最后一个字母或数字字符所对应的数字顺序为-1. 如:

s = 'Monty Python python3'

print (s [-1])     显示 3

print (s [:-1]).    显示 Monty Python python

print (s [::-1]) 显示 3onhtyp onhtyP ytnoM(即原所有字符倒写) 

3. String Concatenation

When the + operator is applied to strings, it means "concatenation". 如:

a = 'Hello'

b = a + 'There'

print (b)        显示 HelloThere

c = a + '' + 'There'     显示 Hello There

第二种连接方式,如:

a = 'hello'

b = 'there'

c = '!'

a + '' + b + '' + c     可显示hello there !

也可 ‘’. join ([a, b, c]) 显示hello there !

4.Using in as a logical Operator

The in keyword can also be used to check to see if one string is "in" another string.

The in expression is a logical expression that returns True or False and can be used in an if statement. 如:

fruit = 'banana'

'n' in fruit       显示 True

'm' in fruit      显示 False

'nan' in fruit    显示 True

if 'a' in fruit

    print ('Found it!')      显示 Found it!

5. String Library(可去python官网查看)

Python has a number of string functions which are in the string library. These functions are already built into every string- we invoke them by appending the function to the string variable. These functions do not modify the original string, instead they return a new string that has been altered.

lower()     upper()

6. The replace() function is like a "search and replace" operation in a word processor. It replaces all occurrences of the search string with the replacement string. 如:

a = 'Hello Bob'

b = a. replace ('Bob', 'Jane')

print (b)       显示 Hello Jane(将Bob替换成了Jane)

b = a. replace ('o', 'x')

print (b)       显示 Hellx Bxb(将o替换成了x)

7. Stripping Whitespace

Sometimes we want to take a string and remove whitespace at the beginning and/or end. lstrip() and rstrip() remove whitespace at the left or right. strip() removes both beginning and ending whitespace. 如:

greet = '      Hello Bob    '

greet. lstrip()       显示 ‘Hello Bob    '

greet. rstrip()       显示 ‘      Hello Bob'

greet. strip()        显示 ‘Hello Bob’

8. Parsing and Extracting- using find() 如:string = 'From stephen. [email protected] Sat Jan 5 09:14:16 2008'

a = string. find ('@')

print (a)                显示 21

b = string. find (' ', a)(a之后的第一个‘’)显示31

c = string [a + 1 : b]      显示 uct. ac. za

 

二. List & tuple

A list is a kind of collection.

A collection allows us to put many values in a single "variable".

A collection is nice because we can carry all many values around in one convenient package.

1. List Constants

List constants are surrounded by square brackets and the elements in the list are separated by commas.

A list element can be any Python object - even another list.

A list can be empty.

2. Looking Inside Lists

Just like strings, we can get at any single element in a list using an index specified in square brackets.

注:list中以element为顺序,从0开始

3. Lists are Mutable

Strings are "immutable" - we cannot change the contents of a string - we must make a new string to make any change.

Lists are "mutable" - we can change an element of a list using the index operator.

注:在list中可直接替换element. 如:

list = [2, 14, 26, 41, 63]

list [2] = 28

print (list)       显示 [2, 14, 28, 41, 63]

4. How Long is a List

The len() function takes a list as a parameter and returns the number of elements in the list.

Actually len() tells us the number of elements of any set or sequence (such as a string......)

5. Using the range function

The range function returns a list of numbers that range from zero to one less than the parameter.

We can construct an index loop using for and an integer iterator.

A tale of two loops:

friends = ['Joseph', 'Glenn', 'Sally']

for friend in friends :

    print ('Happy New Year:', friend).  or

for i in range (len (friends)) :

    friend = friends [i]

    print ('Happy New Year:', friend) 

两种方法显示结果一样

6. Concatenating lists using "+"

We can create a new list by adding two existing lists together. 如:

a = [1, 2, 3]

b = [4, 5, 6]

c = a + b

print (c)          显示 [1, 2, 3, 4, 5, 6]

注:如果list在相加时有重复的element,则不会去除相同element,即相加后的list会有重复值

7. Lists can be sliced using ":" 

Remember: just like in strings, the second number is "up to but not including"

8. List Methods

(1) Building a List from Scratch (append是在空list里加element,pop是在有element的list里删element,pop后面要注明element的顺序数字index,如没数字,默认删最后一个element)

We can create an empty list and then add elements using the append method. 

The list stays in order and new elements are added at the end of the list.

Pop function takes an element out of a list.

如:stuff = list ()

      stuff. append ('book')

      stuff. append (99)

      print (stuff)         显示 ['book' ,  99]

      stuff. append ('cookie')

      print (stuff)      显示 ['book',99,'cookie']

      stuff. pop (0)    显示 ‘book’

      print (stuff)      显示 [99, 'cookie']

(2) Is Something in a list ?

Python provides two operators that let you check if an item is in a list.

These are logical operators that return True of False. They do not modify the list. 如:

some = [1, 9, 21, 10,16]

9 in some              显示 True

15 in some.            显示 False

20 not in some       显示 True

(3) A List is an Ordered Sequence

A list can hold many items and keeps those items in the order until we do something to change the order.

A list can be sorted (i.e., change its order).

The sort method (unlike in strings) means "sort yourself".      如:

friends = ['Joseph' ,  'Glenn' , 'Sally']

friends. sort()

print (friends)  显示 ['Glenn', 'Joseph', 'Sally'](按照数字或string第一个字母,由小到大排序)

print (friends [1])     显示 Joseph

(4) Built-in Functions and Lists

There are a number of functions built into Python that take lists as parameters.

Remember the loops we built ? These are much simpler. 如:

nums = [3, 41, 12, 9, 74, 15]

print (len (nums))         显示 6

print (max (nums))       显示74

print (min (nums))        显示 3 

print (sum (nums))       显示 154

print (sum (nums) / len (nums))     显示 25

(5) Best Friends: Strings and Lists

Split breaks a string into parts and produces a list of strings. We think of these as words. We can access a particular word or loop through all the words.    如:

abc = 'With three words'

stuff = abc. split()

print (stuff)         显示 ['With', 'three','words']

print (len (stuff))     显示 3

print (stuff [0])       显示 With

When you do not specify a delimiter, multiple spaces are treated like one delimiter

You can specify what delimiter character to use in the splitting.     如:

line = 'A lot                     of spaces'

etc = line. split()

print (etc)       显示 ['A', 'lot', 'of', 'spaces']

line = 'first;second;third'

thing = line. split()

print (thing)     显示 ['first;second;third']

print (len (thing))     显示 1

thing = line. split(';')

print (thing)      显示 ['first', 'second', 'third']

print (len (thing))      显示 3

Sometimes we split a line one way, and then grab one of the pieces of the line and split that piece again.(Double Split Pattern)

9. Tuples are like lists- list 用[], tuple用()(tuples与lists的区别是tuple不能添,删,改,即不能变更,同时把list改成tuple时会把相同值duplicate elements进行删减)

Tuples are another kind of sequence that functions much like a list -  they have elements which are indexed starting at 0.

Unlike a list, once you create a tuple, you cannot alter its contents - similar to a string. (Tuples are "immutable"

Things not to do with tuples:  如:

sort() , append() , reverse() 等(list可以,tuple不行)

A tale of Two Sequences :

l = list ()

dir (l)

['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

t = tuple ()

dir (t)

['count', 'index']

10. Tuples are more efficient

Since Python does not have to build tuple structures to be modifiable, they are simpler and more efficient in terms of memory use and performance than lists. So in our program when we are making "temporary variables", we prefer tuples over lists.

11. Tuples and Assignment

We can also put a tuple on the left-hand side of an assignment statement. We can even omit the parentheses.   如:

(x, y) = (4, 'fred')

print (y)           显示 fred

(a, b) = (99, 98)

print (a)           显示 99

12. Tuples are Comparable

The comparison operators work with tuples and other sequences. If the first item is equal, Python goes on to the next element, and so on, until it finds elements that differ. 如:(0, 1, 2) < (5, 1, 2)        True

(0, 1, 2000000) < (0, 3, 4)       True

('Jones', 'Sally') < ('Jones', 'Sam')      True

('Jones', 'Sally') > ('Adams', 'Sam')     True

 

 

 

 

 

 

 

 

 

 

 

 

[展开全文]
穿迷彩的金刚狼 · 2021-06-25 · 0

Python 2

一. Conditional execution  

x = 5

if x < 10

    print 'smaller'

if x < 20

    print 'bigger'

print 'finis'

1. Comparison Operators

"=" is used for assignment(用来附值)

"==" is representative of "Equal to"

“!=” is representative of "Not equal"

Boolean expressions ask a question and produce a Yes or No result which we use to control program flow.

Boolean expressions using comparison operators evaluate to - True/False - Yes/No.

Comparison operators look at variables but do not change the variables.

2. One-Way Decisions

3. Indentation

Increase indent after an if statement or for statement (after :);

Maintain indent to indicate the scope of the block (which lines are affected by the if/for);

Reduce indent back to the level of the if statement or for statement to indicate the end of the block;

Blank lines are ignored- they do not affect indentation;

Comments on a line by themselves are ignored with regard to indentation. 

4. Nested Decisions(在if statement下再套一个condition, 即套的condition是基于if statement的condition一起使用),如:

x = 42

if x > 1 :

    print 'more than one'

    if x < 100 :

        print 'less than 100'

print 'all done'

5. Two-way Decisions

Sometimes we want to do one thing if a logical expression is true and something else if the expression is false.

It is like a fork in the road- we must choose one or the other path but not both.

如:x = 4

      if x > 2 :

          print ('bigger')

      else :

          print ('smaller')

      print ('all done')

6. Multi-way Decisions   如下所示

if x < 2 :

    print ('small')

elif x < 10 :

    print ('medium')

else :

    print ('large')

print ('all done')

7. The try/except Structure (对code试错)

You surround a dangerous section of code with try and except.

If the code in the try works- the except is skipped ; If the code in the try fails- it jumps to the except section. 如:

astr = 'Bob'

try:

    print ('hello')

    istr = int (astr)

    print ('there')

except:

    istr = -1

print ('done', istr)

该代码在istr=int(astr)会报错,因此python会跳过该行代码,继续执行except的命令。

二. Loop execution (循环语句执行)

1. Repeated Steps- 用 ‘while’,要确保在while中的指令能最终跳出loop,否则会一直不停循环,直到报错

Loops (repeated steps) have iteration variables that change each time through a loop. Often these iteration variables go through a sequence of numbers. 如:

n = 5

while n > 0 :

    print (n)

    n = n - 1

print ('blastoff!')

print (n)

2. Breaking Out of a Loop (跳出loop,执行以后的指令)

The break statement ends the current loop and jumps to the statement immediately following the loop. It is like a loop test that can happen anywhere in the body of the loop (跳出循环指令结束). 如:

while True :

    line = input ('> ')

    if line == 'done' :

        break

    print (line)

print ('Done!')

3. Finishing an Iteration with continue (不会跳出loop,跳回重新开始loop,不会再执行以后的指令)

The continue statement ends the current iteration and jumps to the top of the loop and starts the next iteration. (continue结束当前loop,从头开始loop)如:

while True :

    line = input ('> ')

    if line [0] == '#' :

        continue

    if line == 'done' :

        break

    print (line)

print ('Done!')

4. Indefinite Loops

While loops are called "indefinite loops" because they keep going until a logical condition becomes False.

The loops we have seen so far are pretty easy to examine to see if they will terminate or if they will be "infinite loops".

Sometimes it is a little harder to be sure if a loop will terminate.

5. Definite Loops

Quite often we have a list (list()或者[])of items of the lines in a file- effectively a finite set of things.

We can write a loop to run the loop once for each of the items in a set using the Python for construct.

These loops are called "definite loops" because they execute an exact number of times.

We say that "definite loops iterate through the members of a set". 如:

for i in [5, 4, 3, 2, 1]  :

    print (i)

print ('Blastoff!')

another example:

friends = ['Joseph', 'Glenn', 'Sally']

for friend in friends :

    print ('Happy New Year: ', friend)

print ('Done!')

Definite loops (for loops) have explicit iteration variables that change each time through a loop. These iteration variables move through the sequence or set.

for i in [5, 4, 3, 2, 1] :

    print (i)

i是iteration variable,中括号中的数字是five-element sequence, print部分是block(body).

The iteration variable "iterates" through the sequence (ordered set).

The block (body) of code is executed once for each value in the sequence.

The iteration variable moves through all of the values in the sequence.

What we do in loops? The trick is "knowing" something about the whole loop when you are stuck writing code that only sees one entry at a time.

Set some variables to initial values

for thing in data :

    Look for something or do something to each entry separately, updating a variable

Look at the variables(跳出loop后variable执行的指令)

Finding the largest value(for...in 应用,见google colab)

6. The "is" and "is not" Operators (类似SQL)

7. Counting in a Loop- To count how many times we execute a loop, we introduce a counter variable that starts at 0 and we add one to it each time through the loop. 如:

zork = 0

print ('Before', zork)

for thing in [9, 41, 12, 3, 74, 15] :

    zork = zork + 1 (或zork += 1)

    print (zork, thing)

print ('After', zork)

8. Summing in a Loop- To add up a value we encounter in a loop, we introduce a sum variable that starts at 0 and we add the value to the sum each time through the loop. 如:

zork = 0

print ('Before', zork)

for thing in [9, 41, 12, 3, 74, 15]

    zork = zork + thing

    print (zork, thing)

print ('After', zork)

9. Filtering in a Loop- We use an if statement in the loop to catch/filter the values we are looking for. 如:

print ('Before')

for value in [9, 41, 12, 3, 74, 15] :

    if value > 20 :

        print ('large number', value)

print ('After')

10. Search Using a Boolean Variable- If we just want to search and know if a value was found, we use a variable that starts at False and is set to True as soon as we find what we are looking for. 如:

found = False

print ('Before', found)

for value in [9, 41, 12, 3, 74, 15] :

    if value == 3 :

        found = True

    print (found, value)

print ('After', found)

 

 

 

 

 

[展开全文]
穿迷彩的金刚狼 · 2021-06-22 · 0

Python 1:

Python course syllabus:

Class 1: Intro+Variable

Class 2: Condition+Loop execution

Class 3: Data type- string, integer, list, tuple

Class 4: Function&class+ Data type-dictionary

Class 5: reading and writing files

一. Python environment

Jupyter notebook(本地)

Colab: Google cloud based(云端,需要google account)

二. Variable

1. Constants: fixed values such as numbers, letters, and strings are called "constants"(常量),because their value does not change.

Numeric constants are as you expect; String constants(一般指字母) use single quotes(') or double quotes(")(数字不加引号,字母可加单引号或双引号)

2. Variables: a variable is a named place in the memory where a programmer can store data and later retrieve the data using the variable "name".

Programmers get to choose the names of the variables. You can change the contents of a variable in a later statement.

3. Python Variable Name Rules: 

Must start with a letter or underscore_,如:spam  egg  spam23  _speed, 如果这样写23spam #sign var.12,python读不出来(variable里不能有点,#或空格等字符)

Must consist of letters and numbers and underscores 

Case Sensitive:大小写不一样,记录的variable也不一样 

4. Reserved Words(python内设词): you cannot use reserved words as variable names/identifiers(python系统内部已设置好的variable,是无法复制的,但如果把首字母改成大写,也是可以在这类词附加变量值,但一般不这么操作,以免出现confusion),如:and,del,for,is,raise,assert,elif,from,lambda,return,break,else,global,not,try,class,except,if,or,while,continue,exec,import,pass,yield,def,finally,in,print,as,with

5. Sentences or Lines展示

x = 2(assign 2给 x)

x = x+2 或 x += 2(replace x的值等于原x+2 )

print x (return x的结果)

x是variable,=和+是operator,2是constant,print是reserved word

6. Numeric Expressions

because of the lack of mathematical symbols on computer keyboards, we use "computer-speak" to express the classic math operations, 如:**代表power(指数,2 ** 3 = 8),%代表 remainder(余数)

7. Operator Precedence Rules

Highest precedence rule to lowest precedence rule(由高到低):

Parenthesis are always respected

Exponentiation

Multiplication, DIvision, and Remainder

Addition and Subtraction

Left to right

8. Type: in Python variables, literals and constants have a "type". Python knows the difference between an integer number and a string.(data type必须相同,才能做相关的operation, 可以数字+数字,字符+字符,不能数字+字符或字符+数字)

9. Several Types of Numbers: two main types

Integers are whole numbers, 如:-14,-2,0,1,100,401233

Floating Pointing Numbers have decimal parts, 如:-2.5,0.0,98.6,14.0

There are other number types- they are variations on float and integer

10. Type Conversions:

Integer conversions-When you put an integer and floating point in an expression, the integer is implicitly converted to a float, you can control this with the built-in functions int() and float().

String conversions-You can also use int() and float() to convert between strings and integers. You will get an error if the string does not contain numeric characters.(数字可以通过int()和float()把包含数字的string转换成数字,或把数字转换成string,但不能把不含任何数字的string转换成数字)

11. User Input: We can instruct Python to pause and read data from the user using the raw_input() function. The raw_input() function returns a string.

12. Comments in Python:

Anything after a # is ignored by Python.

Why comment- describe what is going to happen in a sequence   of code; document who wrote the code or other ancillary information; turn off a line of code perhaps temporarily

13. String Operations

some operators apply to strings, 如 + implies “concatenation”,print 'abc' + '123' returns abc123;* implies "multiple concatenation", print 'Hi' * 5 returns HiHiHiHiHi

Python knows when it is dealing with a string or a number and behaves appropriately

 

 

 

 

 

 

 

 

[展开全文]
穿迷彩的金刚狼 · 2021-06-20 · 0

SQL 5:

1. JOIN:ON indicates how the two tables (the one after the FROM and the one after the JOIN) relate to each other.

如:ON teams.school_name = players.school_name

.* to return all the columns

2. INNER JOIN: eliminate rows from both tables that do not satisfy the join condition set forth in the ON statement;

In mathematical terms, an inner join is the intersection of the two tables.

The results can only support one column with a given name.

3. OUTER JOIN: in an outer join, unmatched rows in one or both tables can be returned.

There are a few types of outer joins:

LEFT JOIN returns only unmatched rows from the left table;

RIGHT JOIN returns only unmatched rows from the right table;

FULL OUTER JOIN returns unmatched rows from both tables.

4. WHERE OR ON:

在INNER JOIN中,ON......后可以加and(后加限定的内容,相当于JOIN的条件),与INNER JOIN后加WHERE限定是无差别的(都可以);

在LEFT JOIN中是不同的,因为在and后先filter,然后再join;在WHERE后,是先join,再filter

5. UNION: allows you to stack one dataset on top of the other, and allows you to write two separate SELECT statements,you can treat them differently before appending.

UNION only appends distinct values;

UNION ALL append all the values from the second table 

UNION ALL < UNION?

6. 用ON做JOIN的连接词,可以加任何conditional statement, 如ON..... AND.....运算或>,<,!=等条件

7. SELF JOIN: join a table to itself

the same table can easily be referenced multiple times using different aliases

 

[展开全文]
穿迷彩的金刚狼 · 2021-06-18 · 0

SQL3:

1. Using comments

use--(two dashes) to comment out everything to the right of them on a given line

leave comments across multiple lines using /* to begin the comment and */ to close it(以/*开始,以*/结束)

可以用comments对query中可能产生的错误进行标注,如在SELECT中任一list前加--(two dashes)可使该list不产生results,以检查是否该list出现了错误(debug)

Basic SQL结束

二. Intermediate SQL

1. SQL Aggregate Functions

Arithmetic operators only perform operations across rows. Aggragate functions are used to perform operations across entire columns.

COUNT: SELECT COUNT (*) 或 (1)

不包含空值(not include null value)

SUM: only used on columns containing numerical values;

only aggragate vertically;

treats nulls as 0

MIN/MAX: SQL aggregation functions that return the lowest and highest values in a particular column.

MIN will return the lowest number, earliest date, or non-numerical value as close alphabetically to "A" as possible.

MAX returns the highest number, the latest date, or the non-numerical value closest alphabetically to "Z"

AVG: only be used on numerical columns and ignores nulls completely

2. GROUP BY: aggregate only part of a table, separate data into groups, which can be aggregated independently of one another

GROUP by multiple columns- separate column names with commas.

Using GROUP BY with ORDER BY(把GROUP BY的结果进行排序整理)

最后可加LIMIT限制显示数据结果

注:GROUP BY出现的部分必须要出现在SELECT中(SELECT要与GROUP BY的内容一致)

3. HAVING: to filter a query that has been aggregated.(作用类似于WHERE,但WHERE doesn't allow you to filter on aggregate columns),一般用在GROUP BY之后

WHERE在aggregate之前使用,HAVING在aggregate之后使用

Query clause order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT

4. CASE WHEN... THEN: SQL's way of handling if/then logic

always goes in the SELECT clause; every CASE statement must end with the END statement; the ELSE statement is optional, and provides a way to capture values not specified in the WHEN/THEN statements.

常见:CASE WHEN... THEN... (ELSE) END

 

 

 

 

 

 

 

[展开全文]
穿迷彩的金刚狼 · 2021-06-17 · 0

SQL 4:

1. CASE WHEN condition 1 THEN result 1

            WHEN condition 2 THEN result 2

             ELSE result 3 END AS column_name

2. Using CASE with aggregate functions:

Write a query containing the CASE statement first, then COUNT, SUM, AVG......

3. Using CASE inside of aggregate functions (Pivoting)

COUNT (CASE WHEN)

4. DISTINCT: used for viewing unique values.

include multiple columns in a SELECT DISTINCT clause, the results will contain all of the unique pairs of those columns.

注:include DISTINCT once in the SELECT clause

particularly helpful when exploring a new data set. 

using DISTINCT in aggregations-

COUNT DISTINCT

COUNT (DISTINCT CASE WHEN.....)

using DISTINCT, particularly in aggregations, can slow your queries down quite a bit.

5. JOIN: working with data from multiple tables

SELECT * 

FROM a table JOIN another table ON....

重命名不用加AS

 

 

[展开全文]
穿迷彩的金刚狼 · 2021-06-15 · 0

上节课作业:

question: <='N',如果出现N,等<'O',为什么=N不能出现N?

SQL2:

1. LIKE: match on similar values

用在WHERE中,在限定中找相似,加%或-和‘’

ILIKE不区分大小写,LIKE区分大小写

2. IN: specify a list of values that you'd like to include in the results

用在WHERE中,value用逗号连接,非数字加‘’

3. BETWEEN AND

4. IS NULL

5. AND

6. OR

AND和OR一起用要加(),注意括号位置

7. NOT

IS NOT NULL用的较多,NOT不能与>,<,=一起使用

8. ORDER BY

默认按照升序顺序排列,降序排列在ORDER BY后加DESC,先数字,再字母,如ORDER BY year DESC, year_rank DESC(先按照year降序排序,再按照year rank降序排序),ORDER BY year,year_rank DESC(先按照year升序排序,再按照year rank降序排序)

以各种分组排列,中间加逗号,如ORDER BY year, year_rank

用在限定环境中,放在WHERE之后

 

 

[展开全文]
穿迷彩的金刚狼 · 2021-06-15 · 0