Python学习笔记:第一次接触
用的是windows的IDLE(python 3)
对象的认识:先创建一个list对象(用方括号)
a = ['xieziyang','chenmanru']
a
对list中对象的引用
a[0] # 表示列表a中的第一个对象
在列表的末尾添加一个对象
a.append('xiemanru')
a.insert(0,'xiemanru1') # 在指定位置插入一个对象insert object before index
移除对象
a.remove('xiemanru')
a
for循环:
movies = ['The Holy Grail','The Life of Brian','The Meaning of Life']
years = [1975,1979,1983]
k = 1
for time in years:
movies.insert(k,time)
k = k+2
movies
在列表中再插入一个列表:
movies.insert(2,['Transformer',2007]
引用:
>>> movies[2][0]
'Transformer'
嵌套列表的打印:
>>> for each_item in movies:
if isinstance(each_item,list): # 判断each_item是否为列表
for nested_item in each_item:
print(nested_item)
else:
print(each_item)
对于多重嵌套列表的打印:再添加一个列表
>>> movies[2].insert(1,['阿甘正传',2010])
>>> movies
['The Holy Grail', 1975, ['Transformer', ['阿甘正传', 2010], 2007], 'The Life of Brian', 1979, 'The Meaning of Life', 1983]
定义函数:
def print_it_all(the_list): # 定义一个函数:def 函数名(参数):函数代码组
for each_item in the_list:
if isinstance(each_item,list):
print_it_all(each_item)
else:
print(each_item)
看看效果:
>>> print_it_all(movies)
The Holy Grail
1975
Transformer
阿甘正传
2010
2007
The Life of Brian
1979
The Meaning of Life
1983
改进一下,打印的更好看:
def print_it_all(the_list,level):
for each_item in the_list:
if isinstance(each_item,list):
print_it_all(each_item,level+1)
else:
for tab_stop in range(level):
print("\t",end = '')
print(each_item)
看一下结果:
print_it_all(movies,0)
The Holy Grail
1975
Transformer
阿甘正传
2010
2007
The Life of Brian
1979
The Meaning of Life
1983
再添加一个参数控制是否缩进打印:
def print_it_all(the_list,indent = False,level = 0):
for each_item in the_list:
if isinstance(each_item,list):
print_it_all(each_item,indent,level+1)
else:
if indent:
for tab_stop in range(level):
print("\t",end = '')
print(each_item)
看看结果怎么样:
>>> print_it_all(movies)
The Holy Grail
1975
Transformer
阿甘正传
2010
2007
The Life of Brian
1979
The Meaning of Life
1983
>>> print_it_all(movies,indent = True)
The Holy Grail
1975
Transformer
阿甘正传
2010
2007
The Life of Brian
1979
The Meaning of Life
1983
看的书是head frist Python,挺适合初学者看的
以上代码也是参照上书内容,敲几遍就变成自己的了:)
欢迎大家来指教
Python学习笔记:第一次接触的更多相关文章
- python学习笔记--Django入门0 安装dangjo
经过这几天的折腾,经历了Django的各种报错,翻译的内容虽然不错,但是与实际的版本有差别,会出现各种奇葩的错误.现在终于找到了解决方法:查看英文原版内容:http://djangobook.com/ ...
- 【python学习笔记】10.充电时刻
[python学习笔记]10.充电时刻 任何python都可以作为模块倒入 *.pyc:平台无关的经过编译的的python文件, 模块在第一次导入到程序中时被执行,包括定义类,函数,变量,执行语句 可 ...
- python学习笔记(一)、列表和元祖
该一系列python学习笔记都是根据<Python基础教程(第3版)>内容所记录整理的 1.通用的序列操作 有几种操作适用于所有序列,包括索引.切片.相加.相乘和成员资格检查.另外,Pyt ...
- 近期Python学习笔记
近期Python 学习笔记--一篇文入门python 作者:Pleiades_Antares(www.cnblogs.com/irischen) 写在前面的话 想学Python已经许久,一年多以前(应 ...
- Deep learning with Python 学习笔记(9)
神经网络模型的优化 使用 Keras 回调函数 使用 model.fit()或 model.fit_generator() 在一个大型数据集上启动数十轮的训练,有点类似于扔一架纸飞机,一开始给它一点推 ...
- Python学习笔记_Python对象
Python学习笔记_Python对象 Python对象 标准类型 其它内建类型 类型对象和type类型对象 Python的Null对象None 标准类型操作符 对象值的比較 对象身份比較 布尔类型 ...
- Python学习笔记之生成器、迭代器和装饰器
这篇文章主要介绍 Python 中几个常用的高级特性,用好这几个特性可以让自己的代码更加 Pythonnic 哦 1.生成器 什么是生成器呢?简单来说,在 Python 中一边循环一边计算的机制称为 ...
- Python学习笔记之模块与包
一.模块 1.模块的概念 模块这一概念很大程度上是为了解决代码的可重用性而出现的,其实这一概念并没有多复杂,简单来说不过是一个后缀为 .py 的 Python 文件而已 例如,我在某个工作中经常需要打 ...
- Python学习笔记之类与对象
这篇文章介绍有关 Python 类中一些常被大家忽略的知识点,帮助大家更全面的掌握 Python 中类的使用技巧 1.与类和对象相关的内置方法 issubclass(class, classinfo) ...
- Python学习笔记之从文件中读取数据
10-1 Python 学习笔记:在文本编辑器中新建一个文件,写几句话来总结一下你至此学到的Python 知识,其中每一行都以“In Python you can”打头.将这个文件命名为learnin ...
随机推荐
- python 有用的库
1.Faker pip3 install faker官网: https://faker.readthedocs.io/en/master/providers.htmlgithub: https://g ...
- 6.redis
1.Redis的安装以及客户端连接 安装:apt-get install redis-server 卸载:apt-get purge --auto-remove redis-server 启动:red ...
- 13 Zabbix4.4.1系统告警“More than 75% used in the configuration cache”
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 13 Zabbix4.4.1系统告警“More than 75% used in the conf ...
- 测开常见面试题什么是redis
企业中redis是必备的性能优化中间件,也是常见面试题,首先Redis是由意大利人Salvatore Sanfilippo(网名:antirez)开发的一款内存高速缓存数据库.Redis全称为:Rem ...
- MVC 事物
前一阵学习mvc但是对于关联表的数据操作总是分裂开来写,这样有很大的侥幸,比如嵌套了3个 if(){ if(){ if(){ } } } 如果前两个都是true那么最后一个是false了,这样是不想看 ...
- Mysql 查看连接数,状态及最大并发数(转载)
-- show variables like '%max_connections%'; 查看最大连接数 set global max_connections=1000 重新设置 mysql> ...
- 使用CreateMetaFile创建WMF文件,并转换为EMF文件
#include <iostream> #include <stdio.h> #include <WINDOWS.H> #include <shellapi. ...
- iOS的navigationbar设置左边按钮文字
实例代码: - (void)viewDidLoad { [super viewDidLoad]; [self setTitle:@"Test"]; //以下是主要实现代码 UIBu ...
- RSA加密算法原理及RES签名算法简介(转载)
第一部分:RSA算法原理与加密解密 一.RSA加密过程简述 A和B进行加密通信时,B首先要生成一对密钥.一个是公钥,给A,B自己持有私钥.A使用B的公钥加密要加密发送的内容,然后B在通过自己的私钥解密 ...
- 对vue-router的研究--------------引用
pushState/replaceState/popstate 解析 HTML5提供了对history栈中内容的操作.通过history.pushState/replaceState实现添加地址到hi ...