Python Base One,pythonbaseone

來源:互聯網
上載者:User

Python Base One,pythonbaseone

//this is my first day to study python, in order to review, every day i will make notes (2016/7/31)

1. In python , there are many bulit-in funcation. you can use follow to find out hou many built-in funcation:

    dir(__builtins__)

    if you want get BIF detail, you can use follow code:

    help(……)    such as help(input),int()

 

2. In python, you should be care to indent, if not , it will appear a lots error.

    such as follow ,because indent error ,it will appear an error:

    temp = input('input a num you want:')
    guess = int(temp)
    if guess == 8:
    print('you are right')
    else:
           print('you are wrong')

 

3. In python , when you define variable ,uppercase and lowercase is different.

    such as:

    temp = 'roy'

    Temp = 'wyg'

    temp and Temp is not same variable

 

4. In python , if you want to use ' in a string ,you can user escape charcters : \

    such as:

    print('Let\'s go!')

 

5. In python , origin string is userful , maybe the follow result is not  you except

    such as :

    str = 'c:\now\data'

    print(str)

    result is :

    c:

    ow\data

    you can solve this problem by str = r'c:\now\data' ,this code is equal to str = 'c:\\now\\data'

    when you print(str), the result will be c:\now\data

 

6. In python , if a string , you want to change row ,you can user '''  ,if you not user ''' and change row ,it will appear an error

    such as:

    str = '''this

    is

    me'''

    print(str)

    the result is :

    this

    is 

    me 

 

7. In python , import module , such as if you want  a rand num , range is 1-10 and type is int ,how to achieve it

   import random

   randnum = random.randint(1,10)

 

//this is second day to study python(2016/8/1)

8. In python , we can see 'e' as 10

    such as:

    1.5e4 == 15000.0

 

9. In python , type conversion is userful , the follow is base conversion

    float or string -> int

    such as :

    a = '26'    b = int(a)   print(b) -> 26

    a = 'qq'    b = int(a)   error

    a = 3.14  b = int(a)    print(b) -> 3

    ----------------------------------------

    int or string -> float

    such as:

    a = '26'  b = float(b)  print(b) -> 26.0

    a = 'qq'  b = float(b)  error

    a = 26   b = float(b)   print(b) -> 26.0

    ------------------------------------------

    int or float -> str

    such as:

    a = 26  b = str(a)  print(b) -> '26'

    a = 3.14  b = str(a) print(b) -> '3.14'

    a = 5e19 b = str(a) print(b) -> '5e+19'

    sometimes, we need to be care to str,

    such as:

    str = 'I like apple'

    print(str) -> 'I like apple'

    but if it is c = str(5e19)  ,it will appear an error ,because str is BIF ,we define str again , it will have new means, so it will have error

 

10. In python ,we can get the type of variable 

      such as:

      a = 'qq'  

      type(a) -> <class 'str' at 0x----->

      a = 3.0

      type(a) -> <class 'float' at 0x--->

      a = True

      type(a) -> <class 'bool' at 0x--->

      we recommand follow:

      a = 'roy'

      isinstance(a, str)  -> True

      isinstance(a, int)  -> False

      but sometimes you need to know it maybe account an error:

      TypeError: isinstance() arg 2 must be a type or tuple of types

      the reason is may you define str before, In python ,you can change built-in funcation, so when you define variable ,you should try to avoid user special    chararctes. such as try not to use BIF.

 

11. In python , arithmetic operators has +   -   *   /   **   //   %

      a += 4 <==> a = a + 4

      a = 2

      b = 3

      a / b -> 0.6666666666666666
      a // b -> 0

      b // a -> 1

      a % b -> 2

      b % a -> 1

      a * b -> 8

      b ** a -> 9

 

12. In python , logical operators includes and , or , not

      such as:

      not Ture

      False

 

13. In python , if and else how to use:

     score = int(input('please input score:'))

     if 90<= score <= 100:

          print('A')

     elif 80<= score < 90:

          print('B')

     elif 60<= score < 80:

          print('C')

     else:

          print('D')

 

14.  trinocular operator

      small = x if x < y else y

      ->

      x, y = 4, 5

      if x < y:

          small = x

      else:

          small = y

 

15. assert ,when condication is false , it will have assertionerror

      such as:

      assert 3 > 4

      >>> assert 3>4
      Traceback (most recent call last):
      File "<pyshell#1>", line 1, in <module>
      assert 3>4
      AssertionError

 

//third day study python (2016/8/2)

16. In python , how to achieve 'for' loop:

      for target in expression:

            loop body

       such as:

       string = 'roy'

       for i  in  string:

            print(i)

       ->

        r

        o

        y

        students = ['wyg' , 'roy]

        for each in students:

             print(each,len(each))

 

17. In python , range() is often combined with for loop , what's range()

      range([start,] stop [, step = 1])  // three para

      [] represent select

      step = 1 represent by default step is 1

      so it is from start to stop number list

      range(3) -> range(0,3)

      list(range(4)) -> [0, 1, 2, 3]

      for i in range(2,5):

            print(i)

       result is :2  3   4

       for i in range(1,10,4):

            print(i)

        result is :1 5  9

  

18. In python , there are also have break and continue

      continue skip current loop

      break end loop

 

19.  In python , there are no array ,but have list and more power

       such as:

       mixType = [1, 'roy', 3.14, [5, 6]]

       we can also create an empty list:

       empty = []

       how to add content to list:

      empty.appen(3)  -> 3

      or

      empty.extend([3, 6, 9]) -> 3  3  6  9

      or

      empty.insert(0, 'aaa') -> 3 'aaa  3  6  9

 

 





     

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.