zoukankan      html  css  js  c++  java
  • python笔记03

    day03

    一、今日内容:

    1、整型

    2、字符串

    3、布尔类型

    二、内容回顾和补充

    脑图--xmind软件,processon

    (一)运算符补充(in、not in)

    value = "我是中国人" # 判断‘中国’是否在value所代指的字符串中。“中国”是否是value所代指的字符串的子序列。

    v1 = "中国" in value

    示例

    content = input('请输入内容:')

    if "退钱" in content:

    print('包含敏感字符') # 示例 while True:

    content = input('请输入内容:')

    if "退钱" in content:

    print('包含敏感字符')

    else:

    print(content)

    break

    (二)优先级

    符号优先级大于not

    回顾作业:

    ①用户3次登录并提示次数:(n=1,+1;n=2,-1)

    num = 3
    while num > 0:
    	user_name = input('请输入用户名:')
    	password = input('请输入密码:')
    	num = num - 1
    	if user_name == 'alex' and password == 'alex123':
    		print('登录成功!')
    		break
    	else:
    		print('登录失败,你剩余次数为:%d次' % (num,))
    

    ②猜年龄(3次):如果3次,提示是否继续猜,选择Y,继续(count=1,contine),选择N(break),跳出。

    n=3
    while n>0:
    	age=int(input('请输入年龄:'))
    	if age != 26:
    		print('你猜错了')
    		n=n-1
    		if n==0:
    			content=input('三次机会已经用完,是否要继续玩(N/Y):')
    			if content.upper()=='N':
    				break
    			elif content.upper()=='Y':
    				n=3
    			else:
    				print('输入错误,请重新输入!')
    	else:
    		print('你猜对了')
    		break
    

    三、今日详细内容(数据类型)

    (一)整型(int)

    32位电脑:大小范围为2的32次方幂次-1

    64位电脑:大小范围为2的64次方幂次-1

    超出范围后,自动转换为:long。

    py2整数除法只能保留整数位。

    如果需要保留小数位,需要引入模块:

    如:

    from __ future __ import division
    
    v=9/2
    
    print(v)
    

    py3:只有int,无long类型

    整数除法保留所有。

    (二)布尔值(bool)

    False:0和空字符串

    True:其他

    其他

    (三)字符串(str)

    字符串特有:

    ①upper(),lower() #应用:定义验证码,不分大小写。

    ②.isdigit()

    判断输入的字符串里字数的个数。

    val.isdigit()#结果为bool类型

    ③.strip(),lstrip(),rstrip()

    ④.replace('被替换的字符/子序列',‘要替换的内容’)

    ​ .replace('被替换的字符/子序列',‘要替换的内容’,1)#替换1次

    ⑤.split('根据什么东西分割'),split('根据什么东西分割',1),rsplit()

    总结,字符串特有的特性:(upper,lower,isdigit,trip,lstrip,rstrip,split,rsplit,replace)

    例子:

    check_code='iyKY'
    message='请输入验证码%s:%(check_code),'
    code=input(message)
    if code.upper()==check_code.upper():(也可以使用lower)
    print('输入正确!')
    else:
    pirnt("输入错误!")
    
    ####10086
    
    判断是否为数字:num.isdigit()
    
    判断input是否为数字
    
    flag=num.isdigit()#结果为bool值
    
    ###去掉两边字符串的空格
    
    user=input('please put :')
    
    user.rstrip()#去掉右边的空格
    
    user.lstrip()#去掉左边的空格
    
    user.trip()#去掉两边的空格
    
    输出名字全部大写:
    
    print(‘---》’(user.trip()).upper  ’《----‘)
    
    ####字符串替换:
    
    message=input('please input:')
    
    print(message.replace('sb','**')#把敏感词替换成**。
    
    ####字符串切割:
    
    message=input('please input:')
    
    print(message.split(',') #根据逗号进行切割,输入列表
    
    print(message.rsplit(',',1)#从左往右切割,切割1次。
    

    公共特性:

    ①len,计算长度。(字符串-》计算字符串中的字符个数)

    ####计算字符串长度,索引取值:len()
    
    va="alex"
    print(len(va))
    EXAMPLE:
    text=input("please input:")
    index_len=len(text)
    index=0
    while True:
    val=text[index]#索引取值,从0开始;负数从后向前取值。
    print(val)
    if index==index_len-1:
    break
    index+=1
    
    

    ②索引值(0开始)

    v='oldboy'
    v1=v[0]
    v2=v[1]
    
    

    ③切片(从0开始)

    v=‘oldboy’

    v1=v[2:4]

    注意:取头不取尾。

    ###字符串的切片
    
    根据索引值取值:
    
    v=‘oldboy’
    
    v1=v[2:3]#  索引值前取,后不取。
    
    v2=v[3:] #索引3后面的全部。
    
    v3=v[:-1]#从倒数第二个到索引0。
    
    
    data=input(‘please input:’)
    方法1:
    data[-2:0] #取后面两个字符
    方法2:
    data[total_len-2:total_len] # total_len=len(data)
    

    四、练习

    需求:让用户输入任意字符串,获取字符串之后并计算其中有多少个数字。 
    total = 0 
    text = input('请输入内容:') # ads2kjf5adja453421sdfsdf 
    index_len = len(text) 
    index = 0 
    while True:    
    val = text[index]  
    #print(val) # "a"    # 判断val是否是数字    #     - 是数字:total + 1    #     -   不是:继续玩下走,执行下一次循环去检查下一个字符。   
    flag = val.isdigit()  
    if flag:       
    total = total + 1 # total += 1   
    if index == index_len - 1:     
    break   
    index += 1
    
  • 相关阅读:
    Oracle JDBC:驱动版本区别与区分 [转]
    项目管理: Alpha,Beta,RC,GA,Release
    J2EE: JCA (Java Connector Architecture) [转]
    HTML:Event [转]
    JavaScript:inherits
    HTML5 Canvas
    Selenium1 Selenium2 WebDriver
    HTML5 Canvas:初始Canvas
    License友好的前端组件合集
    JavaSe:Comparator
  • 原文地址:https://www.cnblogs.com/cuiyongchao007/p/12056565.html
Copyright © 2011-2022 走看看