Python basics and python Basics

Source: Internet
Author: User

Python basics and python Basics

  • List (similar to array)
    Note:Any types of elements can exist in the same list. 
    table = ["1","2","3"];

    • Access the last element:table[-1], Second to lasttable[-2]And so on.
    • Add Element
      Add (append) at the end of the list)
      table.append(var); 
      Insert an element anywhere)
      table.insert(index,var);// Add the element var to make its subscript index;
    • Delete an element (del)
    • Sort
    • List reversal
      Reverse ()
      table.reverse();
    • List Length
      Len ()
      len(table);
    • Convert to list
      List ()
      For example, convert a string of numbers into a list:
      n = list(range(1,4,2));// Range (, 2) indicates adding 2 (1 by default) to the number string from 1 until the number is greater than or equal to 4 (not including 4)
    • List Parsing
      List parsing generates a desired list in one sentence:
      squares = [value**2 for value in range(1,11)] 
      The expression is value ** 2, and the for loop provides a value for the expression. The element of the entire list is all values ** 2. (** Multiplication operator in python)
    • List slice
      List slicing is actually part of the screenshot list to make it a new list.
      n = [1,2,3,4,5]; 
      n = n[1:3]; 
      It means that the part of list n starting from subscript 1 to subscript 2 is assigned to n as a new list.
    • Copy list
      n = [1,2,3,4]; 
      m = n[:]; 
      This is the correct copy list, that is, take n of the entire slice, and
      n = [1,2,3,4]; 
      m=n; 
      Yes. Here m and n pointSame list, Does not achieve the purpose of replication (note that it is different from other languages)
    • Check whether the list is empty:
      n = []; 
      if(n);// Returns true if the list contains at least one element in python.
    • Use the set function set ()
      The set function set () removes the same elements from the list:
      a = [1,2,3,1]; 
      a = set(a);// Remove the same value from the set to ensure that the elements in the set are different
  • Tuples (unchangeable List)
    yz = (200,50); 
    That is, the yz tuples have two elements: 200 and 50;
    An error will be reported when you modify the value of the tuples;
    You can use tuples to store an unchangeable set of values.

    • Traversal tuples
      Same as traversing the list;
  • Loop

    • for name in array: 
      print(name); 
      print(name.title()); 
      Note: python distinguishes code blocks by indentation, rather than braces {} In general languages. Therefore, the two prints above are in a for loop. Indentation in python is very strict. indentation that should not be indented will also generate syntax errors!
    • While
      while a>=5: 
       print(); 
       a--;
  • Condition Statement
    • If-elif-else
      if car =="jj": 
      print(); 
      elif car =="dd": 
      print(); 
      else: 
      print();
    • And/or
    • In/not in
      Determine whether the element is (NO) in the list:
      >>>n=[1,2,4,8]; 
      >>>"88" in n: 
      >>>true;
  • Dictionary
    Dictionary isA series of key-value pairsThe value can correspond to numbers, strings, lists, dictionaries, and other python objects.
    For example:
    • Dictionary definition:alien = {'color':'green','points':5};
    • Access dictionary elements:alien['color']; 
  • Input
    • input() 
      • message = input(some input information); 
        The input command parses the user's input into a string and stores the message. The parameter is the information displayed to the user. End with user input press ENTER
  • Function
    • Definition:
      def a(b="dd"): 
      print(); 
      return b; 
      Def indicates that this is a function definition. a is the function name, B is the parameter, and has a default value, which is the return value. All the subsequent condentions constitute the function body.
      Note: 
      1. The parameter list of the python function must first list parameters without default values, and then LIST Parameters with default values. 
      2. When the list parameter is used, the modification to the list in the function is permanent. Other parameters are not necessarily. If you want to pass the list parameter without changing the original list, you can pass the list copy as the parameter, that is, the slicea[:];
    • Call:
      a("ff");
    • Real parameter type:
    • Passing any number of real parameters
      Sometimes we don't know how many arguments a function needs to accept. in python, the function is allowed to accept any number of arguments:
    • Store functions in files and import them
      You can store functions in a file and import the file when using the function.
      For example, the function. py file contains a test () function:
  • Mingli

    If you are interested, you can go to the ball ~ Sharing learning technologies: 2042849237

    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.