Think of Python notes like a computer scientist

Source: Internet
Author: User
Tags function definition logical operators

[TOC]the way of #1 procedure
# #1.1 Python programming language -There are two programs that enable high-level languages to be converted to low-level languages, such as literal translators and compilers.-There are two ways to use Python's interpreter: Shell mode and script mode.# #1.8 formal language and natural language-Natural language is the language spoken by people. -Formal language is the language that people design for a particular application*<b> programming language is a formal language designed to render computations, </b>* syntax rules are divided into two categories, namely, tags and structures. Markup is the basic component of a programming language, just like words, numbers, and chemical elements; structure is the arrangement of marks. > When you read a statement in an English sentence or a formal language, you must understand what the structure of the sentence is, and this process is called analysis.
    * * Ambiguity: * * Natural language is full of ambiguity
* * Redundant Sex: * * Natural language in order to compensate for ambiguity and reduce misunderstanding, the use of a lot of fat words. * * Literal: * * Natural language is full of idioms and metaphors.
#2 variables, expressions, and statements (print) # #2.1 values and Patterns-2 is the value, "Chess"is a string.' python>>> print ' chess ' chess>>> type (3.2) <type ' float ' > ' # #2.3 variable name and keyword-* * variable name * * can be any length , you can also include both letters and numbers. But it has to start with a letter python has ' 31 ' keyword <b><font color=blue>|and|del|from|not|while||:----|:---|:----|:---|:----||as|elif|global|or|with|
|assert|else|if|pass|yield|
|break|except|import|print|class|
|exec|in|raise|continue|finally|
|is|return|def|for|lambda|
|try|</font></b># #2.5 expression evaluation It is confusing to find the value of an expression and print a number that is not exactly equal to ' python>>> message= ' Oh, shit ' >>> message ' Oh, Shit ' >>> print Messageoh, shit>>> ' # #2.9 input python has two built-in functions to get input data from the keyboard: ' ' python>>> n=raw_ Input (' Please enter your name: ') Please enter your name:lifeng>>> print nlifeng>>> n=input (' Please enter a number expression: ') Enter a numeric expression:3*9> >> print n27>>> n=raw_input (' Please enter an expression: ') Enter an expression:3*8>>> print n3*8 ' input is an expression #3 function # # 3.1 Definition and usage of functions-in the context of program design, a function is a named sequence of statements used to perform the desired operation. In Python, the syntax for the function definition is: "' Pythondef name (List of PARAMETERS): statements
"# #3.3 parameters, arguments, and import statements-another built-in function that accepts more than one argument is Max ' python>>> Max (7,11,4,23,93) 93"- Import keyword We can use the import statement to import the functions we define in the script into the literal translation program. Let us first assume that the Print_twice function is defined in chap03.py ' python>>> from chap03 import *>>> print_twice (' Lifeng ')
Lifeng lifeng>>> Print_twice (5)5 5>>>
' * * * #4 conditional expression # #4.2 Boolean with Expression Boolean only two true and false ' ' python>>> type (true) <type ' bool ' >>>> type ( False) <type ' bool ' >>>> type (TRUE) Traceback (most recent call last):  File "<stdin>", line 1, in <module>nameerror:name ' true ' is not defined ' # #4.3 logical operators-logical operators have three and, or, not# #4.4 items Execute if statement ' pythonif x > 0:& nbsp   print "X is positive" "# #4.10 type conversion" ' python>>> int ( -2.33) -2>>> int ( -2.5) -2>>> Int (- 2.6) -2>>> Int (3.6) 3>>> str (+) ' + ' >>> bool (1) true>>> bool ("!no") true>> > bool ("") False ' ***# 5 multi-effect function # # 5.1 Returns a function that has a return value, which we call a multi-effect function (fruitful functions) # #5.5 function of the form "' Python>>> def FUNCCCC ():       Return "This is a function" ... >>> type (FUNCCCC) <type ' function ' > >>>  ' # #5.73 quote String ' python>>> print ' ' Mariy said: "You're So Fool", Maria replyed: "You know ' function ' ' ' ' Mariy said: "You're So Fool", Maria ReplyeD: "You know ' function '" >>> print "' Mariy said:" Your is so Fool ",... Maria replyed:" You ... know ' function ' "" Mariy said: "You're So Fool", Maria replyed: "Youknow ' function '" >>> print "" "Mary You said:" Your Nothing ", Maria Rep Lyed: ... "So you Know". "" " Mary said: "You are Nothing", Maria replyed: "So you Know". "The three-quote string, where you can enter double quotes and single quotes.
# #5.8 use Doctest for unit testing * * * #6 Repeat # #6.1 multiple assignments "Pythona=5b=a #a和b现在相等a =3 #a和b现在不相等了" # #6.2 Update Variable # #6.3 while statement ' Pythondef Cou Tdown (n): While n > 0:
Print n
n = n-1
Print "blastoff!" "# #6.9 Encapsulation and generalization encapsulation is the process of wrapping a piece of program code into a function, allowing you to take advantage of all the benefits of a function. Generalization is the addition of parameters. #7. String # #7.1 composite data patterns so far we've seen five types: <font color=blue> ' int`,`float`,`BOOL`,`Nonetype`, and`Str' </font>."' Python>>> fruit=" banana ">>> letter=fruit[1]>>> print lettera>>> len (fruit) 6 ' "# #7.3 visit and for Loop ' Pythonindex=0while index < Len (fruit): letter = Fruit[index]
Print Letter
Index + = 1
"# #7.4 string Slice" ' >>> s= "Peter Jackson, all you do is great" >>> print s[0:5]peter>>> print s[6 : 13]jackson "If you omit the first index, the slice will start at the beginning of the string, and if you omit the second index, the slice will end with the end of the string. # #7.5 A string comparison operator can be used on a string, in order to confirm that two strings are equal. "Pythonif Word = =" Banana ": print" Yes, we have no bananas! "

If Word < "banana": print "Your word," + word + ", comes before banana."
Elif Word > "banana": print "Your word," + word + ", comes after Banana"
else:    Print "Yes, we have no Bananas" "# #7.7 in operator ' python>>> ' P ' in ' person ' true>>> ' a ' In ' person ' false>>> ' er ' in ' person ' True ' ' # #7.10 optional parameter ' Pythondef find (String, ch, start=0) ' # #7.11 String module ' python>>> import string>>> dir (string) [' Formatter ', ' Template ', ' _templatemetaclass ', ' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' _float ', ' _idmap ', ' _IDMAPL ', ' _int ', ' _long ', ' _ Multimap ', ' _re ', ' ascii_letters ', ' ascii_lowercase ', ' ascii_uppercase ', ' atof ', ' atof_error ', ' atoi ', ' atoi_error ', ' Atol ', ' atol_error ', ' capitalize ', ' capwords ', ' center ', ' count ', ' digits ', ' expandtabs ', ' find ', ' hexdigits ', ' Index ', ' Index_error ', ' join ', ' joinfields ', ' letters ', ' ljust ', ' lower ', ' lowercase ', ' lstrip ', ' Maketrans ', ' octdigits ', ' Printable ', ' punctuation ', ' replace ', ' rfind ', ' rindex ', ' rjust ', ' rsplit ', ' Rstrip ', ' Split ', ' splitfields ', ' strip ', ' s Wapcase ', ' translate ', ' upper ', ' uppercase ', ' whitespace ', 'Zfill ']>>> type (string.digits) <type ' str ' >>>> type (string.find) <type ' function ' >> >> Print string.digits0123456789>>> print String.find.__doc__find (s, Sub [, Start [, end]]), in
Return the lowest index in s where substring sub are found, such that sub is contained within S[start,end]. Optional arguments start and end is interpreted as in slice notation.
Return-1 on failure.
>>> string.find ("Banana", "NA") 2>>> string.find ("Bob", "B", up)-1 "# #7.12 Characters of the category ' ' Python>> > Print string.lowercaseabcdefghijklmnopqrstuvwxyz>>> Print string.uppercaseabcdefghijklmnopqrstuvwxyz >>> Print String.whitespace

>>> ' String.whitespace contains spaces, tab (\ t), new lines (\ n) # #7.13 The format of the string ' >>> ' his name is%s. '% ' Arthur ' Name is Arthur. ' >>> name= "Lifeng" >>> age=30>>> "I am%s and I am%d years old"% (name,age) ' I am lifeng and I A M-Years old ' * * * #9 tuple (tuple) # #9.1 variability and tuple strings are immutable, but the list is mutable. Python also has a form named tuple. It is very similar to a list, except that it is immutable. Syntactically, a tuple is a series of comma-separated values, and a tuple of an element must be preceded by a comma, otherwise it will be treated as a string: "'>>> tuple= ' A ', ' B ', ' C ', ' d ', ' E '
>>> type (tuple) <type ' tuple ' >>>> t1= (' A ', ' B ') >>> t2= (' A ',) >>> type (t2) <type ' tuple ' >>>> t3= (' a ') >>> type (T3) <type ' str ' >>>> tuple[0] ' a ' >> > Tuple[1:3] (' B ', ' C ') ' # #9.2 tuple assigned value Exchange, traditional notation "' >>> a=10>>> b=20>>> temp=a>> > a=b>>> b=temp>>> a20>>> B10 "
The notation of the tuple designation ' >>> a=10>>> b=20>>> a,b=b,a>>> a20>>> B10 ' ' Three-value interchange: ' > >> a,b,c=10,20,30>>> a,b,c=c,a,b>>> a30>>> b10>>> C20 "' # #9.4 Random numbers that are mathematically not really random. But it can meet our daily requirements: ' >>> import random>>> for I in range: ... x = Random.random () ... print x ... 0 .6847199296120.04643629298740.3953767674280.8006374569940.990663611060.7657169071620.9898605846570.4133980957960.93516143 38050.0607366842634 "* * * #10. Dictionary * * * #11. Files and exceptions when a program is running, its data is in memory, and when the program ends, the data is lost, and it should be put in a file to be stored. ">>> F=open (" Test.dat "," W ") >>> print F<open file ' Test.dat ', Mode ' W ' at 0x7f706b603d20>>& Gt;> ' Write file ' >>> f.write ("Now Learning python") >>> F.write ("Now stop Writing Python") >> > F.close () "Read the file. F.read () reads the entire contents of the file "' >>> f=open (" Test.dat "," R ") >>> Text = F.read () >>> Print text
New Line
>>> "



Think of Python notes like a computer scientist

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.