python基础知识

一、变量和简单数据类型

1.字符串

name = " ada livelace "

name.title() 以首字母大写的方式显示每个单词

name.rstrip() 剔除字符串末尾空白

name.lstrip() 剔除字符串开头空白

name.strip() 剔除字符串开头和末尾空白

name.upper() 字符串改为全部大写

name.lower()  字符串改为全部小写

2.函数str()的用法

age=23

print("Happy "+str(age)+"rd Birthday")

二、列表简介

bicycles['trek','cannondale','redline']

1.索引从0开始,即第一个元素的索引为0---bicycles[0]

2.修改、添加和删除元素

2.1修改第二个元素的值:bicycles[1]='mobike'

2.2.在列表末尾添加元素: bicycles.append('mobike')

2.3.在列表中插入元素: bicycles.insert(0,'honda')

2.4.从列表中删除元素:del bicycles[1]

2.5.pop()方法获取列表末尾元素,并让你能够接着使用它

poped_bicycles = bicycles.pop()

print(poped_bicycles)

输出结果:redline

2.6.删除列表中任何位置的元素,只要在pop方法的括号内指定要删除元素的索引

poped_bicycles = bicycles.pop(1)

2.7.根据值删除元素,且删除后可接着使用

expensive = 'cannondale'

bicycles.remove(expensive)

print(" A "+expensive.title()+" is too expensive for me")

3.组织列表

3.1使用sort()方法对列表进行永久排序(基于小写)

bicycles列表按字母顺序排列:bicycles.sort()

bicycles列表按字母相反的顺序排列:bicycles.sort(reverse=True)

3.2使用sorted()列表进行临时排序(基于小写)

print(sorted(bicycles))

3.3倒着打印列表

bicycles.reverse()

3.4确定列表的长度:len(bicycles)

三、操作列表

1.遍历整个列表

for bicycle in bicycles:

  print(bicycle)

2.创建数值列表

2.1打印1-4数字:

for value in range(1,5):
    print(value)
输出列表:
1
2
3
4

2.2range()作为list()参数,输出将为一个列表

numbers==list(range(1,5))
    print(numbers)
输出结果:
[1, 2, 3, 4]

2.3使用range()函数还可以指定步长

下面代码打印1-10的偶数:

numbers=list(range(2,11,2))
for number in numbers:
    print(number)

2.4对数字列表执行简单的统计计算

digits=[1,2,3,4,5,6,7,8,9]
min(digits)   取列表最小值
max(digits)  取列表最大值
sum(digits)  求列表各元素之和

2.5列表解析

squares=[value**2 for value in range(1,11)]
print(squares)
分析:表达式用于生成存储在列表中的值,for循环用于给表达式提供值,注意for循环句末没有冒号
输出结果:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

2.6使用列表的一部分-切片

players=['charles','martina','michael','florence','eli']
print(players[0:3])  打印第一个元素到第三个元素
输出结果:
['charles', 'martina', 'michael']

print(players[:4])  --如果没有指定索引,python自动从列表开始,

print(players[2:])  --提取从第三个元素到列表末尾元素的所有元素

print(players[-3:])  --输出列表上最后三名运动员

输出结果:

['michael', 'florence', 'eli']

2.7遍历切片

players=['charles','martina','michael','florence','eli']
for player in players[:3]:
    print(player)

2.8复制列表:根据既有列表创建全新的列表

方法一:
players=['charles','martina','michael','florence','eli']
other_players=players[:3]
for player in players[:]:
print(players)

for other_player in other_players:
print(other_players)
输出结果:
['charles', 'martina', 'michael', 'florence', 'eli']
['charles', 'martina', 'michael']

方法二
players=['charles','martina','michael','florence','eli']
other_players=players

players.append('cream')
other_players.append('cannoli')
print('local players :')
print(players)
print('other place players')
print(other_players)
输出结果:
local players :
['charles', 'martina', 'michael', 'florence', 'eli', 'cream', 'cannoli']
other place players
['charles', 'martina', 'michael', 'florence', 'eli', 'cream', 'cannoli']


 四、操作集合

由于dict也是一个集合,所以,遍历dict和遍历list类似,都可以通过 for 循环实现。

请用 for 循环遍历如下的dict,打印出 name: score 来。
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }

d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 } for k in d: print k + ":" + str(d[k])
原文地址:https://www.cnblogs.com/tanxiaojun/p/10507880.html