开班前自学—python基础二_基础数据(2019.02.27)

一 基础数据类型

list: [],各种数据类型的数据,大量的数据,便于操作。

tuple: 元组。()只读列表。

dic:

存储大量的数据,关系型数据。

二 int str bool相互转换
1 int <---> str
int --->str int(str) 条件:str必须全部由数字组成。
str --->int str(int)
age = int (input('how old are you?'))
print(age)                             # 如果输入内容包括非数字,会报错
s1 = 520
s2 = str(520)
print(s1,type(s1))      # 输出:520 <class 'int'>
print(s2,type(s2))      # 输出:520 <class 'str'>

2 bool <---> int
bool ---> int  True ---> 1   Flase ---> 0 
int ---> bool 非零为True,零为False
3 str <---> bool
bool ---> str   str(True) str(False)
str ---> bool 空str--->Flase 非空str ---> True ps:空格不是空字符串
s1 = ''
if s1:
    print('这是真的')   #当s1为非空字符串(包括空格),输出
else:
    print('这是假的')   #当s1为空字符串时,输出

三 基础数据运算

1 索引 切片 步长

s = 'python'
s1 = s[0]
print(s1)       # 输出字符串s的第一位
s2 = s[-1]
print(s2)       # 输出字符串s的最后一位
s3 = s[2:5]       # 切片顾头不顾尾
print(s3)
s4 = s[:3]       #切首尾可省略
print(s4)
s5 = s[::2]       #步长为2
print(s5)
s6 = s[-1:-6:-1]  #倒切步长为-1
print(s6)

2  capitalize

 首字母大写,其余小写

s = 'python'
s7 = ss.capitalize()
print(s7)        #output:Python

3  center

s = 'python'
s8 = s.center(20)       # 20个字符的空间,字符串居中;如果小于字符串实际大小,按实际长度
s80 = s.center(20,'*',)
print(s8)               # output:       python
print(s80)              # output:*******python*******

4  upper lower

s = 'python'
s9 = ss.upper()           #upper:全部大写
print(s9)
s10 = ss.lower()          #lower:全部小写
print(s10)

验证码

s = input('请输入你的验证码,不区分大小写:').lower()
if s == 'gdn1i':
    print('验证码输入正确')
else:
    print('验证码输入错误')

5 swapcase

  大小写转换

ss = 'pYthOn'
s14 = ss.swapcase()
print(s14)

6 title 

  以非字母符号分开字符串,将分割开的部分首字母大写

ss = 'tom harry*jerry2mary'
s15 = ss.title()
print(s15)                       # output:Tom Harry*Jerry2Mary

7 startswith

 s = 'python'
s11 = s.startswith('pyth')      # 判断s是否以pyth开头
print(s11)
s12 = s.startswith('h',3,6)     # 可切片,用,隔开
print(s12)
s13 = s.endswith('on')          # 判断s是否以on结尾
print(s13)

8  index find

s = 'pychonp'
s16 = s.index('p')
print(s16)             # outout:0 (代表p在python中第一个位置)
s16 = s.index('p',2)
print(s16)             # outout:6 (代表第二个p在python中第一个位置)
# s17 = s.index('k')
# print(s17)             # 找不到时会报错,
s18 = s.find('p')
print(s18)
s19 = s.find('k')          #find 找不到是输出-1,而不是报错
print(s19)

9 strip 

  去除空格 \t \n

d = ' kitty '
dd = '\tpeiqi\n'              # \t 同Tab;\n 换行符
s20 = d.strip(' kit')        # 拆分为空格 k i t 在d中逐个去除
print(s20)
s21 = dd.strip()
s22 = d.lstrip()           #只去除左边
s23 = d.rstrip()           #只去除右边
print(dd)
print(s21)
print(s22)
print(s23)
用户名输入空格取消
s = input('请输入用户名').strip().upper()
if s == 'TOM':
    print('用户名输入正确')
else:
    print('用户名输入错误')

10  split

s = ' Tom John harry mary'
ss = 'Tom,John,harry,mary'
sss = 'TomQJohnQharryQmary'
s24 = s.split()
print(s24)                   # output:['Tom', 'John', 'Harry', 'Mary']
s25 = ss.split(',')          # 可以自定用什么分割
print(s25)
s26 = sss.split('Q',2)         # 可以自定用什么分割,以及分割次数
print(s26)

11  join

  拆分成最小单元并相加

s = 'python'
s27 = '+'.join(s)
print(s27)
ss = ['Tom','Jane','John','Mary']          #多个字符串(只能是字符串),可插入其它符号
sss = '*'.join(ss)
print(sss)
ss = ['Tom','John','harry','mary']
s28 = '>>'.join(ss)
print(s28)

12 replace

  取代

s = 'python'
s29 = s.replace('y','k')
print(s29)

len()   ---通用
s = 'python'
print(len(s))

13  count

s = 'python'
s30 = s.count('p')
print(s30)

14  format

第一种:
msg = 'My name is {},I am {} year old,my hobby is {}.'\
    .format('Tom',20,'reading')                # 使用\再换行,实际输出不换行
print(msg)                  # output:My name is Tom,I am 20 year old,my hobby is reading.

第二种:
msg1 = '{0} is green,{1} is yellow,{2} is white.I like white,so I prefer to traval in {2}'\
    .format('spring', 'autumn','winter')     #  要按'0','1','2'的顺序

第三种:
msg = 'My name is {name},I am {age} year old,my hobby is {hobby}.'\
    .format(name = 'Tom',age = '20',hobby = 'reading')   # name age hobby 不用引号
print(msg)                  # output:My name is Tom,I am 20 year old,my hobby is reading.
15   is
s = 'python37'          # bool值判断
print(s.isalnum())      # 判断是否全部由字母或数字组成
print(s.isalpha())      # 判断是否全部有字母组成
print(s.isdigit())      # 判断是否全部有数字组成

作业:将'python'拆开,每个一行排列

s = python
i = 0
while i <= 5:
    s1 = s[i]
    i += 1
    print(s1)
count = 0
while count < len(s):
    print(s[count])
    count += 1
s = 'python'
for i in s:
    print(i)
#    print(i+'-')    #循环打印











猜你喜欢

转载自www.cnblogs.com/yingpeng/p/10446241.html