concept sorted in category python

appears as: sorted
Python Workout: 50 ten-minute exercises

This is an excerpt from Manning's book Python Workout: 50 ten-minute exercises.

Table 2.1 What you need to know (view table figure)

Concept

What is it?

Example

To learn more

in

Operator for searching in a sequence

'a' in 'abcd'

http://mng.bz/yy2G

Slice

Retrieves a subset of elements from a sequence

# returns 'bdf'

'abcdefg'[1:7:2]

http://mng.bz/MdW7

str.split

Breaks strings apart, returning a list

# returns ['abc', 'def', 'ghi']

'abc def ghi'.split()

http://mng.bz/aR4z

str.join

Combines strings to create a new one

# returns 'abc*def*ghi'

'*'.join(['abc', 'def', 'ghi'])

http://mng.bz/gyYl

list.append

Adds an element to a list

mylist.append('hello')

http://mng.bz/aR7z

sorted

Returns a sorted list, based on an input sequence

# returns [10, 20, 30]

sorted([10, 30, 20])

http://mng.bz/pBEG

Iterating over files

Opens a file and iterates over its lines one at a time

for one_line in open(filename):

http://mng.bz/OMAn

You can solve this exercise several ways, but all will require using the sorted method that you saw in the last chapter, along with a function passed as an argument to its key parameter. You can read more about sorted and how to use it, including custom sorts with key, at http://mng.bz/D28E. One of the options for solving this exercise involves operator.itemgetter, about which you can read here: http://mng.bz/dyPQ.

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest