[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中的保留字段如下:
简单的打印
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"))
强制转换类型
操作符
+ - * / % // == != < > <= => = += -= *= /= %= **= //= & | ^ ~(补码) >> << 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
- python学习笔记1
- python学习笔记(1)
- python学习笔记(1)
- Python学习笔记1
- Python学习笔记1
- Python 学习笔记(1)
- Python学习笔记1
- python学习笔记【1】
- Python学习笔记【1】
- python学习笔记--1
- python学习笔记1
- Python学习笔记[1]
- python学习笔记--1
- python学习笔记1
- Python学习笔记 - 1
- 学习Python笔记1
- Python学习笔记(1)
- Python学习笔记.1
- Simple Tutorial on SVM and Parameter Tuning in Python and R
- aop和注解将权限和操作日志与业务解耦
- 在framework中添加字符串资源
- 爬虫-漫画喵的100行逆袭
- Activity生命周期
- [Python]学习笔记(1)
- java Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
- C# BASE64中文乱码问题
- 深入简出mysql
- 基于C++和OpenCv的SIFT_图像局部特征检测算法代码的实现
- 496. Next Greater Element I
- Android应用层修改LED指示灯
- 掉过的坑
- STM32F4-UCOSIII-1移植