python基础(2) 基础数据类型,以及其他

所有内容来源于 Problem Solving with Algorithms and Data Structures using Python

By Brad Miller and David Ranum, Luther College
http://interactivepython.org/courselib/static/pythonds/index.html

Method Name Use Explanation
append  alist.append(item)  Adds a new item to the end of a list
insert  alist.insert(i,item)    Inserts an item at the ith position in a list
pop alist.pop() Removes and returns the last item in a list
pop alist.pop(i)    Removes and returns the ith item in a list
sort    alist.sort()    Modifies a list to be sorted
reverse alist.reverse() Modifies a list to be in reverse order
del del alist[i]    Deletes the item in the ith position
index   alist.index(item)   Returns the index of the first occurrence of item
count   alist.count(item)   Returns the number of occurrences of item
remove  alist.remove(item)  Removes the first occurrence of item
>>> (54).__add__(21)
75
>>>
Method Name Use Explanation
center  astring.center(w)   Returns a string centered in a field of size w
count   astring.count(item) Returns the number of occurrences of item in the string
ljust   astring.ljust(w)    Returns a string left-justified in a field of size w
lower   astring.lower() Returns a string in all lowercase
rjust   astring.rjust(w)    Returns a string right-justified in a field of size w
find    astring.find(item)  Returns the index of the first occurrence of item
split   astring.split(schar)    Splits a string into substrings at schar
  • list[最灵活]; tuple(不可改,可重复); set{无序不重复},使用set()创建;{}创建空字典
  • set没有索引,可以交集并集差集
  • dict:key value
  • list生成list(range()); 或者sqlist=[x*x for x in range(1,11) if x%2 != 0]
Operation Name  Operator    Explanation
membership  in  Set membership
length  len Returns the cardinality of the set
|   aset | otherset Returns a new set with all elements from both sets
&   aset & otherset Returns a new set with only those elements common to both sets
-   aset - otherset Returns a new set with all items from the first set not in second
<=  aset <= otherset    Asks whether all elements of the first set are in the second
union   aset.union(otherset)    Returns a new set with all elements from both sets
intersection    aset.intersection(otherset) Returns a new set with only those elements common to both sets
difference  aset.difference(otherset)   Returns a new set with all items from first set not in second
issubset    aset.issubset(otherset) Asks whether all elements of one set are in the other
add aset.add(item)  Adds item to the set
remove  aset.remove(item)   Removes item from the set
pop aset.pop()  Removes an arbitrary element from the set pop扔掉第一个
clear   aset.clear()    Removes all elements from the set
dict 也是无序的
Operator    Use Explanation
[]  myDict[k]   Returns the value associated with k, otherwise its an error
in  key in adict    Returns True if key is in the dictionary, False otherwise
del del adict[key]  Removes the entry from the dictionary
keys    adict.keys()    Returns the keys of the dictionary in a dict_keys object
values  adict.values()  Returns the values of the dictionary in a dict_values object
items   adict.items()   Returns the key-value pairs in a dict_items object
get adict.get(k)    Returns the value associated with k, None otherwise
get adict.get(k,alt)    Returns the value associated with k, alt otherwise
  • input()用法
格式化输出
Character   Output Format
d, i    Integer
u   Unsigned integer
f   Floating point as m.ddddd
e   Floating point as m.ddddde+/-xx
E   Floating point as m.dddddE+/-xx
g   Use %e for exponents less than −44 or greater than +5+5, otherwise use %f
c   Single character
s   String, or any Python data object that can be converted to a string by using the str function.
%   Insert a literal % character


Modifier    Example Description
number  %20d    Put the value in a field width of 20
-   %-20d   Put the value in a field 20 characters wide, left-justified
+   %+20d   Put the value in a field 20 characters wide, right-justified
0   %020d   Put the value in a field 20 characters wide, fill in with leading zeros.
.   %20.2f  Put the value in a field 20 characters wide with 2 characters to the right of the decimal point.
(name)  %(name)d    Get the value from the supplied dictionary using name as the key.
  • 异常处理:the logic error leads to a runtime error that causes the program to terminate. These types of runtime errors are typically called exceptions.
  • try and except
  • raise statement
  • finally语句
>>> try:
       print(math.sqrt(anumber))
    except:
       print("Bad Value for square root")
       print("Using absolute value instead")
       print(math.sqrt(abs(anumber)))

Bad Value for square root
Using absolute value instead
4.79583152331
>>>


>>> if anumber < 0:
...    raise RuntimeError("You can't use a negative number")
... else:
...    print(math.sqrt(anumber))
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
RuntimeError: You can't use a negative number
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值