随笔分类 - Python Learning
摘要:If you played with the Fibonacci function, you might have noticed that the bigger the argument you provide, the longer the function takes to run. Furt...
阅读全文
摘要:Lists can appear as values in a dictionary. For example, if you were given a dictionary that maps from letters to frequencies, you might want to inver...
阅读全文
摘要:If you use a dictionary in a for statement, it traverses the keys of the dictionary. For example, print_hist prints each key and the corresponding val...
阅读全文
摘要:Suppose you are given a string and you want to count how many times each letters appears. There are several ways you do it:You could create 26 variabl...
阅读全文
摘要:A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dictionary they can be (almost) any type. You can thin...
阅读全文
摘要:The slice operator can take a third argument that determines the step size, so t[::2] creates a list that contains every other element from t. If the ...
阅读全文
摘要:A string is a sequence of characters and a list is a sequence of values, but a list of characters is not the same as a string. To convert from a strin...
阅读全文
摘要:When you assign an object to a variable, Python copies the reference to the object. In this case a and b refer to the same list.If you want ...
阅读全文
摘要:If we execute these assignment statements: We know that a and b both refer to a string, but we don’t know whether they refer to the same str...
阅读全文
摘要:There are several ways to delete elements from a list. If you know the index of the element you want, you can use pop: pop modifies the list...
阅读全文
摘要:To add up all the numbers in a list, you can use a loop like this: Total is initialized to 0. Each time through the loop, x gets one element...
阅读全文
摘要:Python provides methods that operate on lists. For example, append adds a new element to the end of a list, extend takes a list as an argument and app...
阅读全文
摘要:The + operator concatenates lists: Similarly, the * operator repeats a list a given number of items:List slicesThe slice operator also works...
阅读全文
摘要:The most common way to traverse the elements of a list is with a for loop. The syntax is the same as for strings: This works well if you onl...
阅读全文
摘要:The syntax for accessing the elements of a list is the same as for accessing the characters of a string – the bracket operator ([ ]). The expression i...
阅读全文
摘要:Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in list are called ...
阅读全文
摘要:#! /usr/bin/python# -*- coding: utf-8 -*-### “Recently I had a visit with my mom and we realized that the two digits that# make up my age when reverse...
阅读全文
摘要:This question is based on a Puzzler that was broadcast on the radioprogram Car Talk1:“I was driving on the highway the other day and I happened to not...
阅读全文
摘要:For the exercises in this chapter we need a list of English words. There are lots of word lists available on the Web, but the most suitable for our pu...
阅读全文
摘要:The operators we have seen so far are all special characters like + and *, but there are a few operators that are words. in is a boolean operator that...
阅读全文

浙公网安备 33010602011771号