Python学习1

2.3 字符串操作

1、使用制表符或者换行符来添加空白

print("\tPython\tc\tjava\truby\tmysql\n\tsql\tscala\tr\tperl\toracle")

2、删除空白

确保字符串末尾没有空白,可使用方法rstrip(),剔除字符串开头的左边的空格用lstrip(),同时剔除字符串两端的空白用strip(),类似于SQL中的trim()函数。

动手试一试:


name="Eric Green"


print('"Hello '+name+',would you like to learn some Python today?"')


print(name.lower())


print(name.upper())


print(name.title())


print('Albert Einstein once said,“A person who never made a miatake never tried anything new.”')


第3章 列表简介

列表是由一系列按特定顺序排列的元素组成。

1、在列表末尾添加元素

motorcycles=[]

motorcycles.append('one')

2、在列表中插入元素

motorcycles.insert(0,'one')

3、从列表中删除元素

del motorcycles[0]

pop()删除列表末尾的元素

pop(0,1,2)弹出列表中任何位置处的元素

4、根据值删除元素

motorcycles.remove('one')

动手试一试:

#3-4嘉宾名单

guests=['peng','dad','mom','grandmother','shishi']


for guest in guests:

    print('Dear '+guest.title()+',I invite you to dinner!')


print('Peng,she can\'t come!')


#del guests[0]

#guests.insert(0,'qingquan')

guests[0]='qingquan'

for guest in guests:

    print('Dear '+guest.title()+',I invite you to dinner!')


print('I\'m so happy that I found a bigger table for dinner!')


guests.insert(0,'ting')

guests.insert(3,'long')

guests.append('zuo')


for guest in guests:

    print('Dear '+guest.title()+',I invite you to dinner!')


print('I\'m so sad that  dinner only for two!')

pop1=guests.pop()

print(pop1.title()+',I\'m sorry for that I can\'t invite you tonight!')

pop2=guests.pop()

print(pop2.title()+',I\'m sorry for that I can\'t invite you tonight!')

pop3=guests.pop()

print(pop3.title()+',I\'m sorry for that I can\'t invite you tonight!')

pop4=guests.pop()

print(pop4.title()+',I\'m sorry for that I can\'t invite you tonight!')

pop5=guests.pop()

print(pop5.title()+',I\'m sorry for that I can\'t invite you tonight!')

pop6=guests.pop()

print(pop6.title()+',I\'m sorry for that I can\'t invite you tonight!')

for guest in guests:

    print('Dear '+guest.title()+',I invite you to dinner!')

del guests[0]

del guests[0]

print(guests)

3.3 组织列表

1、使用方法sort()对列表进行永久性排序

cars.sort()

cars.sort(reverse=True)

2、使用函数sorted()对列表进行临时排序

print(sorted(cars))

3、倒着打印列表 临时性

cars.reverse()

恢复再用一次reverse()

4、确定列表的长度

函数len(cars)

动手试一试:

#3-8 放眼世界

places=['the South Pole','Maldives','Europe','USA','Japen']

print(places)

print(sorted(places))

print(places)

print(sorted(places,reverse=True))

print(places)

places.reverse()

print(places)

places.reverse()

print(places)

places.sort()

print(places)

places.sort(reverse=True)

print(places)

第4章  操作列表

4.1、遍历整个列表

for magician in magicians:

     print(magician)

#注意缩进

动手试一试:

#4-1 披萨

pizzas=['chicken','fish','beaf']

for pizza in pizzas:

    print('I like '+pizza+' pizza.')

print('I really love pizza!')

4.3 创建数值列表

1、使用函数range()创建数字列表

numbers=list(range(1,6))

range(2,11,步长2)

for value in range(1,11):

    squares.append(value**2)

列表解析

上面的代码等价于    squares=[value**2 for value in range(1,11)]

动手试一试:

#4-3 数到20

for value in range(1,21):

    print(value)

numbers=list(range(1,1000001)) 

#for number0 in numbers:

#    print(number0)


print(min(numbers))

print(max(numbers))

print(sum(numbers))


odds=list(range(1,21,2)) 

for odd0 in odds:

    print(odd0)


threes=list(range(3,31,3)) 

for three0 in threes:

    print(three0)


cubes=[]

for value in range(1,11):

    cubes.append(value**3)

print(cubes)


cubes_new=[value**3 for value in range(1,11)]

print(cubes_new)

4.4使用列表的一部分

1、切片

列表的部分元素

players[XX:XX]

2、复制列表

friend_foods=my_foods[:]   使用切片复制列表

动手试一试:

#4-10 切片

pizzas=['chicken','fish','beaf','duck','fruit']

for pizza1 in pizzas[0:3]:

    print('The first three items in the list are:'+pizza1 )

for pizza2 in pizzas[1:4]:

    print('three items from the middle of  the list are:'+pizza2 )

for pizza3 in pizzas[-3:]:

    print('The last three items in the list are:'+pizza3 )       

odds=list(range(1,21,2))

for odd3 in odds[0:3]:

    print('The first three items in the list are:'+str(odd3) )


pizzas=['chicken','fish','beaf']

friend_pizzas=pizzas[:]

pizzas.append('duck')

friend_pizzas.append('fruit')

for pizza in pizzas:

    print('My favorite pizzas are:'+pizza)

for friend_pizza in friend_pizzas:

    print('My friend\'s favorite pizzas are:'+friend_pizza) 

4.5 元组:不可变的列表

定义元组:dimensions=(200,50)

不可改变元组的元素,可以给存储元组的变量赋值。

你可能感兴趣的:(Python学习1)