Colin_Gu

导航

 

     今天要写的内容主要从Python的几种基础数据类型及其用法。

     Python支持 int,str,bool,list,tuple,dict,set等数据类型:

      在Python 3里,只有一种整数类型 int,表示为长整型。其功能主要用于计算,计数,运算等。像大多数语言一样,数值类型的赋值和计算都是很直观的。其数值类型可以通过type() 函数或者 isinstance()来调用。

1 a = 10
2 print(a,type(a)
1 print(isinstance(a, int))

显示结果分别为:"  10 <class 'int'>  "  与  True 

     Python中的字符串用单引号 ' 或双引号 " 括起来,同时使用反斜杠 \ 转义特殊字符。例如‘ 你好,Python!’字符串主要用于用户少量数据的存储,便于操作。

    在对字符串操作,通常字符串的截取的语法格式如下:

变量[头下标:尾下标]

索引值以 0 为开始值,-1 为从末尾的开始位置。下面介绍几个例子:

1 str_number = 'python1骑士计划'
2 s1 = str_number[:]                  # python1骑士计划
3 s2 = str_number[:6]                 # python
4 s3 = str_number[1::2]               # yhn骑计
5 s4 = str_number[-1:-5:-1]           # 划计士骑
6 s5 = str_number[-1:-6:-2]           # 划士1
7 s6 = str_number[6]                  # 1
8 s7 = str_number[3:7]                # hon1
9 s8 = str_number[1:8:3]              # yo骑

下面介绍几种对字符串处理的方法:

 1 name = 'oldBOy'
 2 print(name.capitalize())  # 首字母大写:Oldboy
 3 print(name.upper())       # 全大写:  OLDBOY
 4 print(name.lower())       # 全小写:  oldboy
 5 print(name.swapcase())    # 大小写翻转 OLDboY
 6 s1 = 'colin alego beijing jenny'
 7 print(s1.title())   # 非字母隔开的每个部分的首字母大写: Colin Alego Beijing Jenny
 8 # find 通过元素找索引,找到就返回,没找到就返回-1
 9 print(name.find('B'))     # 3
10 print(name.find('ld'))    # 1
11 print(name.find('w'))     # -1
12 # index 通过元素找索引,找到第一个就返回,没有此元素就报错
13 print(name.index('w'))    # 报错

strip:默认去除字符串前后的空格,换行符,制表符

1 name = '    \toldboy\n'
2 print(name.strip())  # 默认去除字符串前后的空格,换行符,制表符
3 name1 = '***col*in**'
4 print(name1.strip('*'))  # 删除首尾的‘*’,中间的不删除

split : 将字符串分割成列表(str-----> list)

正常情况下:

1 s1 = 'colin,wusir,alex,taibai'
2 print(s1.split())       # ['colin,wusir,alex,taibai']
3 print(s1.split(','))    # ['colin', 'wusir', 'alex', 'taibai']

当字符串首部出现空格时:

 

1 s1 = ' alex wusir taibai'
2 print(s1.split())       # ['alex', 'wusir', 'taibai']  alex前的空格没有了
3 s2 = ',alex,wusir,taibai'
4 print(s2.split(','))    #['', 'alex', 'wusir', 'taibai']  逗号前出现了空格
5 s3 = ' alexlwle'
6 print(s3.split(' '))    # ['', 'alexlwle']
1 s1 = 'alexlwle'
2 print(s1.split('l',1))   # 设置分割次数  ['a', 'exlwle']
3 print(s1.rsplit('l',1))  # 从右至左分割  ['alexlw', 'e']

join() :表示自定制连接符,将可迭代对象中的元素连接起来:

1 str1 = 'alex'
2 s1 = '*'.join(str1)
3 s2 = ' '.join(str1)
4 print(s1)              # a*l*e*x
5 print(s2)              # a l e x

replace(): 

1 str_1 = ' colin is very good!'
2 str_2 = str_1.replace('colin', 'Jenny')
3 print(str_2)              # Jenny is very good!

格式化输出: .format():

1 #  格式化输出的三种方法 .format()
2 s1 = '我叫{},今年{},性别{}'.format('colin', '27', '')
3 s2 = '我叫{0},今年{1},性别{2},我依然叫{0}'.format('colin', '27', '')
4 s3 = '我叫{name},今年{age},性别{sex}'.format(age = '27', name = 'colin', sex = '')
5 print(s1)          # 我叫colin,今年27,性别男
6 print(s2)          # 我叫colin,今年27,性别男,我依然叫colin
7 print(s3)          # 我叫colin,今年27,性别男

is 系列:

1 name = 'taibai'
2 name1 = 'a123'
3 print(name.isalnum())  # 数字或字母组成     True
4 print(name.isdigit())  # 判断全是由整数组成  False
5 print(name.isalpha())  # 全部由字母组成     True

公共方法:

1 name = 'alexaaa'
2 print(name.count('a'))    # 4  统计a的个数
3 print(len(name))          # 7  计算name的长度
1 name = 'alexaaa'
2 print(name.count('a'))    # 4  统计a的个数
3 print(len(name))          # 7  计算name的长度

     在计算机中,机器只能识别两种状态,True 与 False ,即bool值,通过机器反馈给用户对与错。

1 print(bool('colin'))
2 print(bool(123))
3 s = ''
4 print(bool(s))

结果为:

True
True
False

从上面分析可知, 空字符串 bool 为False ,非空即为True。

    在Python中,List(列表) 是 Python 中使用最频繁的数据类型。列表用方括号‘[]’表示,内容写在方括号内,列表内容可以是各种数据类型,如int型数字,字符串(str)类型,嵌套列表(list)类型,字典(dict)类型等。例:

1 [1, 2, 'abc']
2 [2, 'msn', [1,3,5]]
3 ['abcd', 786, 2.23, 'runoob', [2, 'hello'], 123, {'name':colin, 'age':27}]

  每日一练:

例: 1,用户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使用字符串格式化)。

       2,如果机会用完了,可以再给用户选择继续的机会,如果选择继续,则还有三次机会,若选择并不继续循环,则退出,并且说一句,要不要脸啊!

1题:

 1 count_number = 0
 2 while count_number < 3:
 3     user_name = input('Entrance your user_name:')
 4     pasward = input('Entrance you passward:')
 5     if user_name == 'Colin' and pasward == '123456':
 6         print('Enter Success!')
 7         break
 8     else:
 9         print('user_name or passward False! And you have only %d chances!' % (2 - count_number))
10     count_number += 1

2题:

 1 flag_state = True
 2 count_number = 0
 3 while flag_state:
 4     user_name = input('Entrance your user_name:')
 5     pasward = input('Entrance you passward:')
 6     if user_name == 'Colin' and pasward == '123456':
 7         print('Enter Success!')
 8         flag_state = False
 9     else:
10         print('user_name or passward False! And you have only %d chances!' % (2 - count_number))
11         count_number += 1
12     if count_number == 3:
13         print('There are three more chances for you! Are you Try again  ?')
14         Your_answer = input('You input answer is Yes or No :')
15         count_number_again = 0
16         flag_state_1 = True
17         if Your_answer == 'Yes':
18             while flag_state_1:
19                 user_name = input('Entrance your user_name:')
20                 pasward = input('Entrance you passward:')
21 
22                 if user_name == 'Colin' and pasward == '123456':
23                     print('Enter Success!')
24                     break
25                 else:
26                     print('user_name or passward False! And you have only %d chances!' % (2 - count_number_again))
27                     count_number_again += 1
28                     if count_number_again == 3:
29                         flag_state = False
30                         flag_state_1 = False
31         else:
32             if Your_answer == 'No':
33                 print('You are a looser!')
34                 flag_state = False
35                 flag_state_1 = False

 

posted on 2018-08-06 17:32  Colin_Gu  阅读(106)  评论(0编辑  收藏  举报