Python基础
发布时间:2020-12-20 10:46:25 所属栏目:Python 来源:网络整理
导读:Python基础 一、Python3注释 1、单行注释 # 这是个注释 print ( ‘ hello world! ‘ ) 2、多行注释 ‘‘‘ print(‘hello,world‘)print(‘hello,world‘) ‘‘‘ 二、Python3变量类型 # 变量 name = ‘ xmb ‘ # 字符串,string age = 18 # 整数,int score
Python基础 一、Python3注释 1、单行注释 #这是个注释 print(‘hello world!‘) 2、多行注释 ‘‘‘ print(‘hello,world‘) print(‘hello,world‘) ‘‘‘ 二、Python3变量类型 #变量 name = ‘xmb‘ #字符串,string age = 18 #整数,int score = 1000.00 #浮点数,float 三、字符串常用方法 1 s = ‘abcDefH‘ 2 print(s.count(‘a‘)) #统计字符串里某个元素的个数 3 print(s.strip()) #默认去空格和换行符 4 print(s.lstrip()) #去左边的空格 5 print(s.rstrip()) #去右边的空格 6 print(s.lower()) #把字符串变成小写的 7 print(s.upper()) #把字符串变成大写的 8 print(s.index(‘d‘)) #找字符串的下标,找不到报错 9 print(s.find(‘d‘)) #找字符串的下标,找不到返回-1 10 print(s.isupper()) #判断是不是都是大写字母 11 print(s.islower()) #判断是不是都是小写字母 12 print(s.isdigit()) #判断是否为数字 13 print(s.startswith(‘1‘)) #判断以xx开头 14 print(s.endswith(‘jpg‘)) #判断以xx结尾 15 print(s.isspace()) #是不是空格 16 print(s.isalpha()) #如果是字母和汉字的话返回true 17 print(s.isalnum()) #如果是字母、汉字和数字的返回true,只要不包含#特殊字符都返回true 18 print(s.capitalize()) #首字母大写 19 print(s.title()) #多个单词首字母大写 20 print(s.center(50,‘*‘)) #居中 21 print(s.zfill(5)) #补零 22 print(s.replace(‘a‘,‘A‘)) #把字符串里的a变成A 23 result = s.split("") #不能传空的字符串,这样是不对的,会报错 24 result = s.split(‘,‘) #以,进行分割 25 print(‘‘.join()) #连接字符串 ?四、字符串格式化 1、字符串格式化符号 ? ?例子1: import datetime user = ‘xmb‘ age = 18 score = 95.3 today = datetime.datetime.today() msg1 = ‘你的名字是 %s,你的年龄是 %d ,你的分数是 %.2f,今天的日期是%s‘ %(user,age,score,today) msg2 = ‘你的名字是 %s,你的年龄是 %s ,你的分数是 %s ‘ %(user,score) msg3 = ‘你的名字是{name},年龄是{nianling},分数是{score}‘.format(name=user,nianling=age,score=score) msg4 = ‘你的名字是{},年龄是{}‘.format(user,age) 例子2: username2=‘xmb‘ password2=‘123456‘ role2=‘1‘ email2=‘[email?protected]‘ phone2=‘18623241323‘ #key与values必须一一对应 sql=‘insert into user (username,password,role,email,phone) values (%s,%s,%s);‘%(password2,username2,role2,email2,phone2) #.format参数不用一一对应 sql = ‘insert into user (username,phone) values ({username},{password},{role},{email},{phone})‘.format(phone=phone2,email=email2,password=password2,username=username2,role=role2) 五、条件判断 例子: score = int(input(‘请输入分数:‘)) if score>=90 and score<=100 : #>= <= == != print(‘优秀‘) elif score<90 and score>=80: print(‘良好‘) elif score<80 and score>=60: print(‘及格‘) elif score<60 and score>0: print(‘不及格‘) else: print(‘分数不合法‘) ? 六、循环 1、Python循环分为while循环和for循环 2、while循环需要定义一个计数器 3、break,在循环里遇到break,立即结束循环 4、continue,在循环里遇到continue,结束本次循环,进行下次循环 例子1:while循环 import random number = random.randint(1,100) #随机产生一个1-100之间数字 print(number) count = 0 while count<7: count = count + 1 guess = input(‘请输入一个数字:‘) guess = int(guess) if guess == number: print(‘恭喜你猜对了,游戏结束‘) break elif guess<number: print(‘猜小了‘) continue else: print(‘猜大了‘) continue else: #while对应的else的作用是,循环正常结束之后,会执行else里面的代码 print(‘错误次数已经用完‘) 例子2:循环 import random number = random.randint(1,100) #随机产生一个1-100之间数字 print(number) for i in range(7): guess = input(‘请输入一个数字:‘) #输入的是sting类型 guess = int(guess) #转成int类型 if guess == number: print(‘恭喜你猜对了,游戏结束‘) break elif guess<number: print(‘猜小了‘) continue else: print(‘猜大了‘) continue else: #循环正常结束之后,会执行else里面的代码 print(‘错误次数已经用完‘) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |