Python入门小案例

Python入门小案例:

print('hello')


def test():
    print('Mr zhang')


test()

if __name__ == '__main__':
    print('world')


class Fish:
    hungry = True

    def eat(self, food):
        if food is not None:
            self.hungry = False


class User:
    def __init__(self, name):
        self.name = name


f = Fish()
Fish.eat(f, None)
print(f.hungry)

Fish.eat(f, 'earthworm')
f.eat('eatworm')
print(f.hungry)
u = User('zhangjun')
print(u.name)


def test2():
    count = 0
    count = count + 1
    count += 1
    count -= 1
    count = count * 10
    count *= 10
    name = 'Blob'
    # return name
    x = y = z = 1
    print(x, y, z)
    x, y, z = 1, 2, 'a string'
    print(x, y, z)
    x, y = y, x
    print('%d mile is the same as %f km' % (x, y))


test2()
print('**' * 5)
pystr = 'zhangjun'
print('%s is cool' % (pystr))

# 元组tuple
# 身有残疾的只读列表
aTuple = ('robert', 77, 93, 'try')
print(aTuple[1:3])
print(type(aTuple))
# aTuple[1] = 5
for x in aTuple:
    print(x)

# 列表
# 左闭右开,
# -1表示最后一个元素
aList = [1, 2, 3, 'a string']
print(aList[0])
print(aList[2:])
print(aList[:3])
aList[1] = 5
print(aList)
for x in aList:
    print(x)



字典:

# 字典 key-value 键值对
dict1 = {'name:' : '张君', 'age' : 21, 'interest:' : '唱歌'}
print(dict1)
print(type(dict1))
namekey = 'name'
dict2 = {namekey : '张君'}
print(dict2)
# 1表示一个key
dict3 = {'name:' : '张君', 'age' : 21, 'interest:' :['唱歌', '跳舞' ,'打游戏'], 1 : [100, 200, 300]}
print(dict3)
# TypeError: unhashable type: 'list'  --hash表示哈希算法
# dict1 = {'name:' : '张君', 'age' : 21, 'interest:' : '唱歌',[100, 200, 300] :1}
# print(dict1)

# 访问字典的内容 、 读写 、删键del
# del dist['name']   del dist
# 字典-面向对象的方法 dict.clear()、dict.keys()、dict.values()
dict3 = {'name' : '张君', 'age' : 21, 'interest' :['唱歌', '跳舞' ,'打游戏'], 1 : [100, 200, 300]}
myname = dict3['name']
print(myname)
myage = dict3['age']
print(myage)
myhobby = dict3['interest']
print(myhobby)
myint = dict3[1]
print(myint)
#  str(myint)  str(myage) +
print(myname, myage, myhobby, myint)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值