作业1:day1

name = ' aleX'
print(name.strip())
print(name.startswith('al'))
print(name.endswith('X'))
print(name.replace('l', 'p'))
print(name.split('l'))
print(name.upper())
print(name.lower())
print(name[1])
print(name[0:3])
print(name[3:5])

i = 0
while i >= 0:
    if name[i] == 'e':
        break
    i += 1
print('e所在索引位置为:', i)

name = name[0:len(name)-1]
print(name)
list1 = ['tank', 18, '广东']
print(list1[2])
print(list1[-1])
str1 = 'my name is xxx   '
print(str1[3:7])
# 切片(顾头不顾尾,步长)
print(str1[3:5])
print(str1[3:10:2])  #3~(7-1)
# len长度

print(len(str1))
# 成员运算in / not in
print('x' in str1)
print('x' not in str1)
# 移除空白strip()
# 去除字符串左右的空格
print(str1.strip())
# split()会根据括号中规则去切分字符串,然后把每个值追加到列表中
print(str1.split(' '))
# print 默认参数end='\n';
# end="" 可以自定义参数
for line in str1:
    print(line, end='')
# lstrip 去除左边的空格或指定符号
# rstrip 去除右边的空格或指定符号
str2 = '**my name is xxx   '
print(str2.rstrip())
print(str2.lstrip('*'))
# 全部转换成小写 lower()
# 全部转换成小写 upper()
str3 = 'My name is xxx '
print(str3.upper())

# startswith ,endswith
# 判断字符串首尾是否成立
print(str3.startswith('My'))
print(str3.endswith('My'))
# format
# 按照位置替换
# 按照所以替换
# 按照名字替换(指名道姓)
str7 = 'my name is {name},my age is {age}'
str7 = str7.format(name='lyj', age=18)
print(str7)
str8 = 'my name is {1},my age is {2}'
str8 = str8.format('lyj', 18, 21)
print(str8)
# join 对字符串拼接,会对列表中数据按照某种标识拼接
name = 'lyj'
age = '18'
print(':'.join([name, age]))
# split,rsplit  :从左边开切,从右边开始切

# replace:括号里面第一个参数是旧参数,第二个是新参数,量参数替换
str8 = 'old new'
str8 = str8.replace('old', 'python')
print(str8)

# isdigit:判断字符串是否是数字
ID = input("请输入:")
print(type(ID))
print(ID.isdigit())
# 列表的内置方法
# 1.按索引取值,正向从左边,负数从右边开始
list = ['lyj', 18, 'female', 3.0, 9]
print(list[4])
print(list[-2])

# 切片,顾头不顾尾,步长(5-1)

print(list[1:5])
print(list[1:5:2])

# 长度len
print(len(list))

# 成员运算in 和not in
print('lyj' in list)
print('lyj' not in list)

# 追加
# 在列表末尾追加值
list.append('hhh')
print(list)

# pop删除
# pop 取出列表中值
# remove 直接删除列表中的值
print(list.pop())  #默认-1
print(list)
print(list.pop(0))  #lyj
level = list.pop()
print(list)

name = list.remove('lyj')print(name)print(list)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# print('hello world!')
'''
print('hello world!')
'''

name = 'lyj'
print(name)


# id,type,value
x = 10
y = 10

'''
python小计数池(python优化机制):
    在变量值产生后指定的范围内,
    在内存中事先开辟一块内存空间,
    然后分别把指定范围内的变量存放进去。
'''
print(id(x))
print(id(y))

name = 'lyj'
print(type(x))
print(type(name))

# 判断值是否相等
print(x == y)

# 判断id是否相等
print(x is y)


# 常量(常量也是变量,在python中大家都规定只要是大写的变量都称之为常量)
SCHOOL = '安徽工程大学'


# 用户与程序交互
name = input('input your name:')
pwd = input('请输入密码:')

print(name, pwd)
print(type(name), type(pwd))

'''
python2:
    raw_input()
    
python3:
    input()

'''


# 数字类型

# 整型int
number = 100  # int(100)
number2 = int(100)
print(number)
print(number2)


# 浮点型 float
sal = 15.0  
sal2 = float(15.0)
print(sal)
print(sal2)


# 字符串类型
name = 'lyj'
name2 = "lyj"
content = '''
1yj
hello
'''
#
print(name, name2, content)

# python中字符串可以相加、相乘
print('lyj ' * 5)
print('lyj' + 'hello')
print('lyj', 'hello')


# 列表类型
# []括号内以逗号隔开,可存放多个不同类型的值。
list1 = ['lyj', 21, ['dl', 25]]
print(list1[0])
print(list1[5][0])

# 字典类型
# 在{}内,都逗号隔开,可存放多个值,每个值以key: value的形式存储
dict1 = {'name': "lyj", "age": 21}  
print(dict1["name"])


# 布尔类型
# True or False
print(10 == 10)  # True
print(10 == 11)  # False

# 注意: 所有数据类型都自带布尔, 0、None、空都为False

# 格式化输出
# 可以在输出时给字符串类型做一次替换
# %s占位符,%d可以替换数字类型
number = input('请输入:')
str1 = '''
    尊敬的用户,您的话费余额为%s元。
 ''' % number
print(str1)


# for循环
list1 = [1, 2, 3, 4, 5]
for line in list1:
    print(line)

for line in range(1, 10):
    print(line)

dict1 = {'name': "lyj", "age": 21}
for key in dict1:
    print(key)
    print(dict1[key])

猜你喜欢

转载自www.cnblogs.com/lyj68/p/11005534.html