python学习笔记(1)
1)** 表示“冥”
2)输入函数 raw_input()
3)字符串操作:>>> pystr='python'
>>> iscool='is cool!'
>>> pystr[0]
'p'
>>> pystr[2:5]
'tho'
>>> iscool[:2]
'is'
>>> iscool[3:]
'cool!'
>>> iscool[-1]
'!'
>>> pystr+iscool
'pythonis cool!'
>>> pystr+' '+iscool
'python is cool!'
>>> pystr*2
'pythonpython'
>>> ‘--------------------’
4)Lists和TuplesLists和Tuples的主要区别在于:Lists用中括号[]包围,其元素和尺寸可以改变;而Tuples用圆括号包围,且不能更新。因此,Tuples可以被认为是只读列表List。
其子集可以用[]和[:]分割,如同字符串那样。
>>> aList = [1,2,3,4]
>>> aList
[1, 2, 3, 4]
>>> aList[0]
1
>>> aList[2:3]
[3]
>>> aList[2:]
[3, 4]
>>> aList[:3]
[1, 2, 3]
>>> aList[1]=5
>>> aList
[1, 5, 3, 4]
>>> aTuple=('robots',77,93,'try')
>>> aTuple
('robots', 77, 93, 'try')
>>> aTuple[0]
'robots'
>>> aTuple[2:]
(93, 'try')
>>> aTuple[:3]
('robots', 77, 93)
>>> aTuple[1]='abc'
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
aTuple[1]='abc'
TypeError: 'tuple' object does not support item assignment
>>> aTuple[1]=5
5)Dictionaries字典Dictionaries是Python的哈希表类型。由键值对组成,键Key可以是任意Python类型,但是值Value只能是数字或字符串。字典封装在尖括号{}内。
>>> aDict={}
>>> aDict['host']='earth'
>>> aDict['port']=80
>>> aDict
{'host': 'earth', 'port': 80}
>>> aDict.keys()
['host', 'port']
>>> aDict['host']
'earth'
6)代码块使用缩进
7)if语句if expression:
if_suite
else:
else_suite
if expression1:
if_suite
elif expression2:
elif_suite
else:
else_suite
while expression:
while_suite
---------
>>> counter=0
>>> while counter<5
SyntaxError: invalid syntax
>>> while counter<5:
print 'loop #%d' %(counter)
counter=counter+1
loop #0
loop #1
loop #2
loop #3
loop #4
8)for循环和内建的range()函数Python的for循环类似于shell脚本的foreach迭代类型的循环。
>>> print 'I like to use the Internet for:'
I like to use the Internet for:
>>> for item in ['e-mail','net-surfing','homework','chat']:
print item
e-mail
net-surfing
homework
chat
---------
Python提供了内建的range()函数,产生一个列表。如下所示:
>>> for eachNum in range(6):
print eachNum;
0
1
2
3
4
5
9)文件和内建的open()函数文件访问是编程语言最重要的功能之一,它也是持久化存储的重要手段。
怎样打开一个文件:
handle = open(file_name, access_mode='r')
例子:
filename = raw_input('Enter file name: ')
file = open(filename, 'r')
allLines = file.readlines()
file.close()
for eachLine in allLines:
print eachLine,
分享到:
相关推荐
python学习笔记1——(廖雪峰教程,菜鸟教程)python基础 python学习笔记1——(廖雪峰教程,菜鸟教程) >>> classmates = ['Michael', 'Bob', 'Tracy'] >>> len(classmates) 3 ⽤索引来访问list中每⼀个位置的元素...
python学习笔记,第一部分,python的安装、初步使用以及系统环境变量的设置方法。是初学python的第一步。以初学者的角度看问题,让初学者能快速入门。
python学习笔记1.docx
Python学习笔记0001 - 安装与配置Python开发环境 Python学习笔记0002 - 海龟画图 - 初画图形 Python学习笔记0003 - 海龟画图 - 变量 Python学习笔记0004 - 海龟画图 - for循环 Python学习笔记0005 - 海龟画图 - ...
Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf Python学习笔记 .pdf ...
第一章python是什么跨平台(平台无关性、可移植)、面向对象、脚本语言组成python解释器、字节码编译(.pyc)、python虚拟机(PVM)第二章pyt
最新Python学习笔记,Day 1,总结性学习笔记,适合初学者归纳总结。
最新Python学习笔记,Day 3,总结性学习笔记,适合初学者归纳总结。
Python基础入门教程,适合Python初学者,文档内容包括, 目录 前言 i 第一章 程序 1 1.1 程序 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 什么是调试 . . . . . . . . . . . . . . . ....
Python学习笔记
Python学习笔记.pdf
1、Python环境搭建 1.1 Python简介 1.2 Python语言的发展历程 1.3 Python语言的主要特点 1.4 Python应用场景 1.5 Python环境搭建 1.6 第一个Python程序 1.7 集成开发工具 2、Python语法基础 2.1 Python中的...
python学习笔记python学习笔记python学习笔记python学习笔记python学习笔记
Python 学习笔记
Python学习笔记--皮大庆,非常适合零基础入门。对应英文版本《How to think like a computer scientist》
最新Python学习笔记,Day 5,总结性学习笔记,适合初学者归纳总结。
python学习笔记一所用文本及图片,一个txt文本、一个slx表格、一个csv表格、一张jpg图片,本内容非学习必须,可以自己制作自己所用资源,发布本文件只是为了对应我的学习笔记。