python编程实例
01-Hello World
01-Hello World
python 的语法逻辑完全靠缩进,建议缩进4个空格
如果是顶级代码,必须顶格书写,哪怕只有一个空格也会语法错误
下面实例中,满足if条件要输出两行内容,这两行内容必须都缩进,而且具有相同的缩进级别。
print("hello world")
if 3 > 0:
print("OK")
print("yes")
x = 3; y = 4 #不推荐,还是应该写成两行
print(x + y)
02-print
print("hello world") #hello world
print("hello","world") #逗号自动添加默认的分隔符 hello world
print("hello" + "world") #加号表示字符拼接 helloworld
print("hello","world",sep="***") #单词间用***分隔 hello***world
print("*" * 10) #*号重复10遍 **********
print("how are you?",end="") #默认print会打印回车,end=""表示不要回车
03-基本运算
运算符可以分为:算术运算符、比较运算符和逻辑运算符。优先级是:算术运算符>比较运算符>逻辑运算符。不过呢,没记住优先级,最好使用括号,这样不用背,也增加了代码的可读性。
print(5 / 2) #2.5 print(5 // 2) #丢弃余数,只保留商 2 print(5 % 2) #求余数 1 print(5 ** 3) #求5的3次方 125 print(5 > 3)#返回True print(3 > 5)#返回False print(20 > 10 >5) #python支持连续比较 True print(20 >10 and 10 >5) #python支持连续比较,与上面相同含义 True print(not 20 > 10) #False print(not 10 > 20) #True
True和False是关键字,区分大小写
04-input
number = input("请输入数字:") #input用于获取键盘输入 20
print(number) # 20
print(type(number)) #input获得的数据是字符型
#print(number + 10) #报错,不能把字符和数字做运算 TypeError: must be str, not int
print(int(number) + 10) #int可将字符10转换成数字10 30
print(number + str(10)) #str可将10转换为字符串后实现字符串拼接 2010
05-输入输出基础练习
username = input("username: ") #刘备
print("welcome",username) #print各项间默认以空格作为分隔符 welcome 刘备 逗号默认中间有一个空格
print("welcome " + username) #注意引号内最后的空格 welcome 刘备 第一个字符串中包含两个空格
06-字符串使用基础
python中,单双引号在字符串中使用,没有区别
sentence = 'tom\'s pet is a cat' #单引号中间还有单引号,可以转义
sentence2 = "tom's pet is a cat" #也可以用双引号包含单引号
sentence3 = "tom said:\"hello world!\""
sentence4 = 'tom said:"hello world!"' #单引号中间可以用双引号,可以转义
#三个连续的三引号或单引号,可以保存输入格式,允许输入多行字符串
words = """
hello
world
abcd"""
print(words)
#hello
#world
#abcd
py_str = 'python'
print(len(py_str)) #取长度 6
print(py_str[0]) #取第一个字符 p
print('python'[0]) #取第一个字符 p
print(py_str[-1]) #取最后一个字符 n
#print(py_str[6]) #错误,下表超出范围 IndexError: string index out of range
print(py_str[2:4]) #切片,起始下标包含,结束下标不包含 th
print(py_str[2:]) #从下标为2的字符渠道结尾 thon
print(py_str[:2]) #从开头取到下标为2之前的字符 py
print(py_str[:]) #取全部 python
print(py_str[::2]) #按照步长为2取值,默认为1 pto
print(py_str[1::2]) #以下标为1开始,步长为2取值 yhn
print(py_str[::-1]) #步长为负,表示从右往左取 nohtyp
print(py_str + 'is good') #简单拼接在一起 pythonis good
print(py_str * 3) #把字符串重复3遍 pythonpythonpython
print('t' in py_str) #True
print('th' in py_str) #True
print('to' in py_str) #False 子字符串必须连续
print('to' not in py_str) #True
07-列表基础
列表也是序列对象,但它是容器类型,列表中可以包含各种数据,列表可变
alist = [10,20,30,'bob','alice',[1,2,3]]
print(len(alist)) #6
print(alist[-1]) #取出最后一项 [1, 2, 3]
print(alist[-1][-1]) #因为最后一项是列表,还可以继续取下标 3
print(alist[-2][2]) #列表倒数第二项是字符串,还可以继续取下标 i
print(alist[3:5]) #['bob','alice']
print(10 in alist) #True
print('o' in alist) #False
print(100 not in alist) #True
alist[-1] = 100 #修改最后一项的值
print(alist) # [10, 20, 30, 'bob', 'alice', 100]
alist.append(200) #向列表中追加一项
print(alist) #[10, 20, 30, 'bob', 'alice', 100, 200]
08-元组基础
元组与列表基本上是一致的,只是元组不可变,列表可变
atuple = (10,20,30,'bob','alice',[1,2,3])
print(len(atuple)) #6
print(atuple[2]) #30
print(atuple[-1]) #[1, 2, 3]
print(atuple[3:5]) #('bob','alice')
print(10 in atuple) #True
atuple[-1] = 100 #错误,元组是不可变的 TypeError: 'tuple' object does not support item assignment


返回顶部
刷新页面
下到页底