基础一
赋值
a = b = c = d = 10
e记法
a = 0.000000025
b = 2.5e-8
print ( a, "\n" )
print ( b)
变量类型转换
a = "520"
b = int ( a)
b
a = 5.99
b = int ( a)
b
a = "520"
b = float ( a)
b
b = str ( 5e + 19 )
b
a = "520"
type ( a)
a = "打野"
isinstance ( a, str )
运算符
+, -, *, /, %, **, //
print ( 10 // 4 )
print ( 3.0 // 2 )
print ( 3 ** 3 )
print ( divmod ( 9 , 5 ) )
2
1.0
27
(1, 4)
逻辑操作符
True and False
True or False
not True
3 < 4 < 5
条件分支
a = 0
if True :
a = 1 ;
else :
a = 2 ;
a
while循环
a = 0
while a < 8 :
a += 1
输入
temp = input ( "Enter a number:" )
print ( temp)
导入包
import keyword
print ( keyword. kwlist )
断言(assert)
assert 3 < 4
for循环
str = "Daye"
for ch in str :
print ( ch, end = " " )
member = [ "Tom" , "Nancy" , "Tom" , "Bill" ]
for each in member:
print ( each, len ( each) )
range ( 5 )
list ( range ( 5 ) )
for each in range ( 2 , 9 ) :
print ( each)
for each in range ( 1 , 10 , 2 ) :
print ( each)
基础二
字符串
分片([start : end : step])
str = 'Daye is a boy'
print ( str [ : 13 : 2 ] )
print ( str [ - 3 : : 1 ] )
Dy saby
boy
split()分割
str2 = 'I am twenty-two years old'
print ( str2. split( ) )
str3 = 'apple,pear,banana,bean'
print ( str3. split( ',' ) )
['I', 'am', 'twenty-two', 'years', 'old']
['apple', 'pear', 'banana', 'bean']
join()合并
crypto_list = [ 'yellow' , 'blue' , 'black' , 'white' ]
crypto_string = ',' . join( crypto_list)
print ( crypto_string)
yellow,blue,black,white
startswith()、endswith()
poem = 'All that doth flow we cannot liquid name that'
print ( "startswith = " , poem. startswith( 'All' ) )
print ( "endswith = " , poem. endswith( 'name' ) )
startswith = True
endswith = False
find()、rfind()
print ( poem. find( 'that' ) )
print ( poem. rfind( 'that' ) )
4
41
count()
poem. count( 'that' )
2
isalnum()是否全为数字或者字母
print ( "pome " , poem. isalnum( ) )
poem2 = 'abcd'
print ( "pome2 " , poem2. isalnum( ) )
pome False
pome2 True
strip()将字符串尾部的规定字符删除
setup = 'a duck goes into a bar...'
setup. strip( '.' )
'a duck goes into a bar'
大小写操作
setup. capitalize( )
'A duck goes into a bar...'
setup. title( )
'A Duck Goes Into A Bar...'
setup. upper( )
'A DUCK GOES INTO A BAR...'
setup. lower( )
'a duck goes into a bar...'
setup. swapcase( )
'A DUCK GOES INTO A BAR...'
格式排版函数
print ( setup. center( 30 ) )
print ( setup. ljust( 30 ) )
print ( setup. rjust( 30 ) )
a duck goes into a bar...
a duck goes into a bar...
a duck goes into a bar...
replace()
setup. replace( 'duck' , 'marmoset' , 100 )
'a marmoset goes into a bar...'
列表、元组、字典与集合
列表
普通列表
member = [ "Tom" , "Nancy" , "Tony" , "Bill" ]
print ( member)
number = [ 1 , 2 , 3 , 4 , 5 ]
print ( number)
['Tom', 'Nancy', 'Tony', 'Bill']
[1, 2, 3, 4, 5]
混合列表
mex = [ 1 , "Daye" , 3.14 , [ 1 , 2 , 3 ] ]
print ( mex)
空列表
empty = [ ]
empty
向列表添加元素
append()方法
member. append( "Linda" )
member
extend()方法
member. extend( [ 'Dear' , 'Pmr' ] )
member
insert()方法
member. insert( 1 , 'Zqy' )
member
从列表中删除元素
remove()方法
member. remove( 'Tom' )
member
del方法
del member[ 0 ]
member
['Nancy', 'Bill', 'Linda', 'Dear', 'Pmr']
pop()方法[有返回值]
member. pop( )
'Pmr'
name = member. pop( )
name
'Dear'
member. pop( 1 )
member
['Nancy', 'Linda']
列表分片(Slice)
print ( "member = " , member)
print ( "1:3 = " , member[ 1 : 3 ] )
print ( ":3 = " , member[ : 3 ] )
print ( ": = " , member[ : ] )
print ( ": : 2= " , member[ : : 2 ] )
member = ['Tom', 'Nancy', 'Tony', 'Bill']
1:3 = ['Nancy', 'Tony']
:3 = ['Tom', 'Nancy', 'Tony']
: = ['Tom', 'Nancy', 'Tony', 'Bill']
: : 2= ['Tom', 'Tony']
列表常用操作符
比较操作符
list1 = [ 123 , 456 ]
list2 = [ 234 , 123 ]
print ( "list1 > list2 = " , list1 > list2)
list3 = [ 123 , 456 ]
print ( "list1 < list2) and (list1 == list3) = " , ( list1 < list2) and ( list1 == list3) )
list1 > list2 = False
list1 < list2) and (list1 == list3) = True
连接、重复操作符
list3 * 3
list3 *= 3
print ( list3)
[123, 456, 123, 456, 123, 456]
成员关系操作符
123 in list3
"123" not in list3
True
list5 = [ 123 , [ "Daye" , "Pmr" ] , 456 ]
print ( 'Daye' in list5)
print ( 'Daye' in list5[ 1 ] )
print ( list5[ 1 ] [ 1 ] )
False
True
Pmr
内置函数
print ( dir ( list ) )
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
print ( list3. count( 123 ) )
81
index(member, startSite, endSite)
print ( list3)
print ( list3. index( 123 , 3 , 10 ) )
[123, 456, 123, 456, 123, 456]
4
list3. reverse( )
print ( list3)
[456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123]
list6 = [ 4 , 5 , 6 , 1 , 10 , 2 ]
list6. sort( )
print ( list6)
list6. sort( reverse = True )
print ( list6)
list6_1 = sorted ( list6)
print ( list6_1)
[1, 2, 4, 5, 6, 10]
[10, 6, 5, 4, 2, 1]
[1, 2, 4, 5, 6, 10]
list7 = list6
print ( "list7 = " , list7)
list8 = list6[ : ]
print ( "list8 = " , list8)
list9 = list6. copy( )
print ( "list9 = " , list9)
list6. sort( )
print ( "反向排序后:" )
print ( "list7 = " , list7)
print ( "list8 = " , list8)
print ( "list9 = " , list9)
list7 = [10, 6, 5, 4, 2, 1]
list8 = [10, 6, 5, 4, 2, 1]
list9 = [10, 6, 5, 4, 2, 1]
反向排序后:
list7 = [1, 2, 4, 5, 6, 10]
list8 = [10, 6, 5, 4, 2, 1]
list9 = [10, 6, 5, 4, 2, 1]
marxes = [ 'yellow' , 'black' , 'white' ]
print ( ',' . join( marxes) )
yellow,black,white
元组
创建元组
普通创建
empty_tutle = ( )
print ( empty_tutle)
one_marx = 'Groucho' ,
print ( one_marx)
marx_tuple = ( 'yellow' , 'black' , 'white' )
print ( marx_tuple)
x, y, z = marx_tuple
print ( x, " " , y, " " , z)
()
('Groucho',)
('yellow', 'black', 'white')
yellow black white
利用tuple()函数创建元组
marx_list = [ 'yellow' , 'black' , 'white' ]
tuple ( marx_list)
('yellow', 'black', 'white')
字典
创建字典
普通创建
empty_dict = { }
print ( empty_dict)
bierce = {
"day" : "A period of twenty-four hours" ,
"positive" : "Mistaken at the top of one's voice" ,
"misfortune" : "The kind of fortune that never misses"
}
print ( bierce)
{}
{'day': 'A period of twenty-four hours', 'positive': "Mistaken at the top of one's voice", 'misfortune': 'The kind of fortune that never misses'}
利用dict()函数转换为字典
lol = [ [ 'a' , 'b' ] , [ 'c' , 'd' ] , [ 'e' , 'f' ] ]
print ( "列表+子列表: " , dict ( lol) )
lot = [ ( 'a' , 'b' ) , ( 'c' , 'd' ) , ( 'e' , 'f' ) ]
print ( "列表+元组: " , dict ( lot) )
tol = ( [ 'a' , 'b' ] , [ 'c' , 'd' ] , [ 'e' , 'f' ] )
print ( "元组+列表: " , dict ( tol) )
los = [ 'ab' , 'cd' , 'ef' ]
print ( "列表+双字符: " , dict ( los) )
tos = ( 'ab' , 'cd' , 'ef' )
print ( "元组+双字符: " , dict ( tos) )
列表+子列表: {'a': 'b', 'c': 'd', 'e': 'f'}
列表+元组: {'a': 'b', 'c': 'd', 'e': 'f'}
元组+列表: {'a': 'b', 'c': 'd', 'e': 'f'}
列表+双字符: {'a': 'b', 'c': 'd', 'e': 'f'}
元组+双字符: {'a': 'b', 'c': 'd', 'e': 'f'}
使用[key]添加或修改元素
some_pythons = {
'A' : 'Tony' ,
'B' : 'Nancy' ,
'C' : 'Tom' ,
'D' : 'Linda' ,
'E' : 'Terry'
}
print ( some_pythons)
some_pythons[ 'E' ] = 'Anny'
some_pythons[ 'F' ] = 'Lucy'
print ( some_pythons)
{'A': 'Tony', 'B': 'Nancy', 'C': 'Tom', 'D': 'Linda', 'E': 'Terry'}
{'A': 'Tony', 'B': 'Nancy', 'C': 'Tom', 'D': 'Linda', 'E': 'Anny', 'F': 'Lucy'}
update()合并字典
other = { 'G' : 'Vance' , 'H' : 'Case' }
some_pythons. update( other)
print ( some_pythons)
{'A': 'Tony', 'B': 'Nancy', 'C': 'Tom', 'D': 'Linda', 'E': 'Anny', 'F': 'Lucy', 'G': 'Vance', 'H': 'Case'}
del删除元素
del some_pythons[ 'H' ]
some_pythons
{'A': 'Tony',
'B': 'Nancy',
'C': 'Tom',
'D': 'Linda',
'E': 'Anny',
'F': 'Lucy',
'G': 'Vance'}
clear()删除所有元素
other. clear( )
other
{}
获取元素
print ( some_pythons[ 'A' ] )
print ( some_pythons. get( 'B' ) )
Tony
Nancy
dict_keys(['A', 'B', 'C', 'D', 'E', 'F', 'G'])
获取键和值
print ( some_pythons. keys( ) )
print ( some_pythons. values( ) )
print ( some_pythons. items( ) )
dict_keys(['A', 'B', 'C', 'D', 'E', 'F', 'G'])
dict_values(['Tony', 'Nancy', 'Tom', 'Linda', 'Anny', 'Lucy', 'Vance'])
dict_items([('A', 'Tony'), ('B', 'Nancy'), ('C', 'Tom'), ('D', 'Linda'), ('E', 'Anny'), ('F', 'Lucy'), ('G', 'Vance')])
集合
创建集合
empty_set = set ( )
print ( empty_set)
even_number = { 0 , 1 , 3 , 5 , 7 }
print ( even_number)
print ( set ( 'letters' ) )
set()
{0, 1, 3, 5, 7}
{'l', 'r', 's', 'e', 't'}
合并及运算符
a = { 1 , 2 }
b = { 2 , 3 }
print ( "a & b = " , a & b)
print ( "intersection(): " , a. intersection( b) )
a & b = {2}
intersection(): {2}
print ( "a | b = " , a | b)
a | b = {1, 2, 3}
print ( "a - b" , a - b)
a - b {1}
print ( "a ^ b" , a ^ b)
print ( "symmetric_difference(): " , a. symmetric_difference( b) )
a ^ b {1, 3}
symmetric_difference(): {1, 3}
print ( "a <= b" , a <= b)
print ( "issubset(): " , a. issubset( b) )
a <= b False
issubset(): False
print ( "a < b" , a < b)
print ( "issuperset(): " , a. issuperset( b) )
a < b False
issuperset(): False