Python入门 基本用法

avatar 2024年01月24日18:03:11 0 477 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此

以下是在慕课网上学的一小时 python 写的测试代码记录,留作备份

# coding=utf-8
print('''Python is created by "Guido".
It is free and easy to learn.
Let's start learn Python in imooc!''')

template = '{0}是{1}'
name = '张三'
title = '废物中的废物'
print(template.format(name, title))

str = 'imooc'
print(str[0:3])

age = 2
if age > 18:
    print('adult')
else:
    if age > 6:
        print('teenager')
    else:
        print('kid')

names = ['Michael', 'Bob', 'Tracy']
for name in names:
    print(name)

while age < 18:
    print(age)
    if age == 10:
        break
    age += 1

s1 = 'ABC'
s2 = '123'
for item in s1:
    for item2 in s2:
        print(item + item2)

L = ['zhangsan', 18, 'zhangsan', 18]
L2 = []
print(L)
for item in L:
    print(item)
print(L[0])
# print(L2[0])


L = ['zhangsan', 18]
L.append('男')
print(L)

L.insert(0, '中国')
print(L)

L.pop()
print(L)

L.pop(0)
print(L)

L[0] = 'lisi'
print(L)

L3 = ['zhangsan', 18, ['lisi', 20]]
print(L3)
print(L3[2][0])

T = (1, 2, 3, 1, '张三', 'aaa')
print(T.count(1))
print(T.count('张三'))
print(T.index('张三'))
print(T)

T2 = (1,)
print(T2)

## tiple和list的区别,tuple一旦初始化就不能修改,但是tiple里的list可以修改


d = {
    'zhangsan': 18,
    'lisi': 20,
    'wangwu': 30,
    age: [1, 2, 3]
}
print(d['zhangsan'])
print(d.get('zhangsan'))
d['zhangsan'] = 19
print(d['zhangsan'])
d[age] = [1, 2, 3, 4]
d['xxx'] = '张三'
print(d)
d.pop('xxx')
print(d)
for key in d.keys():
    print(key)

names = ['zhangsan', 'lisi', 'wangwu', 'lisi']
print(names)
myset1 = set(names)
print(myset1)

# print("请输入您的姓名")
# name1 = input()
# if name1 in myset1:
#     print(name1 + "已存在")
# else:
#     print(name1 + "不存在")


myset1 = set(names)
myset1.add('张三')
# myset1.remove('张三2') // 不存在会报错
myset1.discard('zhangsan')
print(myset1)

s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(s1.isdisjoint(s2))

# 判断s1是否为s2的子集
print(s1.issubset(s2))  # ==> True
# 判断s2是否为s1的超集
print(s2.issuperset(s1))  # ==> True

L = []
for n in range(1, 10):
    L.append(n * n)
print(L)
print(sum(L))


def my_sum(start, end):
    sum = 0
    for n in range(start, end + 1):
        sum += n
    return sum


print(my_sum(1, 10))


def power(x, n=2):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s


print(power(5))


def average(*args):
    l = len(args)
    if l == 0:
        return 0
    sum = 0
    for a in args:
        sum += a
    return sum / float(l)


print(average(1, 2, 3, 4, 5))
print(average())
  • 微信
  • 交流学习,资料分享
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0