课程 \
Python3 入门教程(新版)
4-5 Python之while循环
# Enter a code
num = 1
sum = 0
while num <= 10:
sum += num
num += 1
print(sum)
2023-01-08
查看完整代码
4-4 Python之for循环
# Enter a code
L = [75, 92, 59, 68, 99]
sum = 0.0
for s in L:
sum+=s
print(sum/5)
2023-01-08
查看完整代码
4-3 Python之if-elif-else语句
# Enter a code
age = 10
if age>=50:
print('elderly')
elif 50>age>=20:
print('adult')
elif 20<=age<18:
print('teenager')
elif 3<=age<18:
print('kid')
else:
print('baby')
2023-01-08
查看完整代码
4-2 Python之if-else语句
# Enter a code
age=10
if age>=18:
print('teenager')
else:
print('adult')
2023-01-08
查看完整代码
4-1 Python之if语句
# Enter a code
age = 19
if age>=18:
print('teenager')
else:
print('adult')
2023-01-08
查看完整代码
3-8 Python的字符串编码
# coding: utf-8
print('这是一句中英文混合的Python字符串:\n{}'.format('Hello World!'))
print('这是一句中英文混合的Python字符串:\n{}'.format('Hello World!'))
2023-01-08
查看完整代码
3-7 Python的字符串format
# Enter a code
print('Life is short, {}'.format('you need Python'))
print('Life is short, you need {l}'.format(l='Python'))
2023-01-08
查看完整代码
3-6 Python中raw字符串与多行字符串
# Enter a code
print(r'''To be, or not to be": that is the question.
Whether it's nobler in the mind to suffer''')
2023-01-08
查看完整代码
3-1 Python基础数据类型
# Enter a code
print(type(3.1415926))
print(type('learn python in imooc'))
print(type(100))
print(type(0b1101))
2023-01-08
查看完整代码
3-5 Python的字符串
# Enter a code
s = 'special string: \', ", \\, \\\\, \\n, \\t'
print(s)
2023-01-06
查看完整代码
3-4 Python的布尔类型
# Enter a code
a = 'python'
print('hello,', a or 'world')
b = ''
print('hello,', b or 'world')
2023-01-06
查看完整代码
9-8 Python函数使用可变关键字参数
# Enter a code
def info(**kwargs):
names = kwargs['names']
gender_list = kwargs['gender']
age_list = kwargs['age']
index = 0
for name in names:
gender = gender_list[index]
age = age_list[index]
print('name: {},gender: {},age: {}'.format(name,gender,age))
index += 1
info(names = ['Alice', 'Bob', 'Candy'], gender = ['girl', 'boy', 'girl'], age = [16, 17, 15])
2022-08-20
查看完整代码
9-7 Python函数使用可变参数
# Enter a code
def average(*args):
args_count =len(args)
args_sum=0
if args_count==0:
return 0
else:
for n in args:
if isinstance(n,int) or isinstance(n,float):
args_sum+=n
else:
return -1
return args_sum/args_count
print(average())
print(average(10,11))
print(average(1,'s','sf'))
2022-08-15
查看完整代码
9-6 Python函数使用默认参数
# Enter a code
def greet(base='world'):
print('hello,'+base+'.')
greet()
greet('sanbei')
2022-08-15
查看完整代码
9-5 Python函数参数
# Enter a # Enter a code
def sum(x):
s=0
y=1
if isinstance(x,list):
if len(x)!=0:
for i in x:
if isinstance(i, int) or isinstance(i, float):
s+=i
return s
if isinstance(x,tuple):
if len(x)!=0:
for i in x:
if isinstance(i, int) or isinstance(i, float):
y=i*y
return y
print(sum([1,3,5,'k']))
print(sum((1,3,5,'q')))
print(sum(()))
print(sum([]))
2022-08-15
查看完整代码
首页上一页123下一页尾页