python初学基础知识一
来源:互联网 发布:快手录视频软件 编辑:程序博客网 时间:2024/06/05 16:25
基础知识
变量的基本类型
integer(int):整数 float:浮点数 long:非常大的数,字节较大 string:存储数字、字母、符号 例:"hello2" list:用方括号括住的一组项,中间用括号隔开 [q6,o0",23,2.5]tuple:用圆括号括住的列表,(类似结构体,表示一组特性) (1,2,4),("linda",23,"beijing")dictionary:用花括号括起来的键值列表{'apple':'red','num':25,'address':'beijing'}
变量的存储
a=5
a 即可输出数字5
type():可以使用type()函数显示变量的类型
type(a)
将输出a的类型int
变量的命名
- 变量名不能使用数字开始
- 变量名不能包括特殊的符号,但可以包括下划线 ‘_’
python中的数学运算
基本四则运算:加减乘除Floor除法: // 13//2=6 取模运算: %取反: - 即直接加负号绝对值: abs() 调用函数
求幂: * 即* 2**4=16
控制语句
if -else语句
if a>5: print("a is bigger than 5")else: print("a is no less than 5")
可以使用elif测试更多数据
if score>90: print("Great")elif score>80: print("good")elif score>60 print("quality")else print("unquality")
try-catch语句
使用try-except语句捕捉错误,避免因不确定导致程序崩溃
try: a=10/0 print("run mormal")except: print("failure to run")
字符串
字符串的创建,单引号或者双引号均可以创建字符串.可以使用print 变量输出字符串,print语句输出后将会转到下一行,若继续在本行输出,则在print语句后加‘,’即可。
a='beijing'b="zhengzhou"print a,print b
获取字符串信息的常用函数
.len() 获取字符串的长度.upper() 将字符串小写字母转换为大写.lower() 大写转化为小写.capitalize() 把字符串首字母大学,剩余字母转化为小写.title() 将每个单词首字母大写,剩余字母转化为小写.isdigt() 判断字符串是否全部为数字,输出为True或者False.isalpha() 判断字符串是否全部为字母,输出为True或者False
字符串的连接,使用’+’
a+b将输出 'beijingzhengzhou'a+" "+b将输出 'beijing zhengzhou'
字符串的乘法运算
字符串乘一个整数将会得到连续的几个字符串,字符串乘负数将得到空字符串,字符串乘浮点数将会报错
s='boy'print s*3控制台将输出 boyboyboyboyboy
格式化字符串
用转义’\n’可以将字符串输出为多行, 当要输出单引号,必须使用转义字符即’\”,要输出反斜杠,也要使用转义字符即”\”
s='name:wangdan\nage:28\naddress:zhengzhou'print s结果:name:wangdanage:28address:zhengzhous='name:wangdan\\age:28\\address:zhengzhou'print sname:wangdan\age:28\address:zhengzhongzho
字符串的空格删除
使用strip()函数,他允许我们删除字符串的开头和结尾的所有空格,也可以删除传入函数的字符
s=' book 's1="***book**story***"print s.strip()print s1.strip("*")print s1.lstrip('*')print s.lstrip()print s1.rstrip('*')print s.rstrip控制台输出:bookbook**storybook**story***book ***book**story book
字符串的查找和替换
可以使用.cout()函数查找字符在字符串出现的次数,函数.find()查找字符第一次出现的位置,函数replace(‘str’,’str’)可以用后一个字符串替换前一个字符串,若没有前一个字
串将会返回-1
s='we are the children,we are the boy,we are the future 'print s.count('we')print s.find("we")print s.find('boy')print s.find('you')print s.replace('boy','volunteer')print s控制台输出:3031-1we are the children,we are the volunteer,we are the future we are the children,we are the boy,we are the future
阅读全文
0 0
- python初学基础知识一
- 初学python的一些基础知识
- python 初学一问题
- Python初学(一)
- 初学Python(一)
- 初学Python(一)
- Python初学一
- Python初学(一)
- 初学Python(一)
- 初学python(一)
- Python初学笔记:Python编程基础知识
- 初学 JavaScript–基础知识笔记(一)
- Python基础知识(一)
- Python基础知识(一)
- python基础知识(一)
- Python (一)基础知识
- Python基础知识(一)
- Python 学习一、基础知识
- 欢迎使用CSDN-markdown编辑器
- CentOS搭建Jenkins环境
- HDU 1233-还是畅通工程
- Integer自动装箱的细节
- 排序算法
- python初学基础知识一
- hibernate 标注非数据库字段
- svn--版本库
- Javamail使用IMAP同步QQ自定义文件夹问题
- Go实战--golang上传文件到七牛云对象存储(github.com/qiniu/api.v7)
- Idea maven远程调试(pom配置tomcat)
- 2014-3-15-高级打字机type(可持久化线段树)
- [encode]vim中文乱码
- 线程之间的通信(生产者和消费者问题)