不知不觉的时候,距离毕业越来越近。作为一个二本院校,而且学霸都算不上的普通学生。在面对毕业和就业的时候,难免会有一些的迷茫。前几天的时候,和单位的老前辈聊了聊,然后,有一种豁然开朗的感觉。周末并无大事,变开始了Python的学习。巩固一下基础。
推荐使用Jupyter NoteBook
目录:
- 序列
- 运算
- 循环
# 序列
s1=(1,23,"das")
print (s1,type(s1))
s2=(1,2,3,4,5)
print(s2,type(s2))
s3=[1,2,3,4]
print(s2,type(s3))
s4=[1,[123.12,123,2,44]]
print(s4,type(s4))
# 序列有两种:元组和list。
# tuple在建立后,各个元素不在变更。而list的元素可以再变更。
((1, 23, 'das'), <type 'tuple'>)
((1, 2, 3, 4, 5), <type 'tuple'>)
((1, 2, 3, 4, 5), <type 'list'>)
([1, [123.12, 123, 2, 44]], <type 'list'>)
# 元素的引用。
# 下标从0开始
print(s1[0])
print(s2[2])
print(s4[1][1])
#list的元素可以变更。
s3[1]=1000
print(s3)
# tuple这样的会报错
#s1[1]=10000
#TypeError: 'tuple' object does not support item assignment
#print(s1)
1
3
123
[1, 1000, 3, 4]
(1, 23, 'das')
# 其他引用方式
# 范围引用: 基本的样式 [下限:上限: 步长]
s5=(1,2,3,4,5,6,7,8,9)
print(s5[1:3])
print(s5[:3])
print(s5[1:])
print(s5[1::2])
print(s5[-1])
(2, 3)
(1, 2, 3)
(2, 3, 4, 5, 6, 7, 8, 9)
(2, 4, 6, 8)
9
# 字符串是特殊的元组
str="youxaingyang"
print(str[2:4])
ux
# 运算
#和其他语言类似,没太多强调的。
print 1+2*5-6
print 3**3
print 10%3 #求余数
print 15/3
5
27
1
5
# 判断
print 9==3
print 9 != 9.0
print 3<3,3<=3
print 9 in [1,2,3]
False
False
False True
False
# 逻辑运算
print True and True,True and False
print True or False
print not True
True False
True
False
# 缩进:四个空格的缩进来表示隶属关系,不能随意缩进
a=1
b=2
if a>b:
print b
print a
1
# 循环
# for循环
for a in s2:
print a
# 这个循环每次从s2中取出一个元素给a,然后输出、
# 注意range的用法。
#这个函数的功能是新建一个表。这个表的元素都是整数,
#从0开始,下一个元素比前一个大1, 直到函数中所写的上限 (不包括该上限本身)
for a in range(2):
print a
1
2
3
4
5
0
1
i=0
while i<10:
print i
i+=1
0
1
2
3
4
5
6
7
8
9
i=3
for i in range(10):
if i==3:
#continue
break
print i
0
1
2