[Python]学习笔记(1)

来源:互联网 发布:暴风影音mac官方下载 编辑:程序博客网 时间:2024/05/29 12:10

格式

Python程序在最开头需要注明python的路径

#!/usr/bin/python3   

导入包

Python需要引入使用到的模块,格式如下:

import sys import mathimport randomimport module1[, module2[,... moduleN]from modname import name1[, name2[, ... nameN]]

python的保留字段

python中的保留字段如下:

and exec not assert finally or break for pass class from print continue global def if return del import try elif in while else is with except lambda yield

简单的打印

print("我是打印的语句")

python语句

  is_true=True   //这里注意python的boolean值是True和Flase  if(is_true):    print("is_true value is true!")//这里注意python代码分块是通过代码行的缩进区分的,并且缩进必须一样;并且注意没有';'  else:    print("is_true value is false!")  is_true=False  if(is_true):    print("is_true value is true!")  else:    print("is_true value is false!")

python中的文本操作

  file_name="test.txt"//文件名  try:    file=open(file_name,"w")//打开文件  except IOError:    print("error open file:",file_name)    sys.exit()  file_finished="s"  print("Enter: ",file_finished,"when file finished!")  file_text=""  while(file_text!=file_finished):    file_text=input("Enter text:")//读取键盘输入    if(file_text==file_finished):      file.close      break    file.write(file_text)//写入内容    file.write("\n")  file.close()  file_name=input("Enter file name:")  if(len(file_name)==0):    print("Input nothing,exit")    sys.exit()  try:    file=open(file_name,"r")  except IOError:    print("error open file,name is:",file_name)    sys.exit()  file_text=file.read()//读取文件内容  file.close()  print(file_text)

python中的字符串

  //字符串的区分通过 ''    ""   """来区分,注意的是:一定要成对;长字符串的分行可以通过'\'来区分  text_multi_line="112"\  +"33"\  +"44"  print(text_multi_line)  print('\a'+r'\n') \\r''表示不转义

多行代码写在一行

代码也可以一行写多个语句,但是要通过;来区分

  text_input=input("\n\nname:");print("input name is:",text_input)

变量

对于变量的声明可以批量声明.

  a=b=c=1  e,f,g=1,2,"123dd"  #print(a,b,c,e,f,g)

关于字符串的截断输出

  text_output="abcdefghijklmn"  print(text_output*2)  print(text_output[0])  print(text_output[2:6])  print(text_output[3:])  print(text_output+"___new characters")

字典,list,tuple类型

  list_test=[1,2,3,4,6,'g','h','j'] //list  tuple_test=(1,2,3,'f','g','h','t','h') //tuple,注意tuple内容不能修改元素的值  dict_test={}  dict_test["a"]=123  dict_test["b"]=456  print(list_test,tuple_test)  print(list_test[2])  print(dict_test["a"],dict_test["b"],dict_test)  print(repr(list_test))  print(eval("13"))

强制转换类型

Function Description int(x [,base]) 强制x转换为整型 long(x [,base] ) 强制x转换为长整型 float(x) 强制x转换为浮点型 complex(real [,imag]) 强制x转换为复数 str(x) 强制x转换为字符型 repr(x) 强制x转换为表达式 eval(str) 强制str转换为指定对象 tuple(s) 强制x转换为tuple list(s) 强制x转换为list set(s) 强制x转换为set类型 dict(d) 强制d转换为字典型 frozenset(s) 强制s转换frozen set. chr(x) 强制将整型转换为字符型 unichr(x) 强制将整型转换为unicode类型 ord(x) 强制将单字符转换为整型 hex(x) 将整型转换为16进制字符串 oct(x) 将整型转换为8进制字符串

操作符

+ - * / % // == != < > <= =>    = += -= *= /= %= **= //= & | ^ ~(补码) >> <<  in  not in  is is not     //注意没有自增和自减  print(2**2)#指数  print(12//5)#floor division

pass语句

  for letter in 'Python':    pass  //do noting    print('Current Letter :', letter)  length=len('Python')  i=0  while(i<length):    print("print i:",i)    i+=1

Python 支持的数据类型

包括整型,长整型,浮点型,复杂类型(复数)

0 0
原创粉丝点击