python学习之路(2)

输出格式补充

name = '小明'
age = 18
height = 1.71
stu_id = 1
num = 90

print('我的名字是{},年龄是{},身高是{}m,学号是{},及格率是{}%'.format(name,age,height,stu_id,num))
print('我的名字是{},年龄是{},身高是{:.3f}m,学号是{:06d},及格率是{}%'.format(name,age,height,stu_id,num))

运算符优先级

if条件语句

username = input('请输入用户名')
if username == 'admin' or username == 'test':
    print(f'欢迎{username}登录')
else:
    print('查无此人')

debug :蓝色:即将执行

import random
player = int(input('请输入你的选择石头(1)/剪刀(2)/布(3)\n'))
computer = random.randint(1,3)# 1 2 3
print(player,computer)
if((player == 1 and computer == 2) or (player == 2 and computer == 3)
or (player == 3 and computer == 1)):
    print('player win')
elif player == computer:
    print('平局')
else:
    print('computer win')

while循环语句

import random

while True:
    player = int(input('请输入你的选择石头(1)/剪刀(2)/布(3)\n'))
    computer = random.randint(1, 3)
    print(player, computer)
    if ((player == 1 and computer == 2) or (player == 2 and computer == 3)
            or (player == 3 and computer == 1)):
        print('player win')
        break
    elif player == computer:
        print('平局')
    else:
        print('computer win')

i = 1
ans = 0
while i<=100:
    if i%2==0:
        ans+=i
    i+=1
print(ans)

for循环语句

s = 'hello'
for i in s:
    print(i)
print('-' * 30)
for i in range(5):# 0 1 2 3 4
    print(i)
print('-' * 30)
for i in range(3,5):# 3 4
    print(i)

continue break

容器

字符串

注意的点:

my_str = 'hello'
print(my_str,type(my_str))
my_str2 = "hello"
print(my_str2,type(my_str2))
my_str3 = """hello"""
print(my_str3,type(my_str3))
my_str4 = '''hello'''
print(my_str4,type(my_str4))

# \ 就是转义字符
my_str5 = 'I\'m ww'
print(my_str5)
my_str6 = 'I\\\'m ww'
print(my_str6)
# I'm ww
# I\'m ww

获取字符串一个字符
str1 = 'abcdefg'
print(str1[0])
print(str1[-1])
print(str1[-2])

num = len(str1)

print(str1[num-1])

print(str1[len(str1)-1])

获取字符出纳中的多个字符
s = 'abcdefg'

print(s[0:3:1])
print(s[0:3])
print(s[:3])

print(s[4:7:1])
print(s[4:7])
print(s[-3:7])
print(s[4:])

print(s[:])

print(s[::2])
# 反转字符串
print(s[::-1])

s = "hello world and hello and hello"
num = s.find('hello')
print(num)
# 找第二次出现的
num1 = s.find('hello',num+1)
print(num1)
# 找第三次出现的
num2 = s.find('hello',num1+1)
print(num2)

str1 = "good good study"
str2 = str1.replace('g','G')
str3 = str1.replace('g','G',1)
print(str1,str2,str3)
# good good study
# Good Good study 
# Good good study

s = "hello world and hello and hello"
res = s.split('and')
print(res) # ['hello world ', ' hello ', ' hello']
res1 = s.split()
print(res1) # ['hello', 'world', 'and', 'hello', 'and', 'hello']
res2 = s.split('and',1)
print(res2) # ['hello world ', ' hello and hello']
res3 = s.split(maxsplit=1)
print(res3) # ['hello', 'world and hello and hello']

list1 = ['hello','world']

str1 = ' '.join(list1)
print(str1)
str2 = 'and'.join(list1)
print(str2)
str3 = ' and '.join(list1)
print(str3)
# hello world
# helloandworld
# hello and world

列表

列表:
list1 = list()
print(type(list1),list1)

list2 = list('hello')
print(type(list2),list2)

list3 = []
list4 = [1,2,3,'3.14',3.55,'小明',False]
print(list4)

切片
list1 = ['小明',18,1.77,True]
print(list1[0])
print(list1[-1])
print(list1[0:2:1])
print(list1[::-1])
print(len(list1))

list1 = [1,3,5,7,4,5]
if 9 in list1:
    print(list1.index(9))
else:
    print('不存在')
if list1.count(4) > 0:
    print(list1.index(4))
else:
    print('不存在')

list1 = []
list1.append('ww')
print(list1)

list1.append('xx')
print(list1)

# 下标越界了 不会报错 只是尾部添加
list1.insert(1,'aa')
print(list1)

list1.insert(1,'ll')
print(list1)

list2 = ['ss','jj']
# 拆开加进来
list1.extend(list2)
print(list1)

# 整体放进来
list1.append(list2)
print(list1)

['ww']
['ww', 'xx']
['ww', 'aa', 'xx']
['ww', 'll', 'aa', 'xx']
['ww', 'll', 'aa', 'xx', 'ss', 'jj']
['ww', 'll', 'aa', 'xx', 'ss', 'jj', ['ss', 'jj']]

join使用时注意 列表中内容必须都是字符串

list3 = ['hello' for i in range(5)]
print(list3)
list4 = [i for i in range(5)]
print(list4)
list5 = [i for i in list1]
print(list5)

for i in range(1,10,3):
    print(i)# 1 4 7
判断元素是否是以'e'结尾

if list1[-1] == 'e'

结尾 -1

import random
num = int(input())
是否数字中包含7
if str(num).find('7')!=-1:
    print(num)

# 随机抽取列表中元素
list1 = ['ww','aa','vv','bb']
n = random.randint(0,3)
print(list1[n])

sum([1,2,3]) # 6

# 截取字符串末尾两个字符
print(msg[-2:])
print(msg[::-1])

  • 18
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值