参考书:《Python编程:从入门到实践》

还有其他

Chapter01

print

print(a,b,sep="|")

sep规定输出间的间隔

print(“Python\n\tniubi”)

\n换行,\t制表符

文件操作:

f=open('D:\main.java','w')
print('hello, world.',file=f)
f.close()

关闭文件流,以写入文件,或:

f=open('D:\main.java','w')
print('hello, world.',file=f,flush=true)

字符串操作

1.大小写调整:

name='ada lovelace'
print(name.title()) #每个单词首字母大写
print(name.upper()) #全大写
print(name.lower()) #全小写

2.字符串组合与分割

‘Ada’+' '+'Lovelace'

分割:

a='to be or not to be.'
a.split()

组合:

a='python'
b='hi'
a=a.join(b)

3.删除空格:

name=' Python '
print(name.rstrip()) #前
print(name.lstrip()) #后
print(name.rstrip())

4.str()强制类型转换

a='happy'
b='birthday'
c=17
message=a+b+str(c)

数字操作

a=2
b=4
print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a**b)

Chapter02

1.列表的调取

a=['nan','bei','dong','xi']
print(a[1].title)

修改

a[0]='zhong'

2.添加元素

append尾加

a.append('hell')

insert插入

a.insert(0,'heaven')

3.删除元素

del删除索引元素

del a[1]

pop删除末尾元素

poped_a=a.pop()
print(a)
print(poped_a)

pop()索引元素

poped_a=a.pop(3)
print(a)
print(poped_a)

remove()

a=['nan','bei','dong','xi']
a.remove('nan')

4.组织列表

sort()永久性排序

books=['Python','cookbook','the reader','me before you']
books.sort()
print(books)
books.sort(reverse=ture)
print(books)

sorted()临时排序

books=['Python','cookbook','the reader','me before you']
print(sorted(books))
print(books)

列表反相

books.reverse()

确定列表长度

books_len=len(books)

反向索引

re_books=books[-1]

列表加与乘

list1=[1,2,3,4]
list2=[4.5.6.7]
list3=list1+list2
list4=list1*3

序列封包与序列解包

#序列封包
x=1,2,3
#x输出为元组 #序列解包
li = ["yjj",12,['yjj',3]]
a,b,c = li a = ["",15,8,4,6]
c,b,*d = a a,*b,c = range(10)
#* 承担剩余元素

Chapter04 操作列表

01.遍历整个列表

遍历

books=['Python','cookbook','the reader','me before you']
for book in books:
print(book)

03.创建数值列表

range()

for value in range(1,6):
print(value)
#1-5,后续可以添加间隔eg:range(1,20,2)

创建列表

list1=list(range(1,6))

min,max,sum 简单统计计算

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
digits_min=min(digits)
digits_max=max(digits)
digits_sum=sum(digits)

列表解析

#eg.1 1-10的平方数列
squares = []
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
#
squares = [value**2 for value in range(1,11)]
print(squares)

4.4 使用列表的一部分

切片

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(digits[0:3])

切片同样可以应用于循环

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
for digit_0 in digits[0:3]:
print(digit_0)

4.5 元组

与列表类似,但不能直接修改,但是可以给变量赋值:

dimensions = (200, 50)
for dimension in dimensions:
print(dimension)
#
dimensions = (400, 50)
for dimension in dimensions:
print(dimension)

Chapter05 if

条件设置

#多个条件并行 和
age_0 >= 21 and age_1 >= 21
#多个条件并行 或
age_0 >= 21 or age_1 >= 21
#参数是否在特定序列
'pepperoni' in requested_toppings

列表处理

if user not in banned_users:
print(user.title() + ", you can post a response if you wish.")

Chapter06 字典

字典的定义与输出

alien_0 = {'color': 'green', 'points': 5}
print(alien_0['color'])
print(alien_0['points'])

添加值

alien_0 = {'color': 'green', 'points': 5}

print(alien_0)

alien_0['x_position'] = 0
alien_0['y_position'] = 25 print(alien_0) #{'color': 'green', 'points': 5, 'y_position': 25, 'x_position': 0}

新字典,修改值

alien_0 = {}

alien_0 = {'color': 'green'}
print("The alien is " + alien_0['color'] + ".")
alien_0['color'] = 'yellow'
print("The alien is now " + alien_0['color'] + ".")
#根据键值

例子,alien

alien_0 = {'x_position': 0, 'y_position': 25, 'speed': 'medium'}
print("Original x-position: " + str(alien_0['x_position']))
# 向右移动外星人
# 据外星人当前速度决定将其移动多远
if alien_0['speed'] == 'slow':
x_increment = 1
elif alien_0['speed'] == 'medium':
x_increment = 2
else:
# 这个外星人的速度一定很快
x_increment = 3
# 新位置等于老位置加上增量
alien_0['x_position'] = alien_0['x_position'] + x_increment
print("New x-position: " + str(alien_0['x_position']))

删除键-值对

alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
del alien_0['points']
print(alien_0)

遍历字典

user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for key, value in user_0.items():
print("\nKey: " + key)
print("Value: " + value)
#定义两个变量用来存贮键与值

嵌套:暂略6.4.2

Python学习笔记(01)的更多相关文章

  1. Python 学习笔记01

      print:直接输出 type,求类型 数据类型:字符串,整型,浮点型,Bool型     note01.py # python learning note 01   print('Hello w ...

  2. 【python学习笔记01】python的数据类型

    python的基本数据类型 整型 int 浮点型 float 真值 bool 字符串 str 列表 list       #[1,2,3] 元组 tuple    #(1,2,3) 字典 dict   ...

  3. Python 学习笔记01篇

    编程基础很零碎 看了路飞学城的讲解视频,整体课程列表排列很清晰,每个视频都在十几分钟到二十几分钟之间,适合零碎化的的学习 第一章和第二章的前半部分可以较轻松地入门

  4. python学习笔记01 --------------hello world 与变量。

    1.第一个程序: print('hello world') 输出结果: hello world 2.变量 2.1 变量的作用: 把程序运算的中间结果临时存到内存里,以备后面的代码继续调用. 2.2 变 ...

  5. python学习笔记01:安装python

    下载python: 从从https://www.python.org/downloads/下载python,根据操作系统的不同,选择不同的版本下载.注意:linux系统大多预装了python,可以直接 ...

  6. 数据类型与变量(Python学习笔记01)

    数据类型与变量 Python 中的主要数据类型有 int(整数)/float(浮点数).字符串.布尔值.None.列表.元组.字典.集合等. None 每个语言都有一个专门的词来表示空,例如 Java ...

  7. 【9-15】python学习笔记01

    使用#开启行注释: 命令行:使用ctrl+d 退出

  8. OpenCV之Python学习笔记

    OpenCV之Python学习笔记 直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看 到一本国外的新书< ...

  9. Python学习笔记(十)

    Python学习笔记(十): 装饰器的应用 列表生成式 生成器 迭代器 模块:time,random 1. 装饰器的应用-登陆练习 login_status = False # 定义登陆状态 def ...

  10. Deep learning with Python 学习笔记(10)

    生成式深度学习 机器学习模型能够对图像.音乐和故事的统计潜在空间(latent space)进行学习,然后从这个空间中采样(sample),创造出与模型在训练数据中所见到的艺术作品具有相似特征的新作品 ...

随机推荐

  1. 在 linux 中连接 mysql 数据库

    命令格式 mysql -h主机地址 -u用户名 -p用户密码 登录本机 mysql mysql -u用户名 -p用户密码 实例 TD - X1数据库:/opt/lampp/bin/mysql -u r ...

  2. memcached和redis对比

    关于memcached和redis的使用场景,总结如下:两者对比: redis提供数据持久化功能,memcached无持久化. redis的数据结构比memcached要丰富,能完成场景以外的事情: ...

  3. Web API和Web Service

    首先,Web API是由Web Service演变而来,它们两者关系就是所有Web Service都是API,但并非所有API都是Web Service.其次,两者都有利于信息的传输,但Web API ...

  4. 这是一篇通过open live writer发布的博文

    这两天零零总总的尝试了两三款写博客的软件,总感觉不怎么上手,最后还是使用博客园官方推荐的工具写博吧,简单方便,目前的功能基本都有,尤其是粘贴图片特别方便,回想之前的几篇博文,真是一种煎熬哈哈(对于我这 ...

  5. selenium webdriver 登录百度

    public class BaiduTest { private WebDriver driver; private String baseUrl; private StringBuffer veri ...

  6. 1059 Prime Factors (25分)

    1059 Prime Factors (25分) 1. 题目 2. 思路 先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqr ...

  7. 「hdu 4845 」拯救大兵瑞恩 [CTSC 1999](状态压缩bfs & 分层图思想)

    首先关于分层图思想详见2004的这个论文 https://wenku.baidu.com/view/dc57f205cc175527072208ad.html 这道题可以用状态压缩,我们对于每一把钥匙 ...

  8. 免费天气API,天气JSON API,不限次数获取十五天的天气预报

    紧急情况说明: 禁用IP列表: 39.104.69.*(原因39.104.69.6 在2018年10月的 17~20日 排行为top 1,每天几十万次.) 47.98.211.* (原因47.98.2 ...

  9. springboot项目入门解析

                     config:主要用来存储配置文件,以及其他不怎么动用的信息. controller:项目的主要控制文件 dao:           主要用来操作数据库 entit ...

  10. MyBatis(6)——分页的实现

    分页的实现 a)通过mysql的分页查询语句: 说明:sql的分页语句格式为select * from aaa limit #{startIndex},#{pageSize} //---------- ...