Python数据分析—变量和数据类型

178 阅读1分钟

1.基础变量

name='ada lovelace'
name.title()#首字母大写
name.upper()#大写
name.lower()#小写

first_name = 'ada'
last_name = 'lovelace'
full_name = first_name + ' ' + last_name  # 合并拼接

print('\tpython')  # 制表符空格
print("languages:\npython\nC\njavascript")  # 换行符

language = 'python'
print(language.rstrip())  # 去掉末尾空白
print(language.lstrip())  # 去掉开头空白
print(language.strip())  # 去掉头尾空白

age = 23
message = 'happy ' + str(age) + 'rd birthday!'#数字加str转换字符串
print(message)

2.列表

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0].title())#切片
message = 'my first bicycle was a ' + bicycles[0].title() + '.'
print(message)
bicycles[0] = 'ducati'  # 修改
bicycles.append('honda')  # 在末尾添加
bicycles.insert(0, 'yamaha')  # 中间插入
del bicycles[0]  # 删除
popped_bicycle = bicycles.pop()  # 删除末尾元素
first_owned = bicycles.pop(0)  # 删除第一个
bicycles.remove('redline')  # 删除特定元素

bicycles.sort()  # 顺排序1
print(sorted(bicycles))  # 顺排序2
bicycles.sort(reverse=True)  # 反转排序1
bicycles.reverse()  # 反转排序2
print(sorted(bicycles, reverse=True))  # 反转排序3