Python编程:从入门到实践--一到三章

Python编程:从入门到实践

  • 1 (前三章)
    • 1.1 字符串方法
      • 1.1.1 修改大小写
      • 1.1.2 删除空白
    • 1.2 一个神奇的指令(不知道有何用)
    • 1.3 列表方法
      • 1.3.1 增加元素
      • 1.3.2 删除元素
      • 1.3.3 组织列表

1 (前三章)

1.1 字符串方法

1.1.1 修改大小写

.title() .upper() .lower()

以下是代码:

name = "alGLE LOVE!"
print(name)
print(name.title())
print(name.upper())
print(name.lower())

以下是运行效果:

alGLE LOVE!
Algle Love!
ALGLE LOVE!
algle love!

1.1.2 删除空白

.rstrip() .lstrip() .strip()
以下是代码:

language = "  python  "
print(language)
print(language.rstrip())
print(language.lstrip())
print(language.strip())

以下是运行效果:

  python  
  python
python  
python

注:第一行左右都有空格,第二行只有左侧有,第三行只有右侧有,第四行左右无空格。

1.2 一个神奇的指令(不知道有何用)

import this

以下是代码:

import this

以下是运行效果:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!

1.3 列表方法

1.3.1 增加元素

.append(数据) .insert(索引,数据)

代码从略

1.3.2 删除元素

方法一 索引删除
del .pop() .pop(索引)

以下是代码:

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[2]
print(motorcycles)
del motorcycles[0]
print(motorcycles)

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop() 
print(motorcycles) 
print(popped_motorcycle)

motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0) 
print('The first motorcycle I owned was a ' + first_owned.title() + '.')

以下是运行效果:

[‘honda’, ‘yamaha’, ‘suzuki’]
[‘honda’, ‘yamaha’]
[‘yamaha’]

[‘honda’, ‘yamaha’, ‘suzuki’]
[‘honda’, ‘yamaha’]
suzuki

The first motorcycle I owned was a Honda.

方法二 值删除
remove(值)

以下是代码:

motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)

以下是运行效果:

[‘honda’, ‘yamaha’, ‘suzuki’, ‘ducati’]
[‘honda’, ‘yamaha’, ‘suzuki’]

1.3.3 组织列表

方法 效果
.sort() 永久升序排列
.sort(reverse=True) 永久降序排列
.sorted() 临时升序排列
.sorted(reverse=True) 临时降序排列
reverse 永久反转列表

以下是代码:

cars = ['bmw', 'audi', 'toyota', 'subaru'] 
cars.sort()
print(cars)

cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)

以下是运行效果:

[‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]

[‘toyota’, ‘subaru’, ‘bmw’, ‘audi’]

以下是代码:

cars = ['bmw', 'audi', 'toyota', 'subaru']
print("Here is the original list:")
print(cars)
print("Here is the sorted list:")
print(sorted(cars))
print("Here is the original list again:")
print(cars)

以下是运行效果:

Here is the original list:
[‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
Here is the sorted list:
[‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]
Here is the original list again:
[‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]

以下是代码:

以下是运行效果:

你可能感兴趣的:(python自习,python)