python

python 基本数据类型一

2018-04-26  本文已影响0人  little_short

count = 0

while count < 10:

count = count + 1

continue  #终止当前循环

print('123')

else:

print('end')

while count < 10:

if count ==7;

print('7')

break  #终止整个循环

else:

count = count + 1

print('end')

用户登陆 (三次机会重试)

#!/usr/bin/env python

#-*- coding:utf-8 -*-

count = 0

while count < 3:

user = input('>>>')

pwd = input('>>>')

if user == 'root' and pwd == 'root':

print('欢迎登陆')

break

else:

print('信息有误')

count = count + 1

if count == 3:

print('登陆失败')

else:

print('登陆成功')

运算符

算数运算符

+ - * /

幂**

余%

取整//

成员运算

in

not in

# 字符  子序列

name = '789sss'

if '7s8' in name:

print('ok')

elif '7sss' in name:

print('well')

elif '8s' in name:

print('nice')

else:

print('biu...')

if 's' not in name:

print('sorry')

else:

print('007')

比较运算符

==

>

<

>=

<=

!= 不等于

<> 不等于

逻辑运算

and

or

补充:

计算顺序

()

从左至右

True or ==》 true

True and ==》 往下判断

False or ==》 往下判断

False and ==》 false

赋值运算

+=

-=

*=

/=

**=

%=

//=

布尔值 真 假

基本数据类型---详细

数字 int

a = 123

p3中数字不管多大都是  数字类型 int

p2中数字太大  长整型 long

-int()

将字符串转换成数字

a = '123'

b = int(a)

print(type(a),a)

print(type(b),b)

num = "0011"

int(num, base=2)

-bit_length() 当二进制表示最少用多少个

字符串 str

b = 'xiao'

-capitalize()

test = 'sda'

#首字母大写

v = test.capitalize()

print(v)

-casefold  -lower  大写转小写

test1 = 'ADa'

v = test1.casefold()  特殊字符照样可以转换

print(v)

v1 = test1.lower()  仅英文字符

print(v1)

-center(width,default) 设置宽度  将内容居中  default只能填一个字符

v1 = test.center(20,"*")

print(v1)

-count() 去字符串中寻找目标个数 

test = 'ADeea'

vss = test.count('e',2,4)

print(vss)

-endswith()  以什么结尾 返回bool值

test = 'ADeea'

v = test.endswith('a')

print(v)

-find() 从第一个开始找,获取其位置

v1 = test.find('A',0,1)

print(v1)

-index()  获取索引  找不到报错 建议使用find

-format() 格式化

test = 'i am {0} age {age}'

print(test)

v1 = test.format('kjh',age=19)

print(v1)

-format_map()

v1 = test.format_map({"name" : 'ks', "age" : 12})

print(v1)

-isalnum() 判断是否只存在数字和字母 返回bool值

test = 'uasf788'

v1 = test.isalnum()

print(v1)

布尔值 bool

列表 list

元祖 tuple

字典 dict

上一篇下一篇

猜你喜欢

热点阅读