python学习之路-day1

1.字符串

字符串就是一系列字符。在Python中,用引号括起的都是字符串,其中的引号可以是单引号
>>> name="ada lovelace"
>>> print(name.title())  #首字母改成大写
Ada Lovelace
>>> print(name.upper()) #全改成大写
ADA LOVELACE
>>> print(name.lower())  #全改成小写
ada lovelace
>>>
用函数str()避免类型错误避免类型
age = 23 #此时没有指定类型,它可能是整数23,也可能是字符2和3.
message = "Happy " + str(age) + "rd Birthday!"#要指定str类型,否则会报错
print(message)
Happy 23rd Birthday
 
合并字符串
>>> first_name="ada"
>>> last_name="lovelace"
>>> full_name=first_name+""+last_name
>>> print(full_name)
adalovelace
>>> print("hello,"+full_name.title ()+"!")
hello,Adalovelace!
 
使用制表符或换行符来添加空白
>>> print("Python")
Python
>>> print("	Python")  #添加制表符,可使用字符组合	
Python
 
>>> print("Languages:
Python
C
JavaScript")  #字符串中添加换行符,可使用字符组合

Languages:
Python
C
JavaScript
 
删除空白
>>> favorite_language='python '
>>> favorite_language
'python '
>>> favorite_language.rstrip()     #要确保字符串末尾没有空白,可使用方法rstrip()
'python'
>>> favorite_language
'python '
 
要永久删除这个字符串中的空白,必须将删除操作的结果存回到变量中:
>>> favorite_language=favorite_language.rstrip()
>>> favorite_language
'python'
 
>>> favorite_language=' python '
>>> favorite_language.rstrip() #剔除尾部空白
' python'
>>> favorite_language.lstrip() #剔除开头空白
'python '
>>> favorite_language.strip() #剔除两端空白
'python'
>>>
View Code
 
在Python 2中,无需将要打印的内容放在括号内
在Python中,可对整数执行加(+)减(-)乘(*)除(/)运算。
在终端会话中,Python直接返回运算结果。Python使用两个乘号表示乘方运算
 
2.列表
列表由一系列按特定顺序排列的元素组成
1 >>> bicycles=['trek', 'cannondale', 'redline' ,'specialized']
2 >>> print(bicycles)
3 ['trek', 'cannondale', 'redline', 'specialized']
4 >>> print(bicycles[0]) #提取第一辆自行车
5 trek
6 >>> print(bicycles[0].title())  #首字母大写
7 Trek
8 >>> print(bicycles[-1])  #通过将索引指定为-1,可让Python返回最后一个列表元素
9 specialized
View Code
在Python中,第一个列表元素的索引为0,而不是1。

要修改列表元素,可指定列表名和要修改的元素的索引,再指定该元素的新值

 1 >>> motorcycles=['honda', 'yamaha', 'suzuki']
 2 >>> print(motorcycles)
 3 ['honda', 'yamaha', 'suzuki']
 4 >>> motorcycles[0]='ducati'
 5 >>> print(motorcycles)
 6 ['ducati', 'yamaha', 'suzuki']
 7 
 8 >>> motorcycles.append('honda')  #方法append()将元素'honda'添加到了列表末尾
 9 >>> print(motorcycles)
10 ['ducati', 'yamaha', 'suzuki', 'honda']
11 
12 >>> motocycles=['honda','yamaha','suzuki']
13 >>> motocycles.insert(0,'ducati') #使用方法insert()可在列表的任何位置添加新元素。
14 >>> print(motocycles)
15 ['ducati', 'honda', 'yamaha', 'suzuki']
16 >>> del motocycles[0]  #使用del语句删除元素
17 >>> print(motocycles)
18 ['honda', 'yamaha', 'suzuki']
19 
20 
21 >>> cars=['bmw','audi','toyota','subaru']
22 >>> cars.sort() #使用sort方法将列表按字母排序
23 >>> print(cars)
24 ['audi', 'bmw', 'subaru', 'toyota']
25 
26 >>> cars=['bmw','audi','toyota','subaru']
27 >>> cars.sort(reverse=True) #按字母反方向排序
28 >>> print(cars)
29 ['toyota', 'subaru', 'bmw', 'audi']
View Code
使用函数sorted()对列表进行临时排序对列表进行临时排序

 1 >>> cars=['bmw','audi','toyota','subaru']
 2 >>> print("Here is the original list:")
 3 Here is the original list:
 4 >>> print(cars)#正常排序
 5 ['bmw', 'audi', 'toyota', 'subaru']
 6 >>> 
 7 >>> print("
Here is the sorted list:")
 8 
 9 Here is the sorted list:
10 >>> print(sorted(cars))#临时排序
11 ['audi', 'bmw', 'subaru', 'toyota']
12 >>> 
13 >>> print("
Here is the original list again:")
14 
15 Here is the original list again:
16 >>> print(cars)#正常排序
17 ['bmw', 'audi', 'toyota', 'subaru']
18 
19 >>> cars.reverse() #反向排序
20 >>> print(cars)
21 ['subaru', 'toyota', 'audi', 'bmw']  
22 
23 
24 >>> len(cars) #确定列表的长度
View Code

访问最后一个元素

1 >>> motorcycles=['honda','yamaha','suzuki']
2 >>> print(motorcycles[-1])
3 suzuki
View Code

取最值

1 >>> digits=[1,2,3,4,5,6,7,8,9,0]
2 >>> min(digits) #取最小值 
3 0
4 >>> max(digits)#取最大值
5 9
6 >>> sum(digits)#取最小值
7 45
View Code

元素增删

 1 num=[10,11,12,13,14]
 2 print(num)
 3 num.append(15)#append方法给数组后面加元素
 4 print(num)
 5 num.insert(0,9)#insert方法给数组指定位置加元素
 6 print(num)
 7 num.remove(15)#删除数组中元素15
 8 print(num)
 9 del num[0]#删除第一个元素
10 print(num)
11 num.pop(0)#删除第一个元素
12 print(num)
View Code

输出结果:

1 [10, 11, 12, 13, 14]
2 [10, 11, 12, 13, 14, 15]
3 [9, 10, 11, 12, 13, 14, 15]
4 [9, 10, 11, 12, 13, 14]
5 [10, 11, 12, 13, 14]
6 [11, 12, 13, 14]
View Code

切片:

 1 >>> players=['charles','martina','michael','florence','eli']
 2 >>> print(players)
 3 ['charles', 'martina', 'michael', 'florence', 'eli']
 4 >>> print(players[0:3])
 5 ['charles', 'martina', 'michael']
 6 >>> print(players[1:4])
 7 ['martina', 'michael', 'florence']
 8 >>> print(players[:4])
 9 ['charles', 'martina', 'michael', 'florence']
10 >>> print(players[2:])
11 ['michael', 'florence', 'eli']
12 >>> print(players[-3:]) #取后三位
13 ['michael', 'florence', 'eli']
View Code

元组:

 1 >>> dimensions=(200,50) #用括号表示
 2 >>> print(dimensions[0])
 3 200
 4 >>> print(dimensions[1])
 5 50
 6 >>> dimensions[0]=250  #当修改元组的值时报错。修改元组的操作是被禁止的,因此Python指出不能给元组的元素赋值
 7 Traceback (most recent call last):
 8   File "<pyshell#3>", line 1, in <module>
 9     dimensions[0]=250
10 TypeError: 'tuple' object does not support item assignment
11 >>> 
View Code

比较两个列表的用户名:

1 current_users=['A','B','C','D','E']
2 new_users=['D','E','F','G','H']
3 for users_1 in new_users:#遍历新的用户名组
4     if users_1.upper() in [current_user.upper() for current_user in current_users]:#【遍历原用户名组】得到的数大写后与users_1大写比较
5         print(users_1+"用户名已使用")
6     else:
7         print(users_1+"可以使用该用户名")
View Code

运行结果:

1 D用户名已使用
2 E用户名已使用
3 F可以使用该用户名
4 G可以使用该用户名
5 H可以使用该用户名
View Code
原文地址:https://www.cnblogs.com/awanghang/p/12877772.html