Python built-in function usage

Source: Internet
Author: User

1.Python raw_input () function

Function: Raw_input () is used to get input from the console, treat all inputs as strings, and return string types.

Attention:

Both the input () and raw_input () functions can receive strings, but raw_input () directly reads the console input (any type of input it can receive). For input (), it wants to be able to read a valid Python expression, that is, you must enclose it in quotation marks when you enter a string, or it will throw a syntaxerror.

Unless there is a special need for input (), it is generally recommended to use raw_input () to interact with the user.

The Python3 input () is the STR type that is received by default.

  View Code

2.Python Range () function

Grammar:

Range (Start,stop[,step])

Parameter description:

    • Start: Count starts from start. The default is starting from 0. For example, range (5) is equivalent to range (0, 5);
    • Stop: Count to stop ending, but not including stop. For example: Range (0, 5) is [0, 1, 2, 3, 4] No 5
    • Step: Step, Default is 1. Example: Range (0, 5) is equivalent to range (0, 5, 1)

Example:

>>> Range (1, 2, 3, 4, 5, 6, 7, 8, 9]>>> range (1,11) [1, 2, 3, 4, 5, 6, 7, 8, 9]>>> Range (0,20,5)   # step is 5[0, 5, ten]>>> Range ( 0,14,33, 6, 9, >>>Range (0,-10,-2)    # negative numbers [0,-2, -4,-6,-8  ]>>> Range (0) []>>> Range (1, 0) []>>> Range (5,1) []
View Code

The following is the use of range in for, looping out each letter of dictionary

>>> x='dictionary' for in range     (len (x)): ... Print (X[i]) ... dictionary
View Code

Practice Example 1:

title: There are four numbers: 1, 2, 3, 4, how many different and no repetition of the number of three digits? What's the number?

Program Analysis: can be filled in hundreds, 10 digits, single digit numbers are 1, 2, 3, 4. Make all the permutations and then remove the permutations that do not meet the criteria.

#!/usr/bin/env python#-*-coding:utf-8-*-#Example 1 forIinchRange (1,5):     forJinchRange (1,5):         forKinchRange (1,5):            if(I!=J) and(i!=k) and(j!=k):Printi,j,k#Output:1 2 31 2 41 3 21 3 41 4 21 4 32 1 32 1 42 3 12 3 42 4 12 4 33 1 23 1 43 2 13 2 43 4 13 4 24 1 24 1 34 2 14 2 34 3 14 3 2
View Code

Pop () method in 3.List: the pop () function removes an element from the list (the last element by default) and returns the value of the element.

#!/usr/bin/env python#-*-coding:utf-8-*-List=['ABC','Jack','Taobao']LIST_POP1=list.pop (0) List_pop2=list.pop (1)Print "The first time you delete the item is:", List_pop1Print "The items that are deleted the second time are:", LIST_POP2#Note: After the first deletion, the remaining list elements in List.pop (1) are ' Taobao ', not ' Jack 'Print "the columns are shown in the following:", List
View Code

The uniform () function and the Randint () function in the 4.Python random module

The uniform () method randomly generates a real number in the [x, Y] range, that is, excluding Y, syntax:

Import Random

Random.uniform (x, y)

The Randint () function randomly generates an integer N within a range, in the range of [A, b], a<=n<=b.

Grammar:

Import Random

Random.randint (0,9)

  

Python built-in function usage

Related Article

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.