python 基础知识总结
int,float,str,bool,tuple,list,dict
int()
float()
str()
print(11)
print(123, 'hello', sep="&", end='\n\n')
print(11213)
result = int(input('请输入'))
print(type(result),result)
if result<10 :
print('小于10')
elif result >=10 and result<20:
print('正确')
else:
print('其他')
str = 'hello'
result = str.index('h')
print(result)
result = str.find('h')
print(result)
result = len(str)
print(result)
result = str.count('l')
print(result)
result = str.replace('l','x')
print(result)
str = '你,我,他'
result = str.split(',')
print(result)
url = 'http://123.com'
result = url.startswith('http')
print(result)
result = url.endswith('http')
print(result)
str = 'aaabcccc'
result = str.partition('b')
print(result)
sub_str = '_'
result = sub_str.join(str)
print(result)
str = ' hello '
print(str)
result = str.strip()
print(result)
str.lstrip()
str.rstrip()
str= 'asdhello'
result = str.strip('asd')
print(result)
list.append(1)
list = [123, '12', 12, 'hello', 123, 123, '你好']
print(list, type(list))
list.append(1)
list.insert(3, '插入到hello前面')
list.remove(123)
del list[0]
result = 12 in list
print(result)
result = list.index(123)
print(list, result)
result = list.count(123)
print(list, result)
myTuple = (12, 21,'hello')
print(myTuple, type(myTuple))
myTuple = (12, 21, 'hello')
result = 12 not in myTuple
print(result)
result = myTuple.count(12)
print(result)
my_dict = {'name':'沐雪','123':123}
value = my_dict['name']
value = my_dict.get('name','默认值')
print(value)
my_dict["age"] = 18
print(my_dict)
del my_dict['age']
print(my_dict)
value = my_dict.pop('123')
print(value)
my_dict.popitem()
print(my_dict)
result = my_dict.values()
print(result)
result = my_dict.keys()
print(result)
result = 'age' in my_dict
print(result)
name = '沐雪'
print('我叫%s' % name)
num = 0
while num < 5:
print(num)
num += 1
else:
print('over')
for i in range(0, 6, 2):
print(i)
else:
print('over')
num = 0
while num < 5:
num += 1
if num == 3:
break
print(num)
else:
print('over')
会执行else的代码
num = 0
while num < 5:
num += 1
if num == 3:
continue
print(num)
else:
print('over')
my_set = set()
my_set = {'123','hello','world'}
my_set.remove('123')
my_set.discard('hello')
print(my_set)
my_set.add('2333')
for val in my_set:
print(val)
my_list= enumerate([12,3112])
for val in my_list:
print(val)
for index,value in my_list:
print(index,value)
print(my_dict.values())
for key in my_dict:
print(key)
for value in my_dict.values():
print(value)
for key, value in my_dict.items():
print(key, value)
my_set = {12, 3123, 123}
for value in my_set:
print(value)
my_dict = {'name': '123', 'age': '1'}
a1, a2 = my_dict
print(a1, a2)
my_set = {1, 2}
b1, b2 = my_set
print(b1, b2)
my_list = [1, 2]
num1, num2 = my_list
print(num1, num2)
a = 10
def fn(name='muxue', age=18):
global a
a = 20
print(name, age, a)
fn()
fn('joke', 122)
fn(age=12, name='qwe')
fn('qweq', age=123)
print(a)
def sum(*args):
print(args, type(args))
result = 0
for val in args:
result += val
return result
result = sum(1, 2, 3, 4)
print(result)
def sum(**kwargs):
print(kwargs, type(kwargs))
for key, value in kwargs.items():
print(key, value)
sum(a=1, b=2)
def decorator(func):
print('装饰器')
def inner(sum1, sum2):
func(sum1, sum2)
return inner
@decorator
def my_func(sum1, sum2):
result = sum1 + sum2
print(result)
my_func(1, 2)
file = open('test.py', 'r+',encoding='utf-8')
d='hello'
data= d.encode('utf-8')
file.write(data)
content = file.read()
print(content)
file.close()
class Person():
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return self.name
def __del__(self):
print('销毁了')
p = Person('沐雪', 12)
print(p)
class Person():
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return self.name
def __del__(self):
print('销毁了')
```