python语法入门二

  • 元组
  • 字符串
  • 格式化
  • 序列

元组tuple:不可变

arr = (1, 2, 3, 4, 5)
print(arr)
# (1, 2, 3, 4, 5)
print(arr[1])
# 2
print(arr[:3])
# (1, 2, 3)

temp = (6)
print(temp)
# 6
print(type(temp))
# <class 'int'>
print(type(arr))
# <class 'tuple'>
temp2 = (6,)
print(type(temp2))
# <class 'tuple'>
temp3 = 1, 2, 3
print(temp3)
# (1, 2, 3)
print(type(temp3))
# <class 'tuple'>

temp4 = temp3 + ("abc",)
print(temp4)
# (1, 2, 3, 'abc')

字符串:不可变

str = 'pytHoN'
print(str.capitalize())
# Python

print(str.center(10))
#   pytHoN  .

print(str.count('y'))
# 1

print(str.endswith('N'))
# True

print(str.find('t'))
# 2

print("Hello".istitle())
# True

print("AA".join("xxx"))
# xAAxAAx

print('    .  is null     .'.lstrip())
# .  is null     .

print(str.split('H'))
# ['pyt', 'oN']

print(str.swapcase())
# PYThOn

格式化

str = "{0} love {1}{2}".format('I', 'py', 'thon')
print(str)
# I love python
str = "{a} love {b}{c}".format(a='I', b='py', c='thon')
print(str)
# I love python
str = "{0} love {b}{c}".format('I', b='py', c='thon')
print(str)
# I love python

print("\\a")
# \a
print("{0}".format("不打印"))
# 不打印
print("{{0}}".format("不打印"))
# {0}
print('{0:.1f}{1}'.format(27.452, 'GB'))
# 27.5GB

print('%c' % 97)
# a
print('%c %c %c' % (97, 98, 99))
# a b c
print('%s' % 'string')
# string
print('%d + %d = %d' % (4, 5, 4 + 5))
# 4 + 5 = 9
print('%f' % 234.4234)
# 234.423400
print('%10.2f' % 234.4234)
#     234.42
print('%+d' % 34)
# +34

序列

print(list())
# []

str = 'string'
str = list(str)
print(str)
# ['s', 't', 'r', 'i', 'n', 'g']

c = (1, 2, 3, 4)
c = list(c)
print(c)
# [1, 2, 3, 4]

print(max(1, 2, 3, 4, 5))
# 5

arr = {1, 2, 3, 4, 5}
print(sum(arr))
# 15
print(sum(arr, 10))
# 25

print(list(enumerate(arr)))
# [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

a = [1, 2, 3]
b = [4, 5, 6, 7, 8]
print(list(zip(a, b)))
# [(1, 4), (2, 5), (3, 6)]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值