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()))  # 3
print(example.lstrip())  # 去左边空格
print(len(example.lstrip()))  # 6
print(example.rstrip())  # 去右边空格
print(len(example.rstrip()))  # 5

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', '19', 'M']
name, age, sex = fruit_list
print(name, age, sex)

# 输出
tom 19 M

下面我们对列表和元祖进行一个简单总结:

1、列表和元祖中的元素是有序的

2、列表种的元素是可变的,而元祖是不可变的

3、列表和元祖都是可嵌套的

原文地址:https://www.cnblogs.com/chen1930/p/5968865.html