Python学习笔记(数据结构和面向对象)(1)

Python数据结构

Python数据结构有:数字、字符串、列表、元组、字典
1.数字:Python Number 数据类型用于存储数值,包括整型、长整型、浮点型、复数。
 (1)Python math 模块:Python 中数学运算常用的函数基本都在 math 模块

代码:
import math
print(math.ceil(4.1)) #返回数字的上入整数
print(math.floor(4.9)) #返回数字的下舍整数
print(math.fabs(-10)) #返回数字的绝对值
print(math.sqrt(9)) #返回数字的平方根
print(math.exp(1)) #返回e的x次幂

结果:
5
4
10.0
3.0
2.718281828459045

 (2)Python随机数
   首先import random,使用random()方法即可随机生成一个[0,1)范围内的实数。调用 **random.random() ** 生成随机数时,每一次生成的数都是随机的。但是,当预先使用 **random.seed(x) ** 设定好种子之后,其中的 x 可以是任意数字,此时使用 random() 生成的随机数将会是同一个。

print ("------- 设置种子 seed -------")
random.seed(10)
print ("Random number with seed 10 : ", random.random())
#生成同一个随机数
random.seed(10)
print ("Random number with seed 10 : ", random.random())

------- 设置种子 seed -------
Random number with seed 10 : 0.5714025946899135
Random number with seed 10 : 0.5714025946899135

randint()生成一个随机整数

ran = random.randint(1,20)
print(ran)

结果:14

2.字符串
  (1)字符串连接:+
  (2)重复输出字符串:*
  (3)通过索引获取字符串中字符[]
  (4)字符串截取[:] 牢记:左闭右开

a = "Hello "
print(a[1:4])

结果:
ell

  (5)判断字符串中是否包含给定的字符: in, not in
  (6)join():以字符作为分隔符,将字符串中所有的元素合并为一个新的字符串

new_str = ‘-’.join(‘Hello’)
print(new_str)

H-e-l-l-o

  (7)转义字符 \

print(“The \t is a tab”)
print(‘I’m going to the movies’)

The    is a tab
I’m going to the movies

  (8)三引号:三引号让程序员从引号和特殊字符串的泥潭里面解脱出来,自始至终保持一小块字符串的格式是所谓的WYSIWYG(所见即所得)格式的。

print(’’‘I’m going to the movies’’’)

结果:
I’m going to the movies

3.列表:类似于其他语言中的数组
   (1)声明一个列表,并通过下标或索引获取元素
Alt

#声明一个列表
names = [‘jack’,‘tom’,‘tonney’,‘superman’,‘jay’]

#通过下标或索引获取元素
print(names[0])
print(names[1])

#获取最后一个元素
print(names[-1])
print(names[len(names)-1])

#获取第一个元素
print(names[-5])

#遍历列表,获取元素
for name in names:
print(name)

结果
jack
tom
tonney
superman
jay

#查询names里面有没有superman
直接遍历查询
for name in names:
if name == ‘superman’:
print(‘有超人’)
break
else:
print(‘没有超人’)

使用in/not in语句
if ‘superman’ in names:
print(‘有超人’)
else:
print(‘有超人’)

(2)列表元素添加

#声明一个空列表
girls = []
#append(),末尾追加
girls.append(‘杨超越’)
print(girls)

结果:[‘杨超越’]

#extend(),一次添加多个。把一个列表添加到另一个列表 ,列表合并。
models = [‘刘雯’,‘奚梦瑶’]
girls.extend(models)
#girls = girls + models
print(girls)

结果:[‘杨超越’, ‘刘雯’, ‘奚梦瑶’]

#insert():指定位置添加
girls.insert(1,‘虞书欣’)
print(girls)

结果:[‘杨超越’, ‘虞书欣’, ‘刘雯’, ‘奚梦瑶’]

(3)列表元素修改

列表元素修改,通过下标找到元素,然后用=赋值
fruits = [‘apple’,‘pear’,‘香蕉’,‘pineapple’,‘草莓’]
print(fruits)
fruits[-1] = ‘strawberry’
print(fruits)

结果:
[‘apple’, ‘pear’, ‘香蕉’, ‘pineapple’, ‘草莓’]
[‘apple’, ‘pear’, ‘香蕉’, ‘pineapple’, ‘strawberry’]

(4)列表元素删除:del、remove、pop

words = [‘cat’,‘hello’,‘pen’,‘pencil’,‘ruler’]
del words[1] 或 words.remove(‘cat’) 或 words.pop(1)
print(words)

结果:
[‘cat’, ‘pen’, ‘pencil’, ‘ruler’]

(5)列表切片:
  在Python中处理列表的部分元素,称之为切片。
  创建切片,可指定要使用的第一个元素和最后一个元素的索引。注意:左闭右开。
  将截取的结果再次存放在一个列表中,所以还是返回列表
(6)列表排序:随机生成10个不同的整数,并进行排序

import random
random_list = []
for i in range(10):
  ran = random.randint(1,20)
  if ran not in random_list:
    random_list.append(ran)
print(random_list)

结果:如果生成随机数的过程中有重复的数,那可能生成不到10个数。

import random
random_list = []
i = 0
while i < 10:
  ran = random.randint(1,20)
  if ran not in random_list:
     random_list.append(ran)
     i+=1
print(random_list)>

结果:只有生成的随机数有效时,该数才能被计数,因而可以保证产生10个随机数。

产生默认升序:

#默认升序
new_list = sorted(random_list)
print(new_list)
#降序
new_list = sorted(random_list,reverse =True)
print(new_list)>

4.元组:与列表类似,元祖中的内容不可修改
(1)注意:元组中只有一个元素时,需要在后面加逗号!

tuple1 = ()
print(type(tuple1))
结果:<class ‘tuple’>

tuple2 = (‘hello’)
print(type(tuple2))
结果:<class ‘str’>

tuple3 = (‘hello’,)
print(type(tuple3))
结果:<class ‘tuple’>

(2)元组存放元素:

import random
random_list = []
for i in range(10):
  ran = random.randint(1,20)
  random_list.append(ran)
print(random_list)
random_tuple = tuple(random_list)
print(random_tuple)

结果:
[14, 10, 9, 15, 6, 10, 12, 5, 15, 8]
(14, 10, 9, 15, 6, 10, 12, 5, 15, 8)

(3)元组访问:

print(random_tuple)
print(random_tuple[0])
print(random_tuple[-1])
print(random_tuple[1:-3])
print(random_tuple[::-1])

结果:
(14, 10, 9, 15, 6, 10, 12, 5, 15, 8)
14
8
(10, 9, 15, 6, 10, 12)
(8, 15, 5, 12, 10, 6, 15, 9, 10, 14) random_tuple[::-1]中第三个参数表示步长,步长为-1表示从后往前取。>

(4)元组的修改:

t1 = (1,2,3)+(4,5)
print(t1)

结果:(1, 2, 3, 4, 5)>

t2 = (1,2) * 2
print(t2)

结果:(1, 2, 1, 2)>

 元组的一些函数:

print(max(random_tuple))
print(min(random_tuple))
print(sum(random_tuple))
print(len(random_tuple))

结果:
15
5
104
10

(5)元组的拆包与装包

#定义一个元组
t3 = (1,2,3)
#将元组赋值给变量a,b,c
a,b,c = t3
#打印a,b,c
print(a,b,c)

#当元组中元素个数与变量个数不一致时
#定义一个元组,包含5个元素
t4 = (1,2,3,4,5)
#将t4[0],t4[1]分别赋值给a,b;其余的元素装包(成列表)后赋值给c
a,b,*c = t4
print(a,b,c)
print( c)
print(*c)

结果:
1 2 [3, 4, 5]
[3, 4, 5]
3 4 5

5.字典
(1)定义一个字典:

dict1 = {}
dict2 = {‘name’:‘杨超越’,‘weight’:45,‘age’:25}
print(dict2[‘name’])

结果:
杨超越

(2)列表转换为字典:前提是列表中元素都要成对出现

dict3 = dict([(‘name’,‘杨超越’),(‘weight’,45)])
print(dict3)

结果:
{‘name’: ‘杨超越’, ‘weight’: 45}

dict4 = {}
dict4[‘name’] = ‘虞书欣’
dict4[‘weight’] = 43
print(dict4)

结果:
{‘name’: ‘虞书欣’, ‘weight’: 43}

(3)修改字典中的值

dict4[‘weight’] = 44
print(dict4)

结果:
{‘name’: ‘虞书欣’, ‘weight’: 44}

Python面向对象

定义一个类Animals:
(1)init()定义构造函数,与其他面向对象语言不同的是,Python语言中,会明确地把代表自身实例的self作为第一个参数传入
(2)创建一个实例化对象 cat,init()方法接收参数
(3)使用点号 . 来访问对象的属性。

class Animal:
 def init(self,name):
   self.name = name
   print(‘动物名称实例化’)
 def eat(self):
   print(self.name +‘要吃东西啦!’)
 def drink(self):
 print(self.name +‘要喝水啦!’)

cat = Animal(‘miaomiao’)
print(cat.name)
cat.eat()
cat.drink()

结果:动物名称实例化
    miaomiao
    miaomiao要吃东西啦!
    miaomiao要喝水啦!

class Person:
 def init(self,name):
  self.name = name
  print (‘调用父类构造函数’)
 def eat(self):
  print(‘调用父类方法’)

class Student(Person): # 定义子类
 def init(self):
  print (‘调用子类构造方法’)
 def study(self):
  print(‘调用子类方法’)
 s = Student() # 实例化子类
 s.study() # 调用子类的方法
 s.eat() # 调用父类方法

结果:
调用子类构造方法
调用子类方法
调用父类方法

Ps:点击进入学习的课程

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值