欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python基础(1)

程序员文章站 2023-11-19 12:38:16
1、"hello world"     --print....      &...
1、"hello world"
    --print....
        print 'hello world'   #(2.x)
        print ('hello world')    #(3.x)
    --stdout
        import sys
        sys.stdout.write('hello world!\n')
2、general rules
    --use '#' to start a comment
    --one statement per line,unless semicolon used more than one statement per line
    --code blocks delimited by indentation
    --use '\' to break up long lines
3、simple statements fit on one line,no code block following
4、compound statements have code block
    --fist line called a header
    --indented sub-block of code called a suite
    --header plus ':' followed by indented suite
    --indentation allows any whiltespace
        ==use spaces not tabs
        ==use same # of spaces for each line
5、variables
    --dynamic typing....variables not declared
        ==type inferred on assignment
        ==var = expression
    --flexible assignment syntax
        ==var1 = var2 = xepression
        ==var1, var2 = exp1, exp2        #it equals to var1=exp1;var2=exp2
6、assignment
    --primary assignment operator:=
    --augmented assignment:+=,*=,.....
    --++,-- are not permitted
7、identifier rules (similar to other language)
    --fist character must be alphabetic
    --any others can be alphanumeric
    --treat underscores (_) as alphabetic
    --case-sensitive
8、python gotchas
    --special names begin/end with underscores
        ==avoid using until you know what...
            **they are used for
            **you are doing
    --do not use built-in function/data names
Python基础(1)
   
   Python基础(1)
     --reserved words,or keywords,can't be used
   Python基础(1)
9、function
    --declared using def  keyword
   Python基础(1)
    --declarations:
        ==header line and at least one for body
        ==can have any number of arguments
    --if no value explicitly returned...
        ==python returns none
            **none: python's null or void
            **none: constant boolean false
10、importing & modules
    --allows use of outside code
    --what are modules
        ==self-contained python code
        ==bring in new functionality/objects
        ==import only features that you need
    --two different ways to access module attributes
        == use module elements with name (i preferred)
Python基础(1)
              
        == can import inpidual elements into "namespace"              
              Python基础(1)
11、keyboard input
    -- get keyboard input from user
        == use python's raw_input() function
        == renamed to input() in python 3.x
    -- syntax:
        
    -- example:
   
Python基础(1)