先介绍一下math所包含最基础的用法,使用math可以帮助你更好的进行简单的计算,比如平方,根号等。
import math math.sqrt(12.0) dir(math)
dir()的作用是:返回指定对象的所有属性和方法,不带值。
此函数将返回所有属性和方法,甚至是所有对象的默认内置属性。
from math import cos # import cos into the namespace cos(0.4) # no need to write math.cos from math import * # import all functions sqrt(4.0)
接下来讲解一下如何判断属于各种不同类型
变量是动态类型的,即不需要显式声明类型
a = 1 type(a) #result is int b = 1.0 type(b) #result is float b = a type(b) #result is int x = True x = False type(x) #result is Bool x = 1.0 + 4.0j type(x) #result is complex
计算不可避免要用到各种运算符号,以下是比较常见的单一运算符号
20 + 3, 20 - 3 20 * 3, 20 / 3, 20.0 / 3 # Note, *integer* division 20 % 3, 20.5 % 3.1 # Modulus, i.e. remainder a = 1; a +=1 ; print(a) a *= 5; print(a)
4 > 7, 4 < 7, 4 >= 7, 4 <= 7 4 == 7, 4 != 7
逻辑运算
4 > 7 and 4 < 7 #result is False 4 > 7 or 4 < 7 #result is True not 4 > 7 #result is True
复合类型:字符串, 列表,集合,字典
字符串:
city = "Sheffield" type(city) #result is str city = 'Sheffield' # can use " or ' type(city) #result is str city[0] # strings are like lists characters #result is s 'Jon' + ' ' + 'Barker' # string concatenation #result is 'Jon Barker' city.upper(), city.lower() help(str)
当你不确定自己该怎么做就用help(str),他会显示模块内置中有关类str的帮助
比如以下的这些:
add(self, value, /)
Return self+value.
contains(self, key, /)
Return key in self.
eq(self, value, /)
Return self==value.
format(self, format_spec, /)
Return a formatted version of the string as described by format_spec.
ge(self, value, /)
Return self>=value.
getattribute(self, name, /)
Return getattr(self, name).
getitem(self, key, /)
Return self[key].
列表类似于字符串,但元素可以是任何类型
列表:
primes = [2, 3, 5, 7, 11] type(primes) #result is list weird = [1, 'Monday', 1.4, False, 4+5j] # Mixed types print(weird) #result is [1, 'Monday', 1.4, False, (4+5j)]
运用列表里的数据:
days = ['Mon', 'Tues', 'Weds', 'Thur', 'Fri', 'Sat', 'Sun'] days[0] #result is 'Mon' days[1:3] #result is ['Tues', 'Weds'] days[:] #result is ['Mon', 'Tues', 'Weds', 'Thur', 'Fri', 'Sat', 'Sun'] days[3:] #result is ['Thur', 'Fri', 'Sat', 'Sun'] days[::2] #result is ['Mon', 'Weds', 'Fri', 'Sun']
列表操作:追加、计数、扩展、索引、插入、弹出、删除、反转、排序
days = ['Mon', 'Tues', 'Weds', 'Thur', 'Fri', 'Sat', 'Sun']
days.reverse()
print(days)
days.sort()
print(days)
#result is ['Fri', 'Mon', 'Sat', 'Sun', 'Thur', 'Tues', 'Weds']
x = [1, 2, 3, 4]
x.extend([5, 6, 7])
print(x)
x = list('Let us go then, you and I')
x.count('e'), x.count(' ')
这里的sort补充说明一下,它将列表里的周一到周天按首字母在字母表的顺序排序了。sort不一定非要这样,也可以自己定义想要如何sort列表里的所有信息。
tuples是不可变的列表,即一旦创建它们就不能修改。
x = (1, 2) type(x) print(x[0]) #result is 1
括号并非绝对必要
pos1 = (10, 20, 30) pos2 = (10, 25, 30) pos1 == pos2 #result is False
如果所有元素相等,则为true
x, y = 10, 15 x, y = y, x print(x, y) #result is 10,15
可以用一行交换变量
集合中的项目必须是唯一的。集合使用花括号。
x = {1, 2, 3, 4, 3, 2, 1, 2}
print(x)
#result is {1, 2, 3, 4}
print({1,2,3,7,8, 9}.intersection({1,3,6,7,10}))
print({1,2,3,7,8, 9}.union({1,3,6,7,10}))
print({1,2,3,7,8, 9}.difference({1,3,6,7,10}))
#result is {1, 3, 7}
# {1, 2, 3, 6, 7, 8, 9, 10}
# {8, 9, 2}
字典从唯一键映射到值
office = {'jon': 123, 'jane':146, 'fred':245}
type(office)
#result is dict
office['jon']
#result is 123
office = {'jon': 123, 'jane':146, 'fred':245, 'jon': 354}
print(office['jon'])
#result is 354
dict 就是dictionaries的缩写
后一个条目会覆盖前一个条目,所以在字典里它默认的是最后的一个值。
字典的应用
office['jose'] = 282 # add a new key-value pair print(office) office.keys() office.values() 'jose' in office # check if a key exists
keys 导出在字典里的所有条目名称;
而value导出字典里所有条目所对应的值
更多的信息请看初学者一看就懂的入门python2
希望大家能够多多关注我,我会分享给大家更多简单易懂的代码方式



