name=" Li Wasai "
print(f"Hello {name.title().strip()},would you like to learn some Python today?")
name="li wenhao"
print(name.title())
print(name.upper())
print(name.lower())
print("languages:\n\tPython\n\tC\n\tJava")
name=" Albert Einstein "
print(name.lstrip())
print(name.rstrip())
print(name.strip())
整数大家都知道,浮点数其实就是小数点可以出现在数的任意位置。
number=1000_000_000
print(number)
x,y,z=0,1,2
[]:表示列表,并用逗号分隔其中的元素,索引从0而不是1开始
fruits=['apple','orange','banana','pear']
print(fruits) #访问列表
print(fruits[0]) #访问列表第一个元素
fruits=['apple','orange','banana','pear']
fruits.append('strawberry')
print(fruits)
fruits=['apple','orange','banana','pear']
fruits.insert(0,'strawberry') #把'strawberry'添加到第一个元素
print(fruits)
fruits=['apple','orange','banana','pear']
del fruits[0] #删除第一个元素
print(fruits)
fruits=['apple','orange','banana','pear']
fruit=fruits.pop([1]) #删除列表的第二个元素,并返回第二个元素给fruit变量
print(fruit)
print(fruits)
fruits=['apple','orange','banana','pear']
fruit='banana'
fruit=fruits.remove(fruit)
print(fruits)
为了能更好的理解这几条语句,编写了一个大合集
party_participator=['Monica','Phoebe','Ross','Chandler','Joey']
party_participator.insert(0,'Rachel')
party_participator.append('Janice')
del party_participator[1]
party_participator.remove('Chandler')
name=party_participator.pop(-2)
print(party_participator)
fruits=['apple','orange','banana','pear']
print(sorted(fruits)) #临时排序
print(fruits)
fruits.sort() #永久排序
print(fruits)
fruits.reverse() #顺序颠倒
print(fruits)
print(len(fruits))
使用for循环,从列表中从前往后依次取出一个元素赋给fruit,执行相同的操作,注意缩进:
fruits=['apple','orange','banana','pear']
for fruit in fruits:
print(fruit)
for value in range(1,5) #从1到4
print(value)
numbers=list(range(1,6)) #从1到5
print(numbers)
numbers=list(range(1,10,2)) #加入步长,列表相邻的数值相差2
print(numbers)
numbers=[1,2,3,4,5,6,7,8,9,10]
print(min(numbers)) #列表最小值
print(max(numbers)) #列表最大值
print(sum(numbers)) #对列表元素求和
squares=[value**2 for value in range(1,11)]
print(squares)
fruits=['apple','orange','banana','pear']
print(fruits[0:3]) #该切片包含从0到2三个元素
for fruit in fruits[0:3]:
print(fruit)
fruits=['apple','orange','banana','pear']
copy_fruits=fruits[:]
print(copy_fruits)
numbers=(1,2,3,4,5)
for number in numbers:
print(number)