Py学习  »  Python

第二次学习记录(Python)

AKITOMOKO • 4 年前 • 131 次点击  

学习目标

Python网络爬虫


学习目标:

学习网络爬虫

学习内容:

1.元组 2.字典 3.条件语句 4.循环语句 5.函数 6.类

学习时间:

周三(20:00-22:00) 周四(20:00-22:00) 周五(20:00-22:00)

学习笔记:

  1. 元组与列表类似,不同之处在于元组的元素不能修改。元组写在圆括号()中。
tup1=('Google','test',1997,2000)
print(tup1)
('Google','test',1997,2000)
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当成运算符使用。(?测试一下没有这个问题)

tup1=('Google','test',1997,2000)
tup2=(1,2,3,4,5,6,7)
print("tup1[0]:",tup1[0])
print("tup2[1:5]:",tup2[1:5])
tup1[0]:Google
tup2[1:5]:(2,3,4,5)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

元组一经定义是不能修改和删除的,所以在使用元组时一定要慎重。

  1. 字典是一个可变容器模型,且可存储任意类型的对象,用{}标识。字典是一个无序的键(key):值(value)对的集合。
    name和age为键,张三和23位值
dic={'name':'张三','age':23}
print(dic)#运行打印
{'name':'张三','age':23}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
dic={'name':'张三','age':23}
dic['sex']='男'
print(dic)#打印增加后的结果
{'name':'张三','age':23,'sex':'男'}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
dic={'name':'张三','age':23}
del dic['age']
print(dic)#打印删除后的结果
{'name':'张三'}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  1. 条件语句
if condition_1:
	statement_block_1
elif condition_2:
	statement_block_2
else:
	statement_block_3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

注意缩进,一般情况下4个空格,条件语句根据缩进来判断语句归属。

  1. 循环语句
    for循环
for <variable> in <sequence>
	<statements>
else:
	<statements>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
for x in range(1,10)
	print(x)
1
2
3
4
5
6
7
8
9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
list=[1,2,3,4]
for x in list:
	print(x)
1
2
3
4
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

while循环

while判断条件:
	语句
  • 1
  • 2
  • 1
  • 2
n=100
sum =0
counter=1
while counter<=n
	sum=sum+counter
	counter+=1
print("1到%d之和为:%d"%(


    
n,sum))
1100之和为:5050
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
var=1
while var==1:#表达式永远为True
	num=int(input("输入一个数字:"))
	print("你输入的数字是:",num)
print("Goodbye!")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

while循环使用else语句

count=0
while count<5:
	print(count,"小于5")
	count=count+1
else:
	print(count,"大于或等于5")
0小于5
1小于5
2小于5
3小于5
4小于5
5大于或等于5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  1. 函数
def 函数体(参数列表)
	函数体
  • 1
  • 2
  • 1
  • 2
def hello()
	print("Hello World!")
hello()
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
#定义函数
def printme(str)
	#打印任何传入的字符串
	print(str)
	return
#调用函数
printme("随便乱写的")
随便乱写的
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
class ClassName:
	<statement-1>
	.
	.


    

	.
	<statement-N>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
class Student():

	def info(self)
		print("测试")
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
class Myclass:
"""一个简单的类实例"""
	i=12345
	def f(self):
		return 'hello world'
#实例化类
x=Myclass()
#访问类的属性和方法
print("Myclass类的属性i为:",x.i)
print("MYclass类的方法f输出为:",x.f())
Myclass类的属性i为:12345
Myclass类的方法f输出为:hello world
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
class Complex:
	def _init_(self,realpart,imagpart):
		self.r=realpart
		self.i=imagpart
	x=Complex(3.0,-4.5)
	print(x.r,x.i)#输出结果:3.0 -4.5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
#类定义
class people:
#定义基本属性
name=''
age=0
#定义私有属性,私有属性在类外部无法直接进行访问
_weight=0
#定义构造方法w
def _init_(self,n,a,w):
	self.name=n
	self.age=a
	self._weight=w
def speak(self):
	print("%s说:我%d岁。"%(self.name,self.age))

#实例
p=people('runoob',10,30)
p.speak()
zhangsan说:10岁。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
#类定义
class people:
#定义基本属性
name=''
age=0
#定义私有属性,私有属性在类外部无法直接进行访问
_weight=0
#定义构造方法w
def _init_(self,n,a,w):
	self.name=n
	self.age=a
	self._weight=w
def speak(self):
	print("%s说:我%d岁。"%(self.name,self.age))

#单继承实例
class student(people):
	grade=''
	def _init_(self,n,a,w,g):
	#调用基类的构造函数
	people._init_(self,n,a,w)
	self.grade=g
	#覆写基类的方法
	def speak(self):
	print("%s说:我%d岁了,我在读%d年级"%(self.name,self.age,self.grade))

s=student('ken',10,60,3)
s.speak
ken说:10岁了,我在读3年级
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/99226
 
131 次点击