python-入门教程(-)

# hello world
print("hello world")

# 变量
msg = "使用变量"
print(msg)

# 字符串大小写变换(仅针对英文)
name = "liuzhongchao"
print(name.title()) #首字母大写
print(name.upper()) #全部字母大写
print(name.lower()) #全部字母小写

# 拼接字符串
first_name = "liu"
last_name = "zhongchao"
full_name = first_name+last_name
print(full_name)

# 制表符和换行符的使用
print("欢迎来到召唤师峡谷!")
print(" 欢迎来到召唤师峡谷!") #文本向后缩进一个单位
print(" 欢迎来到召唤师峡谷!") #文本换行

# 删除字符串两端的空白
msg = " 看到后面有空格了么? "
msg = msg.strip()
print(msg)

# 数字变量的正确文本显示
#num里面存储的是数字,在打印的时候不能直接引用数字,需要将其转化为文本,使用“+”进行拼接的时候,拼接的各个内容的类型要相同
num = 2
print("数字"+str(num))

# 列表
heroes = ["张三", "李四", "王五"]
print(heroes)
print(heroes[0])
print(heroes[1])
heroes[0] = "朱八" #修改列表
print(heroes)
heroes.append("王九") # 在列表末尾新增加元素
print(heroes)
heroes.insert(0,"兰陵王") # 在列表指定位置插入元素 inerst(a,b),a代表的是插入的位置,b代表插入的内容
print(heroes)
del heroes[0] #删除列表元素
print(heroes)
heroes.remove("李四") # remove方法是删除特定值元素,并不需要知道元素的位置
print(heroes)
length = len(heroes) #计算列表中元素的计数项
print(length)

heroes = ["张三", "李四", "王五"]
for a in heroes: #遍历列表
  print(a)

# input 提示用户输入,如果不输入,程序将停滞,不继续向下执行
user1=input("请输入您的用户名:")
print(user1)

# if 语句
user = "zhongchao"
if user1 == user :
  print("用户名正确")
else:
  print ("用户名错误")

#if elif else 语句
num = 1
if num <=0 :
  print("数字小于0")
elif num>0 and num <=100 :
  print("数字大于0小于100")
else :
print("数字大于100")

#while 循环 (计算 1 到 100 的总和)
n = 100
sum = 0
counter = 1
while counter <= n:
  sum = sum + counter
  counter += 1
  print("Sum of 1 until %d: %d" % (n, sum))

#for语句
languages = ["C", "C++", "Perl", "Python"]
for x in languages:
  print(x)

#函数
def userFun(name):
return "用户名为:"+name

name = userFun(user1) #函数调用
print(name)

原文地址:https://www.cnblogs.com/liuzhongchao/p/8310615.html