x =3;y =4
print('hello','world!')
print('hello' + 'world!')
print('hello','world',sep='***')
print('#'*50)
print('how are you?',end='')
print(5/2)
print(5//2)
print(5 % 2)
print(5 ** 3)
print(5 > 3)
print(3 > 5)
print(20 > 10 > 5)
print(20 > 10 and 10 > 5)
print(not 20 > 10)
number = input("请输入数字: ")
print(number)
print(type(number))
print(number + 10)
print(int(number) + 10)
print(number + str(10))
username = input('username: ')
print('welcome', username)
print('welcome ' + username)
sentence = 'tom\'s pet is a cat'
sentence2 = "tom's pet is a cat"
sentence3 = "tom said:\"hello world!\""
sentence4 = 'tom said:"hello world"'
words = """
hello
world
abcd"""
print(words)
alist = [10, 20, 30, 'bob', 'alice', [1,2,3]]
len(alist)
alist[-1]
alist[-1][-1]
[1,2,3][-1]
alist[-2][2]
alist[3:5]
10 in alist
'o' in alist
100 not in alist
atuple = (10, 20, 30, 'bob', 'alice', [1,2,3])
len(atuple)
10 in atuple
atuple[2]
print(atuple[3:5])
"""
字典属于:容器、可变、映射
字典的key不能重复
字典的key必须是不可变类型
"""
adict = {'name': 'bob', 'age': 23}
len(adict)
'bob' in adict
'name' in adict
adict['email'] = '[email protected]'
adict['age'] = 25
if 3 > 0:
print('yes')
print('ok')
if 10 in [10, 20, 30]:
print('ok')
if -0.0:
print('yes')
if [1, 2]:
print('yes')
if ' ':
print('yes')
a = 10
b = 20
if a < b:
smaller = a
else:
smaller = b
print(smaller)
s = a if a < b else b
print(s)
s = a if a < b else (b if b < a else print('ok'))
astr = 'hello'
alist = [10, 20, 30]
atuple = ('bob', 'tom', 'alice')
adict = {'name': 'john', 'age': 23}
for ch in astr:
print(ch)
for i in alist:
print(i)
for name in atuple:
print(name)
for key in adict:
print('%s: %s' % (key, adict[key]))
range(10)
>>> list(range(10))
range(6, 11)
range(1, 10, 2)
range(10, 0, -1)
for i in range(1, 10):
for j in range(1, i + 1):
print('%s*%s=%s' % (j, i, i * j), end=' ')
print()
[10 + 5]
[10 + 5 for i in range(10)]
[10 + i for i in range(10)]
[10 + i for i in range(1, 11)]
[10 + i for i in range(1, 11) if i % 2 == 1]
[10 + i for i in range(1, 11) if i % 2]
['192.168.1.%s' % i for i in range(1, 255)]
f = open('/tmp/passwd')
data = f.read()
print(data)
data = f.read()
f.close()
f = open('/tmp/passwd')
data = f.read(4)
f.readline()
f.readlines()
f.close()
f = open('/tmp/passwd')
for line in f:
print(line, end='')
f.close()
f = open('图片地址', 'rb')
f.read(4096)
f.close()
f = open('/tmp/myfile', 'w')
f.write('hello world!\n')
f.flush()
f.writelines(['2nd line.\n', 'new line.\n'])
f.close()
with open('/tmp/passwd') as f:
print(f.readline())
f = open('/tmp/passwd')
f.tell()
f.readline()
f.tell()
f.seek(0, 0)
f.tell()
f.close()
py_str = 'hello world!'
print(py_str)
py_str.capitalize()
print(py_str.capitalize())
print(py_str.title())
print(py_str.center(50))
print(py_str.center(50,'#'))
print(py_str.ljust(50))
print(py_str.rjust(50,'*'))
print(py_str.count('l'))
print(py_str.count('lo'))
print(py_str.endswith('!'))
print(py_str.endswith('d!'))
print(py_str.startswith('a'))
print(py_str.islower())
print(py_str.isupper())
print('Hao123'.isdigit())
print('Hao123'.isalnum())
print(' \thello\t '.strip())
print('hello\t '.lstrip())
print('how are you ?'.split())
print('hello.tar.gz'.split('.'))
print('.'.join(['hello','tar','gz']))
from random import randint
alist = list()
list('hello')
list((10, 20, 30))
astr = str()
str(10)
str(['h', 'e', 'l', 'l', 'o'])
atuple = tuple()
tuple('hello')
num_list = [randint(1, 100) for i in range(10)]
a = max(num_list)
i = min(num_list)
print(a,i)
"%s is %s years old" % ('bob', 23)
"%s is %d years old" % ('bob', 23)
"%s is %d years old" % ('bob', 23.5)
"%s is %f years old" % ('bob', 23.5)
"%s is %5.2f years old" % ('bob', 23.5)
"97 is %c" % 97
"11 is %#o" % 11
"11 is %#x" % 11
"%10s%5s" % ('name', 'age')
"%10s%5s" % ('bob', 25)
"%10s%5s" % ('alice', 23)
"%-10s%-5s" % ('name', 'age')
"%-10s%-5s" % ('bob', 25)
"%10d" % 123
"%010d" % 123
a = "{} is {} years old".format('bob', 25)
b ="{1} is {0} years old".format(25, 'bob')
c = "{:<10}{:<8}".format('name', 'age')
d = "{:>10}{:>8}".format('name', 'age')
print(a)
print(b)
print(c)
print(d)
import shutil
with open('/etc/passwd', 'rb') as sfobj:
with open('/tmp/mima.txt', 'wb') as dfobj:
shutil.copyfileobj(sfobj, dfobj)
shutil.copyfile('/etc/passwd', '/tmp/mima2.txt')
shutil.copy('/etc/shadow', '/tmp/')
shutil.copy2('/etc/shadow', '/tmp/')
shutil.move('/tmp/mima.txt', '/var/tmp/')
shutil.copytree('/etc/security', '/tmp/anquan')
shutil.rmtree('/tmp/anquan')
shutil.copymode('/etc/shadow', '/tmp/mima2.txt')
shutil.copystat('/etc/shadow', '/tmp/mima2.txt')
shutil.chown('/tmp/mima2.txt', user='zhangsan', group='zhangsan')
alist = [1, 2, 3, 'bob', 'alice']
alist[0] = 10
alist[1:3] = [20, 30]
alist[2:2] = [22, 24, 26, 28]
alist.append(100)
alist.remove(24)
alist.index('bob')
blist = alist.copy()
alist.insert(1, 15)
alist.pop()
alist.pop(2)
alist.pop(alist.index('bob'))
alist.sort()
alist.reverse()
alist.count(20)
alist.clear()
alist.append('new')
alist.extend('new')
alist.extend(['hello', 'world', 'hehe'])
adict = dict()
dict(['ab', 'cd'])
bdict = dict([('name', 'bob'),('age', 25)])
{}.fromkeys(['zhangsan', 'lisi', 'wangwu'], 11)
for key in bdict:
print('%s: %s' % (key, bdict[key]))
print("%(name)s: %(age)s" % bdict)
bdict['name'] = 'tom'
bdict['email'] = '[email protected]'
del bdict['email']
bdict.pop('age')
bdict.clear()
adict = dict([('name', 'bob'),('age', 25)])
len(adict)
hash(10)
adict.keys()
adict.values()
adict.items()
adict.get('name')
print(adict.get('qq'))
print(adict.get('qq', 'not found'))
print(adict.get('age', 'not found'))
adict.update({'phone': '13455667788'})
myset = set('hello')
len(myset)
for ch in myset:
print(ch)
aset = set('abc')
bset = set('cde')
aset & bset
aset.intersection(bset)
aset | bset
aset.union(bset)
aset - bset
aset.difference(bset)
aset.add('new')
aset.update(['aaa', 'bbb'])
aset.remove('bbb')
cset = set('abcde')
dset = set('bcd')
cset.issuperset(dset)
cset.issubset(dset)
hex(20)
oct(10)
bin(10)
>>> '%#o' % 10
'0o12'
>>> '%#x' % 10
'0xa'
>>> '%f' % (5/3)
'1.666667'
>>> '%.2f' % (5/3)
'1.67'
>>> '%5.2f' % (5/3)
' 1.67'
>>> '%e' % 10000
'1.000000e+04'
>>> f = open('/tmp/aaaa', 'wb')
>>> hi = '你好\n'
>>> hi.encode()
b'\xe4\xbd\xa0\xe5\xa5\xbd\n'
>>> f.write(hi.encode())
7
>>> import getpass as gp
>>> p = gp.getpass()
Password:
import os
os.getcwd()
os.listdir()
os.listdir('/tmp')
os.mkdir('/tmp/mydemo')
os.chdir('/tmp/mydemo')
os.listdir()
os.mknod('test.txt')
os.symlink('/etc/hosts', 'zhuji')
os.path.isfile('test.txt')
os.path.islink('zhuji')
os.path.isdir('/etc')
os.path.exists('/tmp')
os.path.basename('/tmp/abc/aaa.txt')
os.path.dirname('/tmp/abc/aaa.txt')
os.path.split('/tmp/abc/aaa.txt')
os.path.join('/home/tom', 'xyz.txt')
os.path.abspath('test.txt')