0% found this document useful (0 votes)
47 views3 pages

Python: 1 Useful Build-In Methods 1

command in python

Uploaded by

AL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

Python: 1 Useful Build-In Methods 1

command in python

Uploaded by

AL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python

2021 年 4 月 10 日

目录

1 Useful Build-in Methods 1

1 Useful Build-in Methods

(Keep a minimum list of build in methods and functions)

Python indexing:

1. index starts from zero.

2. a slice goes up to but will not include the value at the second index.

3. negative indexing slicing: a[-4:-1]. You can also specify the step
size, like: s[4:1:-2] index in reverse order and -2 is the step size.

List replication: [x,y,z]*2 generates [x,y,z,x,y,z]

1
Delete one element value in a list: del spam[2] (notice the space in be-
tween.)

l.pop() only remove element at the end of the list.

del(l[index]) remove element in any position.

l.remove(element) remove element by name. If element occurs multiple


times, removes first occurrence.

''.join(l) turn a list of characters into a string. (restricted to characters.)

in and not in are operator. Like `cat` in spam return a Boolean value.

Use ** for exponential operation

Use % to calculate the 余数.

operator +=, -=, the math operator all comes before =, meaning

spam +=1 -> spam = spam + 1

Key, values, items are method, such as A.keys(), A.values() and A.items().
Use these methods to return list-like values of the dictionary’s key, values
or both keys and values.

Dictionary have a get() method that takes two arguments, the key of the
value to retrieve and a fall back value to return if the key does not exist.
picnicItems.get('cup',0). picnicItems is a dictionary.

Delete an entry in a dictionary by using the key: del(grades['ana']). ana


is the key in the dictionary grades. Key in the dictionary is not immutable.

Reserve sorting: spam.sort(reverse = True) or l.reverse() reserve sort-

2
ing on list.

sorted(l) return sorted list, does not mutate list l. l.sort() mutate list
l as well, list is changed.

sort() uses ‘ASCIIbetical order’ rather than actual alphabetical order for
sorting strings. This means uppercase letters come before lowercase let-
ters. If you need to sort the values in regular alphabetical order, pass
str.lower for the ‘key‘ keyword argument in the sort() method call.
spam.sort(key=str.lower)

raw_input().split() : split the input based on space, the result is a list.


Each element inside is a string.

map(int,raw_input()): if the raw input is string, change the type to inte-


ger. The return object is a map object. But you can use list() to convert
it to a list.

Import the advanced print function:

from _future_ import print_function


print(*values, sep=' ', end='\n', file=sys.stdout)

values is an array and *values means array is unpacked (into separate


items), you can add values separated by a comma too. (Does python has
the concept of Array? or Array is list in python.)

The any(...) function returns True if any item in an iterated item are
true, otherwise it returns False. The argument is a list of Boolean results.

You might also like