Python入门到实战

闲话不多说,开始记录吧。

1.如何使用变量:

    name 定义为一个变量,存储了一个字符串。

name = 'miao min'

对一个字符串进行大小写的转换,可以使用:

upper()【把字符串全部变为大写】

lower()【把字符串全部变为小写】

title()【把首字母变为大写】


连接字符串使用+:

print("miao" + "min")

number = 3
print("my favorite number is " + str(number))
PS:数字和字符串连接时,需要把数字转换成str类型

数字之间的运算:

print(2+6)
print(9-1)
print(2*4)
print(24/3)

去除字符串的空白:

string = " abc "

string.lstrip()【去除左边的空白】

string.rstrip()【去除右边的空白】

string.strip()【去除所有的空白部分】

2.列表的使用

列表由一系列特定顺序排列的元素组合。

sorted(countries)【临时排序】


countries = ["Japan", "Paris", "America", "China", "Helan"]
print(countries)

print(sorted(countries))

print(countries)

print(sorted(countries,reverse=True))

print(countries)

countries.reverse()
print(countries)
countries.reverse()
print(countries)

countries.sort()
print(countries)
countries.sort(reverse=True)
print(countries)

names = ["Jay", "Jim", "Star", "Bob"]
print("Please " + names[0] + " " + names[1] + " " + names[2] + " " + names[3] + " eating food")
print(names[2] + " can't join")
names[2] = "Sanny"
print("Please " + names[0] + " " + names[1] + " " + names[2] + " " + names[3] + " eating food")

names.insert(0, "min")
print(names)

names.append("cui")
print(names)

a = names.pop()
print("sorry " + a)

b = names.pop()
print("sorry " + b)
names.pop()
names.pop()

del names[1]
del names[0]

print(names)






你可能感兴趣的:(Python)