Python Notes

#+SETUPFILE: theme-bigblow.setup

* faq
** rename
>>> for quizNum in range(35):
    quizFile=open('capitalsquiz%s.txt' %(quizNum +1), 'w')

#Python3+
name = input("who are you? ")
print("hello %s" % (name,))

* Installation
To check whether you OS is 32-bit or 64-bit
#+begin_src
uname -m
#+end_src

which may return:
=x86_64=

#+begin_src
~sudo apt-get install python3~
~sudo apt-get install idle3~
~sudo apt-get install python3-pip~
#+end_src

* Introduction
** intro
- python :: interpreted, high-level, general-purpose programming language
With python, you can automate simple tasks such as the following:

-    Moving and renaming thousands of files and sorting them into folders

-    Filling out online forms, no typing required

-    Downloading files or copy text from a website whenever it updates

-    Having your computer text you custom notifications

-    Updating or formatting Excel spreadsheets

-    Checking your email and sending out prewritten responses
(Python for Non-Programmers[b])

All programs use basic instructions as building blocks.
Here are a few of the most common ones, in English:

    “Do this; then do that.”

    “If this condition is true, perform this action; otherwise, do that action.”

    “Do this action that number of times.”

    “Keep doing that until this condition is true.”

** platforms

- IPython :: An enhanced Interactive Python

- anaconda

- python tutor -- visualize code

1. IDLE
2. terminal
  python file.py  3. [[https://www.spyder-ide.org/][spyder]] -- scientific python development environment  To Starte interactive development environment (IDLE) on Ubuntu #+begin_src idle3 #+end_src  ** python on Windows   ** python on Ubuntu - interactive - batch mode   to exit  > Ctrl+d *** batch mode 1. open a terminal on Ubuntu:    >> C+Alt+t 2. load python scripts    >> python3 test.py  *** interactive 1. open a terminal on Ubuntu:    >> C+Alt+t 2. load python    >> python3  ** reserved words - reserved words can not be used as variable names/indentifiers  and, del, for, is, raise assert, elif, from, lambda, return, break, else, global, not, try, class, except, if, or, while, continue, pass, yield, def, in, print   , if , or , while  ** read and write files  *To add a directory to a working directory*  >>> cd /home/kaiming/Documents/Python  >>> /usr/bin/idle3  *To  print out the current directory*  >>> import os  >>> os.getcwd()  To write 'hello world' to 'bacon.txt'  >>> baconFile=open('bacon.txt','w')  >>> baconFile.write('hello world \n')  - objects :: an encapsulation of variables and functions into a single entity  ** change directory  \ -->> Windows  / -->> Linux  *currrent working directory*  - cwd :: current working directory  os.getcwd() -->> get the current workding directory as a string value  os.chdir() -->> change directory  To get the current directory:  >>> import os >>> os.getcwd()  To get the =current working directory= as a string value with =os.getcwd()= function and change it with =os.chdir()=. enter the followting into the  interactive shell:  ********* >>> import os  # os module >>> os.getcwd() # get Current Working Directory '/home/kaiming/Documents/Python'  >>> os.chdir('/home/kaiming/Documents/Python/test') # Change DIRectory >>> os.getcwd() '/home/kaiming/Documents/Python/test' *********  To create new folders with =os.makedirs()=  ****************** >>> import os >>> os.makedirs('/user/Documents/newfolder') ******************  example of *plaintext files*: - .txt - .py  ** print  Try the following in your console:  Notice how if we have two print statements                 #+BEGIN_SRC Python print("Hi") print("there") #+END_SRC  The output will have each string on a separate line:                 #+BEGIN_SRC Python Hi there #+END_SRC Now try adding the following: #+BEGIN_SRC Python print("Hi",end='') print("there") print("Hi",end='*') print("there")    #+END_SRC  The output will place the subsequent string on the same line and will connect the two prints with the character given by end #+BEGIN_SRC Python Hithere Hi*there                 #+end_SRC  ** Absolute vs. Relative paths  ./  # current directory ../ # home directory file:figures/python/relative_absolute_paths.png  ** dictionaries -- {} Jan -->> January May -->> March #+BEGIN_SRC Python monthConversions = {  "Jan": "January",  "Feb": "February",  "Mar": "March", }  print(monthConversions["Jan"]) #+END_SRC   ** file reading/writing process  file types: - *plaintext* files, such as =.txt=, =.py= - *Binary* files, such as =.docx=, =.pdf=, iamges, spreadsheets, and executable programs(.exe)  *steps to read/write files*  1. call the =open()= function to return a =File object= 2. Call the =read()= or =write()= method on the File object 3. Close the file by calling the =close()= method on the File object  *To open a file in 'reading plaintext' mode (read mode):*    >>> helloFile=open('/user/kaiming/Python/hello.txt')    >>> helloFile=open('/user/kaiming/Python/hello.txt', 'r')    where 'r' stands for =read mode=; =open()= returns a =File object=, and assigned to the variable =helloFile=  - file object :: a file on your computer  *To open a binary file with read mode :* >>> f=open('users/michael/test.jpg','rb')  To get a list of string values from the file, /one string for each line of text/, use =readline()= function  *Reading the contents of files* 1. to read /the entire contents of a file/ as a string value:   *************    >>> helloContent = helloFile.read()   >>> helloContent   'hello world!' ************* 2. To read one sting for each line of text, use =.readline()= e.g. the contents  *writing to files*  note:  1. you can not write to a file you've opened in read mode 2. you need to open it in a 'write plaitext'(*write mode*) or 'append plaintext' (*append mode*)   + write mode will overwrite the existing file and start from scratch, just like when you overwrite a variable's value with a new value      *Writing to files*   >>> open ('hello.txt', 'w')  # write mode   >>> open ('hello.txt', 'a')  # append mode  >>> f = open('/Users/michael/test.txt', 'w') >>> f.write('Hello, world!') >>> f.close()  你可以反复调用write()来写入文件,但是务必要调用f.close()来关闭文件。 当我们写文件时,操作系统往往不会立刻把数据写入磁盘, 而是放到内存缓存起来,空闲的时候再慢慢写入。 只有调用close()方法时,操作系统才保证把没有写入的数据全部写入磁盘  忘记调用close()的后果是数据可能只写了一部分到磁盘,剩下的丢失了。所以,还是用with语句来得保险:  **********  with open('/Users/michael/test.txt', 'w') as f:      f.write('Hello, world!') **********   ******** >>> with open("test.txt",'w',encoding = 'utf-8') as f:         f.write("my first file\n")         f.write("This file\n\n")         f.write("contains three lines\n") 15 11 22 ************  To read a file =line-by-line= using a *for loop*  *************** >>> f=open('test.txt','r') >>> for line in f:     print(line, end ='')       my first file  this file  contains three lines  ********  where, ~\n~ means /next line/   Note:  1. when a file is opened in =read mode=, Python lets you only read data from the file; you can't write or modify it in any way.  2. if the filename passed to =open()= does not exist, both write and append mode will create a new, blank file 3. remember to close the file with =.close()=   ************* #+BEGIN_SRC Python >>> baconFile = open('bacon.txt', 'w')  # create a blank file named 'bacon.txt' >>> baconFile.write('Hello world!\n') 13 >>> baconFile.close() >>> baconFile = open('bacon.txt', 'a') >>> baconFile.write('Bacon is not a vegetable.') 25 >>> baconFile.close() >>> baconFile = open('bacon.txt') >>> content = baconFile.read() >>> baconFile.close() >>> print(content) Hello world! Bacon is not a vegetable. #+END_SRC  ************* - pprint.pprint() :: 'pretty print' the contents of a list or dictionary to the screen - pprint.pformat() :: retun text as a string ** project: generating random quiz files (P187, Sweigart) To generate =35= files named 'capitalsquiz1.txt', 'capitalsquiz2.txt'...  file:figures/python/quiz_files.png  ************** >>> for quizNum in range(35):     quizFile=open('capitalsquiz%s.txt' %(quizNum +1), 'w')  ************** where, The filenames for the quizzes will be =capitalsquiz<N>.txt=,  where =<N>= is a unique number that comes from =quizNum=   ** Open files in Python  text modes: - ACII mode - binary mode   e.g.   1. To open the =test.txt= file ~>>> f=open("/users/jack/test.txt","r")~  in above code opens a file, then assign the resulting file object in a variable, =f= - r :: read in ASCII mode, use "wb" if you write in binary mode - open () :: open function  2. To read full contents of =test.txt= , then assign it a variable 'StrContent'     ~>>> StrContent = f.read()~ 3. To close the file  ~>>> f.close()~  ** Reference https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431917715991ef1ebc19d15a4afdace1169a464eecc2000 ** rules | case sensitive | yes                                                   | | #              | comment                                               | | Alt+n          | next command                                          | | Alt+p          | previous                                              | | %s             | string formatting syntax                              | | \n             | line break                                            | | %              | mudulo operator, used for string formatting operation | |                |                                                       | single quote, ' and double quote, " are different  * Ch1 ** Rules | \n | next line | |    |           |  ** first script on IDLE To Starte interactive development environment (IDLE) on Ubuntu #+begin_src idle3 #+end_src   To open the =file editor= in IDLE, select =File/new file=   #+BEGIN_SRC Python  # this program says hello and asks for my name print('hello world!')              # 'print' function print('what is your name?') myName=input()                   # assignment; 'input' function print('It is good to meet you,' +MyName) print('the length of your name is:') print(len(myName))         # 'len' function #+END_SRC   then save the file as =hello.py=      >> File/save as  To run the program  >>> run/run module (F5)  ** Variable    Do you need to pre-define a variable? - no  *rules for variable names* 1. it can be only one word 2. it can use only letters, numbers, and the underscore(_)  3. it can't begin with a number   | spam   | variable | | "spam" | string   | |        |          |  ** string - string :: 字符串, a sequence of characters      >>> character_name = "Tom"      >>> character_age = 50      >>> is_male = False      >>> print("This \n is")      >>> phrase = "kemin"  # upper case  >>> print (phrase.upper())      KEMIN  >>> print (phrase[0])     k - character starts from 0  quotation mark in string  >>> print ("kemin\" academy")  append string  >>> phrase = "kemin" >>> print ("kemin" + " is cool")  >>> print(len(phrase)) >>> print(phrase.index("k"))  # replace string >>> print(phrase.replace("kemin", "kaiming"))  | .upper()    | capital letter                                       | | .lower()    | lower case                                           | | isupper()   | check is it upper or lower, return 'true' or 'false' | | len(phrase) | character length                                     | | phrase[0]   | 1st letter                                      | | phrase.index("k") | index function                        |      字符串是以单引号'或双引号"括起来的任意文本,比如'abc',"xyz"等等。 请注意,''或""本身只是一种表示方式,不是字符串的一部分, 因此,字符串'abc'只有a,b,c这3个字符。 如果'本身也是一个字符,那就可以用""括起来  如果字符串内部既包含'又包含"怎么办?可以用转义字符\来标识,比如:  >>> 'I\'m \"OK\"!'     I'm "OK"!   | \n | 换行   | | \t | 制表符 | | \  | 转义   | | \\ | \      | ***  String formatting the '%' operator is used to format a set of variables enclosed in a "tuple" ( a fixed size list) | %s                    | string                                                                       | | %d                    | integers                                                                     | | %f                    | floating point numbers                                                       | | %.<number of digits>f | floating point numbers with a fixed amount of digits to the right of the dot | | %x or %X              | Integers in hex representation                                               | |                       |                                                                              |  name="kemin" print("hello %s" name)   *** string operation a string is immutable; it can't be changed  *joining sting* = string concatenation  Stings can join using concatenation operator, "+" #+BEGIN_SRC Python     >>> a='Do you see this, '     >>> b='?>>>a+bDoyouseethis,?’ #+END_SRC   #+BEGIN_SRC Python     >>> a='file'     >>> b='.txt'     >>> a+b  ‘file.txt’ #+END_SRC  ** data types -- integer, floating point, and string  +   # joins the strings  #+BEGIN_EXAMPLE  >>> 'Alice' + 'Bob' 'AliceBob' #+END_EXAMPLE le and asignment statement  ~spam =42~    ## a variable named =spam= has the integer value =42= - variable names are case sensitive  ** numbers To change number to string  print(3+4) | pow(3,2) | 3^2 | | max(4,6) | maximum |    | from math import * | load math functions | | int()              | integer             | | float()            | float number        | |                    |                     |  #+BEGIN_SRC from math import *  >>> num=5 >>> print(num) 5 >>> print(str(num)) 5 >>> print(str(num)+"my num") 5my num #+END_SRC  ** input from users #+BEGIN_SRC name = input("Enter your name:") print("Hello " + name + "!") #+END_SRC  #+BEGIN_SRC name = input("Enter your name:") age = input("Enter your age:") print("Hello " + name + "! you are " + age) #+END_SRC  ** build a basic calculator num1 = input("enter a number") num2 = input("enter another number") result = int(num1) + int(num2) print(result)  ** list    ** Entering Expressions into the Interactive Shell open a new terminal window and type      ~idle3~ Enter 2 + 2 at the prompt to have Python do some simple math:    >>> 2 + 2   4  expression, value, operator   In python, =2+2= is named *expression* - expressions include *values* (such as 2), and *operators* (e.g. + ) - expressions can always evaluate down to a single value ** common python errors http://nostarch.com/automatestuff/ ** math operator | Operator | exponent | Example | results | | *        | exponent | Example | results | | %        |          |         |         | | +        | add      |         |         |  * Dictionaries and stucturing data dict=dictionary  举个例子,假设要根据同学的名字查找对应的成绩,如果用list实现,需要两个list:  names = ['Michael', 'Bob', 'Tracy'] scores = [95, 75, 85]  给定一个名字,要查找对应的成绩,就先要在names中找到对应的位置,再从scores取出对应的成绩,list越长,耗时越长。  如果用dict实现,只需要一个“名字”-“成绩”的对照表,直接根据名字查找成绩,无论这个表有多大,查找速度都不会变慢。用Python写一个dict如下:  >>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} >>> d['Michael'] 95  * IDE tools pycharm -- an IDE for python  SPYDER  anaconda  vscode ** [[file:pycharm.org][pycharm]]  * Flow Control -- for, boolean  *flow control statements* decide which Python isntructions to  execute under which conditions  ** for loop for letter in "kemin is a guy":     print(letter)  ** while loop i = 1 while i <= 10:       print(i)       i = i+1 print("done with loop")  example 2  secret_word = "kemin" guess = "" guess_count = 0 guess_limit = 3 out_of_guesses = False  while guess ! = secret_word and not (out_of_guesses):     if guess_count < guess_limit:        guess = input ("Enter guess: ")        guess_count +=1     else:         out_of_guesses = True   if out_of_guesses:      print("out of guesses, you Lose" else:     print("you win!") ** for loop -- iterate over all lines  a file object can be used as a collection with the =for= loop  you can iterate over all lines in a file (a common action) with the =for= statement.  e.g. ~for line in f:print line~ #+BEGIN_SRC Python for quizNum in range(35):     quizFile=open('capitalsquiz%s.txt' %(quizNum +1), 'w') #+END_SRC  where, '%s' is replaced by 'quizNum+1'  https://stackoverflow.com/questions/997797/what-does-s-mean-in-a-python-format-string  ** for loops and the range() function https://www.programiz.com/python-programming/for-loop  *What is =for= loop in Python?* =for= loop is used to iterate over a /sequence/ or other iterable /objects/.   To luanch interactive shell:    >>> idle3   >>> ~for i in range(5):~  which includes: - ~for~ is a keyword - ~i~ is a variable - ~in~ is a keyword - ~range()~ - ~:~ is a colon, which inplies a code block if the following commands are indented   #+BEGIN_SRC Python >>> print('my name is ') my name is  >>> for i in range(5):     print('Jimmy Five Times ('+str(i)+')') #+END_SRC  then the output is as follows #+BEGIN_EXAMPLE    Jimmy Five Times (0)   Jimmy Five Times (1)   Jimmy Five Times (2)   Jimmy Five Times (3)   Jimmy Five Times (4) #+END_EXAMPLE  In the above code, =str()= is a =string= function    exaple 2: Goal: to find the sum of all numbers stored in a =list=  #+BEGIN_SRC Python # Program to find the sum of all numbers stored in a list  # List of numbers numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]  # variable to store the sum sum = 0  # iterate over the list for val in numbers:     sum = sum+val  # Output: The sum is 48 print("The sum is", sum)  #+END_SRC   example 3: sum of 1+2+...+100 #+BEGIN_SRC Python sum=0 for x in range (101):    sum=sum+x print(sum) #+END_SRC   ** Boolean Values ** Comparison Operators * Python - Eric Grimson, MIT 26/06/2019 [[https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/references/][Intro to computer sicence and programming]]--Eric Grimson, MIT - [[https://www.cs.utah.edu/~germain/PPS/Topics/recursion.html][Recursion]]   A *computational mode of thinking* means that  everything can be viewed as  a math problem involving numbers and formulas.  ** faq  *** Algorithm vs Program An *algorithm* is a _conceptual idea_,  a *program* is a _concrete instantiation of an algorithm_.   ** goal - computational thinking - understand code - cabilities and limits - problem description  make heavy use of libraries  categories of knowledge: - declaretive, e.g. sqrt(x) = y, thus, y^2 =x, y is positive - imperative, e.g. a descrition of how to do something  e.g. start G,  if G is close to X, stip, return-->> G else, G <<-- G= (G+x/G)/2, repeat the new G  *six primitives*   - high level vs low level - general vs targetted - interpreted vs compiled - syntax - what are the legal languages - static semantics - which programs are meaningful http://open.163.com/movie/2010/6/D/6/M6TCSIN1U_M6TCSTQD6.html  values - number, e.g. 3(integer), 4.15(floating points) - strings, 'abc'  >>> 3/5  0 ## integer division  >>> 3.0/5 0.599998 ** language  6  primitive operations  - a programming language provides a set of primitive operations types of programming languages:   + number    + strings   + simple operators  syntax vs semantic  - Syntax :: Determines whether a string is legal  - semantics :: Assigns a meaning to a legal sentence  - Static semantics :: Determines whether a string has meaning - semantics :: (oxford) the meaning of words, phrases or systems -  semantics :: the meaning associated with a syntactically correct string of symbols with no static semantic errors ** types - program :: a sequence of definitions and commands - commands :: (staements) struct interpreter to do something - object :: data types  types of data: - scalar, can not be subdivided - non-scalar, have internal structure that can be accessed  scalar objects:    + int   + float   + bool - boolean values: True or False   + NoneType, special and has one value, None you can use =type()= to see the type of an =object=  >> type(3)   int  *type conversion (cast):*  >>> float(3)     3.0   ** variables radius =2 radius = radius + 1 |radius += 1|radius = radius + 1|  ** operators and branching *comparison* operators (==) on =int= and =float= |==| comparison | *operators on ints and floats* | expression | operation           | type                                                                                  | | i+j        | sum                 | if both are =ints=, result is =int=, if either or both are =floats, result is =float= | | i-j        | difference          | if both are =ints=, result is =int=, if either or both are =floats, result is =float= | | i*j        | the product         | if both are =ints=, result is =int=, if either or both are =floats, result is =float= | | i/j        | division            | int in =python2=, float in =python3=                                                  | | i//j       | int division        | result is =int=, quotient without remainder, e.g. =9//2= -->> =4=                     | | i%j        | remainder           | 9%2 -->> 1                                                                            | | i**j       | i to the power of j |                                                                                       | | round(2.6) |                     | 3, type, =int=                                                                        | | i!=j       | inequality          |                                                                                       | file:figures/python/operators_ints_floats.png   >>> a=3 >>> a==5.0 False >>> a 3  i>j  2>3  2<3  i>=j  i ==j  # equality test, true if i equals j     >>> 2==2        True  >>> 4>+4 False  >>> 5*2 == 5.0 * 2.0 True  >>> round(2.6) 3.0    *logic* operators on *bools*  =a= and =b= are any variable names  not a   # true if =a= is False  if  | elif | else if | |      |         | ** variables ** strings - letters, special characters, spaces, digits - enclose in *quotation marks or single quotes*       hi = 'hello there'    greetings = 'hello '  - *concatenate* strings    name = "kemin"    greet = hi + name    greeting = hi + " " + name  *operations on strings* >>> s = 'Python is Fun!' >>> s[1:5] 'ytho' >>> s[:5] 'Pytho' >>> s[1:] 'ython is Fun!' >>> s[:] 'Python is Fun!' elwrd helloworld  *s[i:j:k]. This gives a slice of the string s from index i to index j-1, with step size k* elloworld  >>> str1='hello'  >>> str1[:-1]     'hell'   >> "ab"+"cd"    'abcd'  >>> len('kemin')      5  >>> 3*"ab"      'ababab'   >>> 'kemin'[2]     'm'  >>> 'kemin'[1:3]     'em'  >> str1='hello'  >> str1[len(str1)]        IndexError: string index out of range   *string slicing*  can slice a string with a call such as s[i:j], which gives you a portion of string s from index i to index j-1  >>> s = 'Python is Fun!' >>> s[1:5] 'ytho'  >>> s[:] 'Python is Fun!'  ** input/output  keyword : print, input, output | input() | input  | | print() | output | >>> x=1 >>> print(x) 1 >>> x_str=str(x) >>> print("my fav num is", x, ".", "x=",x) ('my fav num is', 1, '.', 'x=', 1) >>> print("my fav num is", x_str, ".", "x=",x_str) ('my fav num is', '1', '.', 'x=', '1') >>>   >>> text= input("hello world")     hello world  *input* #+begin_src >>> text=input("enter a word") enter a word"kaiming" >>> print(text) kaiming #+end_src  ** flow control-- while and for loop |Ctrl + c| stop infinite loop| *** while #+begin_src n= 0 while n<5:      print(n)      n = n+1 #+end_src *** if  if expression:    statement(s) else:    statement(s)   if expression1:     statement(s) elif expression2:     statement(s) elif expression3:     statement(s) else:     statement(s) ***  for loop for *var* in range(4):     <expressions> - =var= iterates over values, 0,1,2,3  >> num = 10 >> for num in range(5): >>     print(num) >> print(num)  0 1 2 3 4 4  https://www.yiibai.com/python/python_if_else.html ** 'range'--  built-in function range(start, stop, stepSize)  The standard way of using the range function is to give it a number to stop at,  and range will give a sequence of values that start at 0 and go through the stop at (value-1).  For example, calling range(stop) yields the following:  >>> range(5) [0, 1, 2, 3, 4]  However, we can call range with some additional, optional parameters - a value to start at, and a step size. You can specify a start value by calling range(start, stop), like this: >>> range(2,5) [2, 3, 4]  To specify a step size, you must specify a start value - the call is range(start, stop, stepSize), like this:  >>> range(2, 10, 2) range(2, 10, 2)  which gives the sequence of values 2, 4, 6, 8  Note that these parameters - start, stop, stepSize - are the same parameters that you can use when slicing a string:  >>> s = "Hello, world!" >>> s[1:] # s[start:] ello, world! >>> s[1:10] # s[start:stop] ello, wor >>> s[1:10:3] # s[start:stop:stepSize] eow  ** iteration x = 3 ans = 0 itersLeft = x while (itersLeft != 0):     ans = ans + x     itersLeft = itersLeft – 1 print(str(x) + '*' + str(x) + ' = ' + str(ans))  # This code squares the value of x by repetitive addition.   ** guess and check - exhaustive enumeration - bisection search - newton-raphson (for root sorting)    Remember our “declarative” definition of square root of x   If we could guess possible values for square root (call it g), then can use definition to check if g*g = x   We just need a good way to generate guesses    ** bisection search ***  Exercise: guess my number  In this problem, you'll create a program that guesses a secret number!  The program works as follows:  you (the user) thinks of an integer between 0 (inclusive) and 100 (not inclusive). The computer makes guesses, and you give it input - is its guess too high or too low? Using bisection search, the computer will guess the user's secret number!  Here is a transcript of an example session:  Please think of a number between 0 and 100! Is your secret number 50? Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l Is your secret number 75? Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l Is your secret number 87? Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h Is your secret number 81? Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l Is your secret number 84? Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h Is your secret number 82? Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l Is your secret number 83? Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. c Game over. Your secret number was: 83  ** floats and fraction ** lec4 functions *** decomposition and abstraction *** introducing functions - python returns the value =none=, if no return given   + =none= represents the absence of a value  *** function call  *** functions as agruments  - arguments can take on any type,  even functions  *** Keyword Arguments    #+begin_src ''' Simple function definition, if last argument is TRUE, then print lastName, firstName; else firstName, lastName '''  def printName(firstName, lastName, reverse): if reverse: print(lastName + ‘, ‘ + firstName) else: print(firstName, lastName) #+end_src  *** inductive reasoning example  0+1+2+...+n = n(n+1)/2  mathematical induction   To prove a statement indexed on integers is true for all  values of n:   -  Prove it is true when n is smallest value (e.g. n = 0 or n = 1)   -  Then prove that if it is true for an arbitrary value of n, one can show that it must be true for n+1  *** iteration and recursion what is recursion?  - a way to design solutions to problems by  divide-and conquer or =decrease-and-conquer= - a programming technique where a function calls inteself  *iterative algorithms* - looping constructs (while and for loops) lead to =iterative= algorithms - can capture computation in a setof state variables taht update on each iteration through loop  *multiplication-iterative solution* ' a*b' = ' add =a= to itself =b= times'  a+a+...+a = a*b = a + a*(b-1)    #+begin_src def mult_iter(a,b): result = 0 while b>0:     result +=a     b-=1 return result  #+end_src  *factorial* n! = n*(n-1)*(n-2)*(n-3)*...*1 #+begin_src  def fact(n)    if  n == 1:        return 1    else:          return n*fact(n-1)  print(fact(4) #+end_src  *** Recursion on non-numerics  how to check if a string of characters is a palindrome, i.e., reads the same forwards and backwards  *** files and module - module :: a =.py= file containning a  collection Python definitions and statements  EXAMPLE MODULE - the file =circle.py= contains #+begin_src pi = 3.14159 def area(radius):     return pi*(radius**2) def circumference(radius):     return 2*pi*radius #+end_src  then we can import and use this =module=:  #+begin_src import circle pi = 3 print(pi) print(circle.pi) print(circle.area(3)) print(circle.circumference(3)) #+end_src results in the following being printed: #+begin_src 3 3.14159 28.27431 18.849539999999998 #+end_src  if we don’t want to refer to =functions= and =variables= by their =module=, and the names don’t collide with other bindings, then we can use: #+begin_src from circle import * print(pi) print(area(3)) #+end_src  this has the effect of creating bindings within the current scope for all objects defined within circle  Note:  - statements within a module are executed only the first time a module is imported  *** specification  ** lec5: Structured Types  *** Tuples--() e.g. (1,2,3) - closed in =()=, separated with =,=  Feature: - different types, e.g. (1, "a") - iterable - conveniently used to swap variable values - used to return more than one value from a function #+begin_src def quotient_and_remainder(x, y): q = x//y r = x%y return (q, r) (quot, rem) = quotient_and_remainder(4,5) #+end_src  *** range - range is a special procedure   +  returns something that behaves like a tuple!   +  doesn’t generate the elements at once, rather it generates the first element, and provides an iteration method by which subsequent elements can be generated |range(5)| equivalent to tuple[0,1,2,3,4]   |range(2,6)|equivalent to tuple[2,3,4,5] |range(5,2,-1)|equivalent to tuple[5,4,3] - when use range in a for loop, what the loop variable iterates over behaves like a list! for var in range(5):       <expressions> behind the scenes, gets converted to something that will behave like: for var in (0,1,2,3,4):       <expressions> *** lists -- [] - denoted by square brackets, [] - ordered sequence of information, acceessible by index - mutable - iterable  e.g. [1,2,3,4]  L=[1,2,3,4] L[1] L[1] = 3  total = 0 for i in L:    total += i print(total)  >> x = [1, 2, [3, 'John', 4], 'Hi']  >> 2 in x  True  >> 3 in x False  *list operations*  To *add* elements to =end= of list with =L.append(element)=   *mutates* the list with *L.extend(some list)*  >> L = [1,2] >> L.append(3)  # L is now [1,2,3] >> L2 = [3,4,5] >> L3 = [6,2] >> L4 = L2+L3 >> L2.extend([2,4])  To *delete*  element at a *specific index*  >> del(L[index]) >> del(L[1])  *list* <-----> string  - convert string to list with *list(s)*, returns a list with every character from s an element in L -  can use s.split(), to split a string on a character parameter, splits on spaces if called without a parameter -  use ''.join(L) to turn a list of characters into a string, can give a character in quotes to add char between every element s = "I <3 cs" # s is a string list(s)  # returns ['I',' ','<','3',' ','c','s'] s.split('<') # returns ['I ', '3 cs'] L = ['a', 'b', 'c'] # L is a list ''.join(L) #returns "abc" '_'.join(L) # returns "a_b_c"  *sort list* - =sort()= and =sorted()= - =reverse()=  L=[9,6,0,3]  |sorted(L)|#returns sorted list, does not mutate L |L.sort()|mutates L=[0,3,6,9] |L.reverse()|mutates L=[9,6,3,0]  *** TODO Mutation, Aliasing, Cloning  ***  Functions as Objects   **** exercise apply to each 1  Here is the code for a function applyToEach:  def applyToEach(L, f):     for i in range(len(L)):         L[i] = f(L[i]) # ?? f(L[i])  Assume that  testList = [1, -4, 8, -9]  For each of the following questions (which you may assume is evaluated independently of the previous questions, so that testList has the value indicated above), provide an expression using applyToEach, so that after evaluation testList has the indicated value. You may need to write a simple procedure in each question to help with this process.  Example Question:  >>> print(testList) [5, -20, 40, -45]  Solution to Example Question  def timesFive(a):     return a * 5  applyToEach(testList, timesFive)   ** Lec6: Dictionaries --{}, functions as objects | key(label) | value | | "Ana"      | 'A'   |   >>> grades = {'Ana':'B', 'John':'A'}    >>> grades['Ana']  'B' *** problem set 3 ** lec7: good programming practices   *** testing and debugging *** exceptions and assertions *** problem set 4  ** lec8: exception and assertion  *** exceptions   ** ref  MITx: 6.00.1x Introduction to Computer Science and Programming Using Python  * Video  tutorials-giraffe academy package: Pycharm https://www.youtube.com/watch?v=rfscVS0vtbw  * [[file:idle.org][IDLE]] Help * [[file:crawler.org][crawler]]  * TODO  download all pdf links in a webpage  It would have been tiring to  download each pdf manually.   steps: - extract links - download  ** code -- spider.py import requests  from bs4 import BeautifulSoup   '''  URL of the archive web-page which provides link to  all pdf lectures. In this example, we first crawl the webpage to extract  all the links and then download pdfs.  '''  # specify the URL of the archive archive_url = "https://qmplus.qmul.ac.uk/course/view.php?id=2775"  def get_pdf_links():           # create response object      r = requests.get(archive_url)           # create beautiful-soup object      soup = BeautifulSoup(r.content,'html5lib')           # find all links on web-page      links = soup.findAll('a')       # filter the link sending with .pdf     pdf_links = [archive_url + link['href'] for link in links if link['href'].endswith('pdf')]       return pdf_links    def download_pdf_series(pdf_links):       for link in pdf_links:           '''iterate through all links in pdf_links          and download them one by one'''                  # obtain filename by splitting url and getting          # last string          file_name = link.split('/')[-1]           print ("Downloading file:%s"  %file_name )                  # create response object          r = requests.get(link, stream = True)                   # download started          with open(file_name, 'wb') as f:              for chunk in r.iter_content(chunk_size = 1024*1024):                  if chunk:                      f.write(chunk)                   print ("%s downloaded!\n" %file_name )      print ("All pdfs downloaded!")     return   if __name__ == "__main__":       # getting all pdf links      pdf_links = get_pdf_links()       # download all pdfs      download_pdf_series(pdf_links)  ** bug ========== RESTART: /home/kaiming/Documents/Python/DEN331/spider.py ========== Traceback (most recent call last):   File "/home/kaiming/Documents/Python/DEN331/spider.py", line 63, in <module>     pdf_links = get_pdf_links()   File "/home/kaiming/Documents/Python/DEN331/spider.py", line 27, in get_pdf_links     pdf_links = [archive_url + link['href'] for link in links if link['href'].endswith('pdf')]   File "/home/kaiming/Documents/Python/DEN331/spider.py", line 27, in <listcomp>     pdf_links = [archive_url + link['href'] for link in links if link['href'].endswith('pdf')]   File "/usr/lib/python3/dist-packages/bs4/element.py", line 958, in __getitem__     return self.attrs[key] KeyError: 'href' >>>   * python4informatics: exploring information  Let Google do the hard work  Coursera  edX  python 3 https://www.py4e.com/ python 2 http://www.py4inf.com/  * FAQ ** online help - useful forum: [[http://stackoverflow.com/][Stack Overlow]], [[http://reddit.com/r/learnprogramming/][Learn programming subreddit]] When asking programming questions, remember to do the following:      Explain what you are trying to do, not just what you did. This lets your helper know if you are on the wrong track.      Specify the point at which the error happens. Does it occur at the very start of the program or only after you do a certain action?      Copy and paste the entire error message and your code to http://pastebin.com/ or http://gist.github.com/.      These websites make it easy to share large amounts of code with people over the Web, without the risk of losing any text formatting. You can then put the URL of the posted code in your email or forum post. For example, here some pieces of code I’ve posted: http://pastebin.com/SzP2DbFx/ and https://gist.github.com/asweigart/6912168/.      Explain what you’ve already tried to do to solve your problem. This tells people you’ve already put in some work to figure things out on your own.      List the version of Python you’re using. (There are some key differences between version 2 Python interpreters and version 3 Python interpreters.) Also, say which operating system and version you’re running.      If the error came up after you made a change to your code, explain exactly what you changed.      Say whether you’re able to reproduce the error every time you run the program or whether it happens only after you perform certain actions. Explain what those actions are, if so.  ** read content of a text file in python capability: reading =text= from a text file  1. open the IDLE text editor idle3
2. declare a *string* variable that holds *the path to the text file*, =test.txt=
  >>> strPath="/home/kaiming/Documents/Python/text/text.dat"
3. open the file using the =open()= function
   >>> f=open(strPath)
4. Read the contents of the file using the =read()= function
  >>>  StrText=f.read()
5. Print out the contents of the file
 >>> print(strText)

refer to [[https://docs.python.org/2/tutorial/inputoutput.html][input]] and out

** How to read and write multiple files?
Goal:

I want to write a program for this:
In a folder I have =n= number of files;
 first read one file and perform some operation then store result in a separate file.
Then read 2nd file, perform operation again and save result in new 2nd file.
Do the same procedure for n number of files.
The program reads all files one by one and
stores results of each file separately.



solution:

#+BEGIN_SRC Python
import sys       #import 'sys' module
#argv is your commandline arguments, argv[0] is your program name, so skip it

for n in sys.argv[1:]:
   print(n) #print out the filename we are currently processing
   input=open(n,"r")
   output=open(n,+".out","w")
   # do some processing
   input.close()
   output.close()
#+END_SRC

- sys :: system-specific parameters and functions
- module :: a file contains Python code

Before you use the functions in a module, you must import the module with an =import= statement

why python module?

Python module is used to group related functions, classes, and variables
for better code management and avoiding name clash

https://stackoverflow.com/questions/208120/how-to-read-and-write-multiple-files

* Glossary
- argument :: a value that is passed to a function
  e.g. function-name(argument)

* notes - Python for Non-Programmers --book
[[https://automatetheboringstuff.com/][automate the boring stuff with python-practical programming for total beginners]]
- The programs in this book are written to run on *Python 3*

* Function
*built-in funcions* include the =print()=, =input()=, =len()=

To define a function

>> def function_name()
** 函数的参数
除了正常定义的必选参数外,还可以使用默认参数、可变参数和关键字参数
*** 位置参数

我们先写一个计算x2的函数:

def power(x):
    return x * x

对于power(x)函数,参数x就是一个位置参数。

当我们调用power函数时,必须传入有且仅有的一个参数x:

>>> power(5)
25
>>> power(15)
225

现在,如果我们要计算x3怎么办?可以再定义一个power3函数,但是如果要计算x4、x5……怎么办?我们不可能定义无限多个函数。

你也许想到了,可以把power(x)修改为power(x, n),用来计算xn

def power(x, n):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s

对于这个修改后的power(x, n)函数,可以计算任意n次方:

>>> power(5, 2)
25
>>> power(5, 3)
125

修改后的power(x, n)函数有两个参数:x和n,这两个参数都是位置参数,调用函数时,传入的两个值按照位置顺序依次赋给参数x和n


** 递归函数
如果一个函数在内部调用自身,这个函数就是递归函数。

举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact(n)表示,可以看出:

fact(n) = n! = 1 x 2 x 3 x ... x (n-1) x n = (n-1)! x n = fact(n-1) x n

所以,fact(n)可以表示为n x fact(n-1),只有n=1时需要特殊处理。

于是,fact(n)用递归的方式写出来就是:
#+BEGIN_SRC Python
def fact(n):
    if n==1:
        return 1
    return n * fact(n - 1)
#+END_SRC
如果我们计算fact(5),可以根据函数定义看到计算过程如下:

===> fact(5)
===> 5 * fact(4)
===> 5 * (4 * fact(3))
===> 5 * (4 * (3 * fact(2)))
===> 5 * (4 * (3 * (2 * fact(1))))
===> 5 * (4 * (3 * (2 * 1)))
===> 5 * (4 * (3 * 2))
===> 5 * (4 * 6)
===> 5 * 24
===> 120

** range()
Syntax:
 
  range(start, end, step size)

e.g. ~range(0, 10, 2)~ will count from 0 to 8 by intervals of 2
#+BEGIN_SRC Python
>>> for i in range(0, 10, 2):
    print(i)

    
0
2
4
6
8
#+END_SRC
* lists and tuples
- list :: a value that contains multiple values in an ordered suequence
  + typed with []
 
   e.g. =['cat','bat','rat', 'element']=

What is list used for?

- tuple :: almost identical to the *list* data type, but immutable
  + typed with (),

- list value :: refers to the list itself, not the values inside the list value

- iterms :: values inside the list


*To insert a iterm after the item 1 in a list:*
    
    >>> classmates=['Michael', 'Bob']
    >>> classmates.insert=[1,'jack']
   ['Michael','Jack', 'Bob']

* courses
edx [[https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/references/][Intro to computer sicence and programming]]--Eric Grimson, MIT
* References
Introduction to Computation and Programming Using Python, Second Edition
https://docs.python.org/2/tutorial/index.html

https://www.python.org/about/gettingstarted/

** video
tutorials-giraffe academy
** Documentation
•Official Python 3 Documentation - "official"/technical explanation of what a particular function/operator does, examples of correct syntax, what the various libraries are, etc.

Textbooks/Tutorials
•Dive Into Python - another survey of Python syntax, datatypes, etc.
•Think Python by Allen Downey - a good general overview of the Python language. Includes exercises.
•The Official Python Tutorial - self-explanatory
•Learn Python the Hard Way - (note: for Python 2) another free online text
•Reserved Keywords in Python - don't use these as variable names
•PEP 8 - Style Guide for Python Code - learn what is good and bad style in Python
•CheckIO - learn Python by exploring a game world
•Invent with Python - develop your Python skills by making games or hacking ciphers
•Codecademy - (note: for Python 2) learn Python by building web apps and manipulating data; interactive tutorial sequence
•Python Tutor - interactive tutorial sequence of exercises
•Blog with tutorials - created by one of our community TAs

** Debugging
•Python Tutor - an excellent way to actually visualize how the interpreter actually reads and executes your code
•DiffChecker - compares two sets of text and shows you which lines are different
•Debugging in Python - steps you can take to try to debug your program

Software
•Python Tools for Visual Studio - Visual Studio plug-in enabling Python programming

Other Q&A


** More practice problems
•Python Challenge - a series of puzzles you can try to test your Python abilities
•Project Euler - additional programming challenges you can try once your Python knowledge becomes stronger; problems are sorted by increasing difficulty
•Coding Bat - problems you can solve within an online interpreter
•Codewars - improve your skills by training on real code challenges

** non-programmer
Interactive Courses
CheckiO is a gamified website containing programming tasks that can be solved in either Python 2 or 3.

Python on Codecademy (Python 2)
笨方法学 Python
http://learnpythonthehardway.org/book/

「廖雪峰的 Python 2.7 教程」:Home - 廖雪峰的官方网站Python 中文教程的翘楚,专为刚刚步入程序世界的小白打造。
The Hitchhiker’s Guide to Python


链接:https://www.zhihu.com/question/29138020/answer/72193349


** chinese
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431658427513eef3d9dd9f7c48599116735806328e81000

posted @   kaiming_ai  阅读(152)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
历史上的今天:
2020-01-14 test
点击右上角即可分享
微信分享提示