python学习笔记(二):基础知识点

2018/02/28 23:32
阅读数 27

python基本元素

7 // 2
3
7 % 3
1
# 取商以及余数
divmod(7,3)
(2, 1)
1j*1j
(-1+0j)
10/3
3.3333333333333335
'3,''1.00e2'
'3,1.00e2'
'3,' + '1.00e2'
'3,1.00e2'
'na'*3
'nanana'
'na'[0:1]
'n'
w = ''
if w:
    print("hh")
else:
    print("hhh")
hhh
count = 1
while count <=5:
    print(count)
    count += 1
1
2
3
4
5
while True:
    stuff = input('string to capitalize[type q to quit]: ')
    if stuff == "q":
        break
    print(stuff.capitalize())
string to capitalize[type q to quit]: q

不能将可变的数据类型当作函数的参数值

字符串处理

import unicodedata
name = unicodedata.name('\u2603')
value = unicodedata.lookup(name)
value
'☃'
place = 'caf\u00e9'
place
'café'
place = '\N{LATIN SMALL LETTER E WITH ACUTE}'
a = place.encode('utf-8') # 编码
a
b'\xc3\xa9'
b = a.decode('utf-8') # 解码
b
'é'
'%d%%' % 32
'32%'
import re
source = "¥马冬梅.com"
m = re.search(r'\w*', source)
m.group()
''
import string
pri = string.printable
re.findall('/.', pri)
['/:']
import re
source = "<b>frf<b>"
m = re.findall(r'<.+>', source)
m
['<b>frf<b>']
pome = '''thewrj ok poj 'po 
jio lkn 
;jij 
j; %%javascriptjoij 
j;oi joijl 
jiojioj;oij
'''
len(pome)
79
fout = open('datas', 'wt')
fout.write(pome)
79
fout.close()
fin = open('datas', 'r')
pomes = fin.read()
fin.close()
pomes
"thewrj ok poj 'po \njio lkn \n;jij \nj; %%javascriptjoij \nj;oi joijl \njiojioj;oij\n"
fout = open('oops.txt', 'wt')
print('hollow word!', file = fout)
fout.close()
import os
os.path.exists('./oops.txt')
True
name = "oops.txt"
os.path.abspath('oops.txt')
'D:\\Code\\Python\\code\\oops.txt'
os.remove(name)
---------------------------------------------------------------------------

PermissionError                           Traceback (most recent call last)

<ipython-input-27-a353836641f9> in <module>()
----> 1 os.remove(name)


PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'oops.txt'
os.listdir('.')
['.ipynb_checkpoints',
 'introducting python.py',
 'Introduction.ipynb',
 'oops.txt']
os.getcwd()
'D:\\Code\\Python\\code'
import calendar
calendar.isleap(2000)
True
from datetime import date
hallo = date(2014, 10, 31)
hallo.day
31
hallo.isoformat()
'2014-10-31'
now = date.today()
now
datetime.date(2018, 2, 7)
from datetime import timedelta
one = timedelta(days = 1)
tom = now + one * 6
tom
datetime.date(2018, 2, 6)
from datetime import time
noon = time(12, 0, 0)
noon.hour
12
from datetime import datetime
now = datetime.now()
now
datetime.datetime(2018, 1, 31, 15, 30, 45, 978714)
import time 
now = time.time()
now
1518007860.2188663
time.ctime(now)
'Wed Jan 31 15:34:00 2018'
time.localtime(now)
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=31, tm_hour=15, tm_min=34, tm_sec=0, tm_wday=2, tm_yday=31, tm_isdst=0)
time.gmtime(now)
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=31, tm_hour=7, tm_min=34, tm_sec=0, tm_wday=2, tm_yday=31, tm_isdst=0)
fmt = "It's %A, %B %d, %Y, local time %I:%M:%S:%p"
t = time.localtime()
t
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=7, tm_hour=20, tm_min=51, tm_sec=5, tm_wday=2, tm_yday=38, tm_isdst=0)
time.strftime(fmt, t)
"It's Wednesday, January 31, 2018, local time 03:46:19:PM"
fmt = "%Y-%m-%d"
time.strptime("2014-04-28", fmt)
time.struct_time(tm_year=2014, tm_mon=4, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=118, tm_isdst=-1)
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部