1.字符串
1.判断字符串 变成‘标题’
In [1]: 'Hello'.istitle()
Out[1]: True
In [2]: 'hello'.istitle()
Out[2]: False
In [7]: 'heLLo'.islower()
Out[7]: False
In [8]: 'heLLo'.isupper()
Out[8]: False
2.将字符串全部变为大写
In [3]: 'hello'.upper()
Out[3]: 'HELLO'
3.将字符串全部变为小写
In [4]: 'heLLo'.lower()
Out[4]: 'hello'
In [5]: 'heLLo'.title()
Out[5]: 'Hello'
In [6]: 'heLLo'.swapcase()
Out[6]: 'HEllO'
[[:digit:]] [[:alnum:]]
[[:upper:]] [[:lower:]]
[[:space:]]
4.变量名是否合法?
1).变量名可以由字母,数字或下划线组成
2).变量名只能以字母或下划线开头
1.变量名的第一个字符是否为字母或下划线
2.如果是,继续判断
3.如果不是,报错,不合法
4.依次判断除了第一个字符之外的其他字符
5.判断这个字符是否为数字或下划线
while True:
s = input('变量名:')
if s == 'exit':
print('欢迎下次使用')
break
if s[0].isalpha() or s[0] == '_':
for i in s[1:]:
if not(i.isalnum() or i =='_'):
print('%s变量名不合法' %(s))
break
else:
print('%s变量名合法' %(s))
else:
print('变量名不合法')
变量名:asda
asda变量名合法
变量名:1dsasd
变量名不合法
变量名:sdada_
sdada_变量名合法
变量名:dasd-
dasd-变量名不合法
2.python常用内置方法
In [1]: min(2,4)
Out[1]: 2
In [2]: max(2,4)
Out[2]: 4
In [3]: sum(range(1,101))
Out[3]: 5050
In [4]: sum(range(2,101,2))
Out[4]: 2550
In [5]: sum(range(1,101,2))
Out[5]: 2500
枚举:返回索引值和对应的value值
In [8]: for i,v in enumerate('hello'):
...: print(str(i) + '--------->' + v)
...:
0--------->h
1--------->e
2--------->l
3--------->l
4--------->o
In [9]: s1 = 'abc'
In [10]: s2 = '123'
In [11]: for i in zip(s1,s2):
...: print(i)
...:
('a', '1')
('b', '2')
('c', '3')
In [12]: for i in zip(s1,s2):
...: print(''.join(i))
...:
a1
b2
c3
In [13]: for i in zip(s1,s2):
...: print('-'.join(i))
...:
a-1
b-2
c-3
星星
n = int(input('Num:'))
for i in range(1,n):
print(('*' * i).center(3*n))
for i in range(n,0,-1):
print(('*' * i).center(3*n))
Num:10
*
**
***
****
*****
******
*******
********
*********
**********
*********
********
*******
******
*****
****
***
**
*
3.pycharm的使用
pycharm设置界面(ctrl+alt+s)
修改编辑界面字体
修改控制台界面字体
如何快速创建文件(alt+insert)
格式化python代码,使编程风格好看
如何撤销代码的修改(ctrl + z)
如何取消撤销的代码的修改(ctrl + shift + z)
快速重命名(shift + F6)
快速注释代码(ctrl + /)
快速取消注释代码(ctrl + /)
4.列表
1.列表的创建
数值类型:int float (long)
布尔型
字符串
列表(list)
数组:存储同一种数据类型的集和 scores=[1,2,33,44]
列表(打了激素的数组):可以存储任意数据类型的集和
li = [1,2.2,True,'hello']
print(li,type(li))
列表里面是可以嵌套列表的
li = [1,2,3,False,'python',[1,2,3,4,5]]
print(li,type(li))
import random
li = list(range(10))
random.shuffle(li)
print(li)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2.列表的特性
service = ['http','ssh','ftp']
#索引
#正向索引
print(service[0])
#反向索引
print(service[-1])
#切片
print(service[::-1]) # 列表的反转
print(service[1:]) # 除了第一个之外的其他元素
print(service[:-1]) # 除了最后一个之外的其他元素
#重复
print(service * 3)
#连接
service1 = ['mysql','firewalld']
print(service + service1)
#成员操作符
print('firewalld' in service)
print('ftp' in service)
print('firewalld' not in service)
print('ftp' not in service)
#列表里面嵌套列表
service2 = [['http',80],['ssh',22],['ftp',21]]
#索引
print(service2[0][0])
print(service2[-1][1])
http
ftp
['ftp', 'ssh', 'http']
['ssh', 'ftp']
['http', 'ssh']
['http', 'ssh', 'ftp', 'http', 'ssh', 'ftp', 'http', 'ssh', 'ftp']
['http', 'ssh', 'ftp', 'mysql', 'firewalld']
False
True
True
False
http
21
3.列表练习
1)