Python的入门要点

时间:2023-03-09 18:36:54
Python的入门要点

一、输入

1、键盘输入

  在python 2.7中,不用input(),而用 raw_input()读入一行键盘输入,并转化为字符串。

  s = map(int ,raw_input().split()) 输入编程数据链表

2、文件输入与输出

http://www.cnblogs.com/allenblogs/archive/2010/09/13/1824842.html

  打开文件 file_object = open('data', 'r') //r 文本文件,rb二进制文件,w写文本,wb写二进制,w+追加

   读取所有内容 all_the_text = file_object.read( )

  读固定字节 chunk = file_object.read(100)

  读每行 list_of_all_the_lines = file_object.readlines( )

  如果文件是文本文件,还可以直接遍历文件对象获取每行:

  for line in file_object:
       process line

  写文件 file_object.write(all_the_text)
  写入多行 file_object.writelines(list_of_text_strings)

  关闭文件 file_object.close( )

二、输出

  字符串形式输出 print "%.3s " % ("jcodeer")

  格式化形式输出 'User ID: {uid} Last seen: {last_login}' .format( uid='root', last_login = '5 Mar 2008 07:20')

三、内部数据类型

  原子数据类型: bool(True)intlongfloat 和 complex

  集合数据类型:   

  1. Tuples[元组] 是有序而不可变的值序列。
  2. Lists[列表] 是值的有序序列。
  3. Sets[集合] 是装满无序值的包裹。
  4. Dictionaries[字典] 是键值对的无序包裹。
  5. Strings[字符串型] 是 Unicode 字符序列,例如: 一份 html 文档。

  数据类型转换:

    ord()    将字符转换成ASCII
    chr()    将ASCII转换成字符

四、语法格式

1、不同级别不同缩进

2、凡代码块用 : 来分隔

3、函数定义 def fun(n,m,...)

4、主函数

   if __name__=='__main__':

5、逻辑运算符: not and or

五、正则表达式

print "%.3s " % ("jcodeer")