python 学习 training 个人笔记

本文深入探讨了Python编程的高效文件处理方法,并通过具体示例展示了如何使用Python进行字符串处理、安装与运行Python脚本,以及基本的条件判断、循环结构与格式化输出。此外,还详细介绍了Python中数据类型、列表操作、字典类应用以及文件操作的基本步骤,旨在为读者提供全面的Python编程实践指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2014年7月9日 09:23:59

python  优点:处理文件快

based on python R2.7.5

www.python.org

readability counts

更多处理字符串类型

安装 
uhuntu: apt-get install python
centos:  yum install python

my first python demo: 1.py
#!/user/bin/python

import os,sys
def calcs(first,sec,third):
	result=first+sec+third
	print result
def main():
	file="./1.txt"
	f=open(file,'r')
	str=f.readlines()
	str2=str[2].split(':')[0]
	str3=str[2].split(':')[1]
	print 'str2 :'+str2+'\n'+'str3:'+str3
	
if __name__=='__main__':
	main()

import sys
import os
类似于c中include头文件


Data types:
1代码格式对齐,保持统一缩进格式。
2一行多个语句,分号分割
3反斜杠\,扩展到下一行
4关键字
function type() to check the type of the data

in Terminal:
>>> str = 'hello,world'
>>> str
'hello,world'
>>> str[2:]
'llo,world'
[]注意左闭右开,[a,b]    a=<result<b


list1=['physics','chemistry',1997,2000];

cpm(list1,list2)
len(list)
max(list)   
min(list)
list(seq)   convert tuple to list

exit()退出

list.count(字符)不行?
可以,list[1].count('o')
dictionary 类似于 hashmap

dict_a={'1':['A','X'],2:'STR1'}
demo:
>>> dict_a={'1':['A','X'],2:'STR1'}
>>> dict_a.keys()
['1', 2]
>>> dict_a[2]
'STR1'

str(dict_a)  : convert the dictionary into str,类似于数据类型强制转换。

how to run py file:
python /root/a.py





2014年7月9日 10:40:12
if expression:
     fun1();
else:
    fun2();


for loop
for target in expression_list:
        suite
else:
        suite

format();

1.func中的参数,11对应
2.位置互换
3.其中某个参数带默认值


file operations
open
f=open(file,'r')
str_obj=f.read()
f.close()
练习!



str_obj=f.readline()
str_obj=f.readlines()


with open()  as fd;


str='hellw_world'
str.split('_')[1]


python -m pdb: debug
c: next
b: breakpoint
l: show context
s: step into the function
n: next statment

second method of pdb:
import pdb
pdb.set_trace() 自动加断点,when the py project is too large






评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值