Python入门2
字符串操作
字符串是语言中使用最多的,下面我们来看看python为字符串提供哪些方法:
1、upper()、lower()、title() 这3个方法都是返回一个新的字符串。重要性:**
name = 'example_EXAMPLE' print(name.upper()) # 转换为大写
print(name.lower()) # 转换为小写
print(name.title()) # 首字母大写 # 输出 EXAMPLE_EXAMPLE
example_example
Example_Example
2、isdigit()、isalnum()、isspace()、isappha() 返回bool类型。重要性:***
name = 'example_EXAMPLE'
print(name.isdigit()) # 是否是数字
print(name.isalnum()) # 是否是字母和数字
print(name.isspace()) # 是否是空格
print(name.isalpha()) # 是否是字母 # 输出 False
False
False
False
3、startswith()、endswith返回bool类型,判断字符串开始或结束是否为真。重要性:****
name = 'example_EXAMPLE'
print(name.startswith('ex'))
print(name.endswith('LE')) # 输出
True
True
4、split() 这个方法非常重要,它会根据字符串里面制定的字符进行分割,返回列表。 重要性:*****
name = 'tom&jerry'
name_list = name.split('&')
print(name_list[0])
print(name_list[1]) # 输出
tom
jerry
5、join()的方法刚好跟split相反,它会把列表转换成字符串。重要性:*****
fruit_list = ['apple', 'banana', 'pear', 'orange']
new_val = '&'.join(fruit_list)
print(new_val) # 输出
apple&banana&pear&orange
6、strip()、lstrip()、rstrip() 去空格。重要性:**
example = ' abc ' # len = 8
print(example.strip()) # 去掉左右空格
print(len(example.strip())) #
print(example.lstrip()) # 去左边空格
print(len(example.lstrip())) #
print(example.rstrip()) # 去右边空格
print(len(example.rstrip())) #
7、rjust()、ljust()、center() 重要性:**
example = 'abc'
print(example.rjust(20, '*'))
print(example.ljust(20, '#'))
print(example.center(20, '=')) # 输出
*****************abc
abc#################
========abc=========
While循环
while语句是python语言中第二种循环,它和for循环在功能是都是一致的,不同点在于,for循环在迭代完毕后停止,而while循环是在条件不成立的时候停止。有的时候我们可能会遇上一种死循环,例如:
count = 0
while True:
print('hello world ', count)
count += 1
这个语句就是因为while后边的条件为真,所以会一直运行下去,那我们改改,让它的条件在到达5的时候程序停止跳出循环。
count = 0
while count < 5:
print('hello world ', count)
count += 1
其实让while停下来有很多种方式,下面我介绍一个break,这个例子就是让用户输入3次,如果不正确就退出并打印,如果正确就输出ok
count = 0
while count < 3:
inp = input('input your name :') if inp == 'chen':
print('ok')
break
count += 1
else:
print('More times')
通过上边的例子我们稍微修改一下,引入continue看看有什么变化,当用户输入的值为空的时候我们要求他必须输入值,continue会跳出当次循环。
count = 0
while count < 3:
inp = input('input your name :')
if len(inp) == 0:
continue
if inp == 'chen':
print('ok')
break
count += 1
else:
print('More times')
列表和元祖
列表和元祖可以使编程在处理大量数据的时候变得更加容易和灵活,而且,列表和元祖自身可以嵌套,在python中列表以方括号[],元祖是以小括号()来进行放置数据,看起来像这样['apple','banana','pear'],列表或元祖中的值也可以叫元素。它们可以包含多个值,也可以不包含值,例如[]或(),这代表是一个空列表或空元祖,下面我们对是用列表很元祖的一些方法进行举例:
1、用下标进行取值: 此方式适合于列表和元祖
下图展示了一个水果列表,在该列表中有4个值,我们可以通过下标进行取值。列表下标的值是从0可以,所以如果你要取出apple,就需要是用下边0,如fruit_list[0],其余以此类推。

fruit_list = ['apple', 'banana', 'pear', 'orange']
print(fruit_list[0])
print(fruit_list[1])
print(fruit_list[2])
print(fruit_list[3])
注意:
下边只能是整数,不能写成浮点类型
2、负数下标:此方式适合于列表和元祖
虽然下标是从0开始并向上增长,但是有的时候为了方便取值,我们也可以通过负数进行取值,比如
fruit_list = ['apple', 'banana', 'pear', 'orange'] print(fruit_list[-1])
print(fruit_list[-2]) # 输出
orange
pear
3、切片取值:此方式适合于列表和元祖
我们知道字符串可以通过切片进行取值,列表跟字符串取值完全一样,比如
fruit_list = ['apple', 'banana', 'pear', 'orange']
print(fruit_list[:]) # 打印全部的值
print(fruit_list[-1]) # 打印最后一个值
print(fruit_list[1:]) # 打印下标从1到最后的值
print(fruit_list[0:3]) # 打印从apple到pear的值
print(fruit_list[:-1]) # 打印从apple到pear的值
print(fruit_list[::2]) # 打印全部的值,步长为2 # 输出
['apple', 'banana', 'pear', 'orange']
orange
['banana', 'pear', 'orange']
['apple', 'banana', 'pear']
['apple', 'banana', 'pear']
['apple', 'pear']
4、修改列表里面的值:元祖不能是用
fruit_list = ['apple', 'banana', 'pear', 'orange']
fruit_list[1] = 'grape' # 修改banana为grape
print(fruit_list) # 输出
['apple', 'grape', 'pear', 'orange']
5、插入值:元祖不能是用
要在列表中添加新值,有2种方式,一个是insert,一个是append
append是插入列表最后
fruit_list = ['apple', 'banana', 'pear', 'orange']
fruit_list.append('grape')
print(fruit_list) #输出
['apple', 'banana', 'pear', 'orange', 'grape']
insert是插入到下标制定某个地方,比如我要把grape插入到apple后面
fruit_list = ['apple', 'banana', 'pear', 'orange']
fruit_list.insert(1, 'grape')
print(fruit_list) # 输出
['apple', 'grape', 'banana', 'pear', 'orange']
6、删除:元祖不能是用
删除有3种方法,del,pop,remove
del不是列表独有的方法,可以在处理字符串种是用
fruit_list = ['apple', 'banana', 'pear', 'orange']
del fruit_list[0] # 删除apple
print(fruit_list) # 输出
['banana', 'pear', 'orange']
remove
fruit_list = ['apple', 'banana', 'pear', 'orange']
fruit_list.remove('apple') # 删除apple
print(fruit_list)
# 输出
['banana', 'pear', 'orange']
pop需要制定下标并且有返回值
fruit_list = ['apple', 'banana', 'pear', 'orange']
res = fruit_list.pop(0) # 删除apple
print(res)
print(fruit_list) # 输出
apple
['banana', 'pear', 'orange']
7、len()计算列表长度:此方式适合于列表和元祖
fruit_list = ['apple', 'banana', 'pear', 'orange']
res = len(fruit_list)
print(res) # 输出
4
8、列表多重赋值技巧:此方式适合于列表和元祖
多重赋值其实是一种快捷方法,让你在一行代码中,用列表中的值为多个变量赋值,比如:
fruit_list = ['tom', '', 'M']
name, age, sex = fruit_list
print(name, age, sex) # 输出
tom 19 M
下面我们对列表和元祖进行一个简单总结:
1、列表和元祖中的元素是有序的
2、列表种的元素是可变的,而元祖是不可变的
3、列表和元祖都是可嵌套的
Python入门2的更多相关文章
- python入门简介
Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...
- python入门学习课程推荐
最近在学习自动化,学习过程中,越来越发现coding能力的重要性,不会coding,基本不能开展自动化测试(自动化工具只是辅助). 故:痛定思痛,先花2个星期将python基础知识学习后,再进入自动化 ...
- Python运算符,python入门到精通[五]
运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算.例如:2+3,其操作数是2和3,而运算符则是“+”.在计算器语言中运算符大致可以分为5种类型:算术运算符.连接运算符.关系运算符.赋值运 ...
- Python基本语法[二],python入门到精通[四]
在上一篇博客Python基本语法,python入门到精通[二]已经为大家简单介绍了一下python的基本语法,上一篇博客的基本语法只是一个预览版的,目的是让大家对python的基本语法有个大概的了解. ...
- Python基本语法,python入门到精通[二]
在上一篇博客Windows搭建python开发环境,python入门到精通[一]我们已经在自己的windows电脑上搭建好了python的开发环境,这篇博客呢我就开始学习一下Python的基本语法.现 ...
- visual studio 2015 搭建python开发环境,python入门到精通[三]
在上一篇博客Windows搭建python开发环境,python入门到精通[一]很多园友提到希望使用visual studio 2013/visual studio 2015 python做demo, ...
- python入门教程链接
python安装 选择 2.7及以上版本 linux: 一般都自带 windows: https://www.python.org/downloads/windows/ mac os: https:/ ...
- Python学习【第二篇】Python入门
Python入门 Hello World程序 在linux下创建一个叫hello.py,并输入 print("Hello World!") 然后执行命令:python hello. ...
- python入门练习题1
常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...
- Python入门版
一.前言 陆陆续续学习Python已经近半年时间了,感觉到Python的强大之外,也深刻体会到Python的艺术.哲学.曾经的约定,到现在才兑现,其中不乏有很多懈怠,狼狈. Python入门关于Pyt ...
随机推荐
- 闲扯游戏编程之html5篇--山寨版《flappy bird》源码
新年新气象,最近事情不多,继续闲暇学习记点随笔,欢迎拍砖.之前的〈简单游戏学编程语言python篇〉写的比较幼稚和粗糙,且告一段落.开启新的一篇关于javascript+html5的从零开始的学习.仍 ...
- Java的多态
多态的定义: 同一种行为,在不同对象上有不同的表现形式 实现多态的条件: 要有继承 要有方法的重写 要有父类的引用指向子类的对象 代码如下: public class Animal { String ...
- C++深拷贝与浅拷贝
当用一个已初始化过了的自定义类类型对象去初始化另一个新构造的对象的时候,拷贝构造函数就会被自动调用.也就是说,当类的对象需要拷贝时,拷贝构造函数将会被调用.以下情况都会调用拷贝构造函数: (1)一个对 ...
- Back to openGL!
#include <iostream> #include <windows.h> #include <gl/glut.h> #include <math.h& ...
- C#正则表达式匹配字符串
正则表达式可以快速判断所给字符串是否某种指定格式.这里将一些常用的方法封装进一个字符串工具类中. public static class StringTool { /// <summary> ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确。此 RPC 请求中提供了过多的参数。最多应为 2100
出现这个问题的背景是,判断一批激活码在系统中是否已经存在,很傻的一个作法是,把这一批激活码,以in(in (‘ddd‘,‘aaa‘))的形式来处理,导致问题的出现. 后来,查找资料,http://bb ...
- Cruehead.1
查壳 没有 我拖 alt+F9 到上面 入口处 下断 关键跳 略过 就没了 要实现 强暴 直接过... 仔细来看看... 那两个调用 都下断 看看 判断 ...
- mybatis调用视图和存储过程
现在的项目是以Mybatis作为O/R映射框架,确实好用,也非常方便项目的开发.MyBatis支持普通sql的查询.视图的查询.存储过程调用,是一种非常优秀的持久层框架.它可利用简单的XML或注解用语 ...
- 深入剖析ConcurrentHashMap(2)
转载自并发编程网 – ifeve.com本文链接地址: 深入剖析ConcurrentHashMap(2) 经过之前的铺垫,现在可以进入正题了.我们关注的操作有:get,put,remove 这3个操作 ...