python工具:pycharm配置以及使用
http://blog.csdn.net/hk2291976/article/details/51141868
一.python基础
1.数字运算符
2**3 指数 =8
22%8 取余 =6
22//8 整除 =2
22/8 除法 =2.75
3*5 乘法 =15
2.数据类型
整型 -2,0,3浮点型-3.5, 0.0, 8.9字符串型 ‘qq','HELLO!','11CAT'
3.字符串连接和复制
>>> 'Alice'+'Bob'
'AliceBob'
>>> 'Alice'*3
'AliceAliceAlice'
4.变量名规则
不允许:
中划线
空格
数字开头
特殊字符$'python常用变量名:
小写字母开头
5.常用函数
>>> myname=input() #这里是注释:输入#
liujiao>>> print('Nice to meet you, myname is' + myname)#输出#
Nice to meet you, myname isliujiao>>> spam=input()100
>>> spam #输入为字符串#
'100'
>>> len('hello') #字符串长度#
5
>>> str(29)#整数转变为字符串#
'29'
>>> int('42')#字符串转变为整型#
42
>>> float('3')#整数转变为浮点数#
3.0
>>> int(7.7))#浮点数转变为整数#
7
二.控制流
1.布尔值,比较操作符
>>>#==和!=用于所有数据类型的比较#
>>> 44==88False>>> 'hello'=='Hello'False>>> 23==23.0True>>> 23=='23.0'False>>> 2!=3True>>> 'cat'!='dog'True
>>>#<和>和<=和>=用于整型和浮点型#
2.布尔操作符
>>> True andTrue
True>>> True andFalse
False>>> True orFalse
True>>> False orFalse
False
>>> not True
False
3.混合布尔和比较操作符
>>> (4<5) and (5<6)
True
>>> (4<5) and (5<6) or (not 2+2==4)
True
4.if/elseif/else
>>> name = 'Bob'
>>> age = 5
>>> if name =='Alice':print('Hi,Alice')elif age<12:print('hi')else:print('hello')
hi
5.while/sys.exit
importsyswhileTrue: #当用于条件判断时,0,0.0,‘’(空字符串)被认为是False,其他值被认为是Trueprint('Type exit to exit.')
response=input()if response == 'your name':
sys.exit()#退出程序
#break #退出循环
print('good job')print('end')
6.for/range()
for i in range(5):print('i is' + str(i))
for i in range(10,13):print('i is' + str(i))
for i in range(10,20,5):print('i is' + str(i))
三.函数
1.print()
>>> spam = print('hello')
hello>>> None ==spam
True>>> print('cat', 'dog','mice')
cat dog mice>>> print('cat', 'dog','mice', sep=',')
cat,dog,mice
>>> print('one two three' + \
... 'four five')
one two threefour five
2.try/catch
defspam(dividedBy):try:return 42/dividedByexceptZeroDivisionError:print('Error')print(spam(0))print(spam(2)) #继续执行
输出
Error
None21.0
defspam(dividedBy):return 42/dividedBytry:print(spam(0))print(spam(2)) #一旦执行到except语句,就无法返回
exceptZeroDivisionError:print('Error')
输出
Error
四.列表
1.一维列表/二维列表/下标/切片
>>> spam=['cat','dog','rice']>>>spam[0]'cat'
>>> spam[0:2]
['cat', 'dog']>>> spam=[['cat','dog','rice'],[10,20,30]]>>> spam[1][2]30
2.列表链接和复制
>>> [1,2,3]+['a','b','c']
[1, 2, 3, 'a', 'b', 'c']>>> [1,2,3]*3[1, 2, 3, 1, 2, 3, 1, 2, 3]
3.列表删除
>>> spam=['cat','dog','mice']>>> del spam[2]>>>spam
['cat', 'dog']
4.in/not in操作符
>>> 'cat' in ['cat','dog']
True>>> 'mice' not in ['cat','dog']
True
5.多重赋值
>>> spam=['cat','dog','mice']>>> cat,dog,mice=spam>>>cat'cat'
>>>dog'dog'
>>>mice'mice'
6.列表的其他方法
>>> spam=['cat','dog','mice']>>> spam.index('dog')1
>>> spam.append('monkey')>>>spam
['cat', 'dog', 'mice', 'monkey']>>> spam.insert(1,'chicken')>>>spam
['cat', 'chicken', 'dog', 'mice', 'monkey']>>> spam.remove('monkey')>>>spam
['cat', 'chicken', 'dog', 'mice']>>> spam.insert(1,'chicken')>>>spam
['cat', 'chicken', 'chicken', 'dog', 'mice']>>> spam.remove('chicken') #只能删除第一次出现的chichen
>>>spam
['cat', 'chicken', 'dog', 'mice']>>> spam = [2,5,-9,8]>>>spam.sort()>>>spam
[-9, 2, 5, 8]>>> spam.sort(reverse=True)>>>spam
[8, 5, 2, -9]
6.字符串和元组
五.字典和结构化数据
1.字典的定义
>>> mycat={'size':'fat','color':'gray','disposition':'loud'}>>> mycat['size']'fat'
2.字典与列表
>>> spam=['cat','dog','mouse']>>> bacon=['dog','cat','mouse']>>> spam==bacon #列表是排序的
False>>> mycat={'size':'fat','color':'gray','disposition':'loud'}>>> hercat={'color':'gray','disposition':'loud','size':'fat'}>>> mycat==hercat #字典是不排序的
True
3.字典的用法
1)keys, values,items
>>> mycat={'size':'fat','color':'gray','disposition':'loud'}>>> for k inmycat.keys():
...print(k)
...
size
color
disposition>>>
>>>
>>> for v inmycat.values():
...print(v)
...
fat
gray
loud>>> for i inmycat.items():
...print(i)
...
('size', 'fat')
('color', 'gray')
('disposition', 'loud')
>>> list(mycat.keys())
['size', 'color', 'disposition']
>>> for k,v in mycat.items():
... print('key:' + k + ',value:' + v)
...
key:size,value:fat
key:color,value:gray
key:disposition,value:loud
>>> 'fat' in mycat.values()
True
>>> 'color' in mycat.keys()
True
2)get(), setdefault()
>>> mycat.get('color', 'white')'gray'
>>> mycat.get('colors', 'white')'white'
>>> mycat.setdefault('width',8)8
>>> mycat.setdefault('width',10)8
>>>mycat
{'size': 'fat', 'color': 'gray', 'disposition': 'loud', 'width': 8}
4.嵌套的字典和列表
allGuests = {'Alice': {'apples': 5, 'bananas':4},'Bob': {'sandwiches':3, 'apples':3},'Carol': {'cups':10, 'apple pies': 9}}deftotalBrought(guests, item):
numbrought=0for k,v inguests.items():
numbrought= numbrought +v.get(item, 0)returnnumbroughtprint('Apples' + str(totalBrought(allGuests, 'apples')))print('Cakes' + str(totalBrought(allGuests, 'cakes')))
D:\liujiao\python3.6.0\python.exe D:/liujiao/pyCharm/test/test.py
Apples8Cakes 0
六.字符串操作
1.处理字符串
>>> spam = 'This is Alice\'s cat'
>>>spam"This is Alice's cat"
>>> """This is test python program,
... written by liujiao"""
'This is test python program,\nwritten by liujiao'
>>>spam[0]'T'
>>> spam[3:7]'s is'
>>> 'hello' in 'hello world'
True
>>> 'HELLO' in 'hello world'
False
2.有用的字符串方法
1)upper(), lower(),isupper(),islower()
>>> spam = 'Hello world'
>>> spam =spam.lower()>>>spam'hello world'
>>>spam.upper()'HELLO WORLD'
>>> spam = 'Hello world'
>>>spam.islower()
False>>> 'abc1234'.islower()
True>>> '123'.islower()
False>>> '123'.isupper()
False
2)isX
isalpha():字符串只包含字母,并且非空。则为true
isalnum():字符串只包含字母和数字,并且非空。则为true
isdecimal():字符串只包含数字字符,并且非空。则为true
isspace():字符串只包含空格,制表符和换行,并且非空。则为true
istitle():字符串只包含以大写字母开头,后面都是小写字母的单词。则为true
3)startswith(),endswith()
>>> 'hello world'.startswith('hello')
True>>> 'hello world'.endswith('world')
True
4)join(),split()
>>> ','.join(['cat', 'dog','mouse'])'cat,dog,mouse'
>>>
>>> 'my name is liujiao'.split()
['my', 'name', 'is', 'liujiao']>>> 'my name is liujiao'.split('m')
['', 'y na', 'e is liujiao']