整理自《python编程从入门到实践》
1.安装文本编辑器Geany
首先确保已经安装了gcc与g++(一般的Linux都会有内置的):
sudo apt-get install gcc
sudo apt-get install g++
接下来就是安装Geany:
sudo apt-get install geany
配置geany使用python3编译:build->set build commands:
2.list简介
motorcycles=['honda','yamaha','suzuki']print(motorcycles)
motorcycles.append('ducati')#添加
print(motorcycles)del motorcycles[0]#删除
print(motorcycles)
motorcycles.insert(0,'yamaha')#添加
print(motorcycles)
first_word=motorcycles.pop(0)#删除
print(first_word)print(motorcycles)
motorcycles.remove('suzuki')#删除
print(motorcycles)print(len(motorcycles))#长度
cars=['bmw','audi','toyota','subaru']print(cars)print(sorted(cars))#临时排序
print(cars)
cars.sort()#正排序
print(cars)
cars.sort(reverse=True)#倒排序
print(cars)
cars.reverse()print(cars)
运行结果:
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha', 'suzuki', 'ducati']
['yamaha', 'suzuki', 'ducati']
['yamaha', 'yamaha', 'suzuki', 'ducati']
yamaha
['yamaha', 'suzuki', 'ducati']
['yamaha', 'ducati']
2
['bmw', 'audi', 'toyota', 'subaru']
['audi', 'bmw', 'subaru', 'toyota']
['bmw', 'audi', 'toyota', 'subaru']
['audi', 'bmw', 'subaru', 'toyota']
['toyota', 'subaru', 'bmw', 'audi']
3.list operation
magicians=['alice','david','carolina']for magician inmagicians:print(magician.title()+",that was a great trick!")#create numerical list
numbers=list(range(1,6))
even_numbers=list(range(2,11,2))print(numbers)print(even_numbers)#以下两种方法结果相同
squares=[]for value in range(1,11):
square=value**2squares.append(square)print(squares)
squares_1=[value**2 for value in range(1,11)]print(squares)print(min(squares))print(max(squares))print(sum(squares))
players=['a','b','c','d','e']print(players)print(players[-3:])#最后三个#使用切片可复制列表
players_new=players[:]#简单的复制会使两个变量指向同一个列表
players_new1=players
players.append('f')print(players_new)print(players_new1)#不可变的列表被称为元组,但可重新定义
dimensions=(200,50)for dimension indimensions:print(dimension)
dimensions=(400,100)for dimension indimensions:print(dimension)
运行结果:
Alice,that was a great trick!
David,that was a great trick!
Carolina,that was a great trick!
[1, 2, 3, 4, 5]
[2, 4, 6, 8, 10]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
1
100
385
['a', 'b', 'c', 'd', 'e']
['c', 'd', 'e']
['a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e', 'f']
200
50
400
100
4.if语句
由于没什么特别的,这部分放的示例很少:
#if-elif-else结构
age=12
if age<14:
price=0elif age<18:
price=5
else:
price=10
print("Your admission cost is $"+str(price)+".")#循环操作列表时要判断列表是否为空
requested_toppings=[]ifrequested_toppings:for requested_topping inrequested_toppings:
do somethingelse:
do something
运行结果:
Your admission cost is $0.
5.字典
alien_0={}#创建
alien_0['color']='green'#添加
alien_0['points']=5
print(alien_0)
alien_0['color']='yellow'#修改
print(alien_0)del alien_0['points']#删除
print(alien_0)
{'points': 5, 'color': 'green'}
{'points': 5, 'color': 'yellow'}
{'color': 'yellow'}
遍历所有键值对:
user_0={'username':'efermi','first':'enrico','last':'fermi',
}for key,value inuser_0.items():print("\nkey:"+key)print("value:"+value)
key:username
value:efermi
key:first
value:enrico
key:last
value:fermi
遍历所有键:
favorite_languages={'jen':'python','sarah':'c','edward':'ruby','phil':'python',
}
friends=['phil','sarah']for name infavorite_languages.keys():print(name.title())if name infriends:print("Hi"+name.title() +
", I see your favorite language is" +favorite_languages[name].title()+ "!")
Edward
Phil
Hi Phil, I see your favorite language is Python!
Jen
Sarah
Hi Sarah, I see your favorite language is C!
对包含重复元素的列表可以调用set(),要对列表进行排序可以使用函数sorted(),以下例为例:
遍历字典中的所有值:
favorite_languages={'jen':'python','sarah':'c','edward':'ruby','phil':'python',
}print("The following languges have been mentioned:")for language inset(favorite_languages.values()):print(language.title())
The following languges have been mentioned:
Ruby
Python
C
字典嵌套:
users={'aeinstein':{'first':'albert','last':'einstein','location':'princeton',
},'mcurie':{'first':'marie','last':'curie','location':'paris',
},
}for username,user_info inusers.items():print("\nUsername:"+username)
full_name=user_info['first']+" "+user_info['last']
location=user_info['location']print("\tFull name:"+full_name.title())print("\tLocation:"+location.title())
Username: mcurie
Full name: Marie Curie
Location: Paris
Username: aeinstein
Full name: Albert Einstein
Location: Princeton