【Python】基础篇
目录
1 输出与输入
print(1,2,3,end="") # 以空格分隔,不换行
print("ok") # 1 2 3ok
s=input("请输入你的名字:") # 相当于c++中 cout<<"请输入你的名字:"; cin>>s;
print(s+",你好!")
a=int(input()) # 每次输入一行
b=int(input()) # 输入的 字符串 整型化
print(a+b)
2 输出格式控制
h = 1.746
print("My name is %s, I am %.2fm tall." % ("tom",h)) #My name is tom(%s), I am 1.75(%.2f 保留小数点后两位)m tall.
print("My age is %d." % 18) #My age is 18.
print("%d%s" % (18,"hello")) #18hello
print("%.2f,%.2f" % (5.225,5.325)) #5.22,5.33
3 关系运算符和逻辑运算符
a=4
print(2<a<6<8) #True
print(a==4) #True
print(2<a>5) #False
b=a<6
print(b) #True
print(b==1) #True
a="abc"
print(a=="abc") #True
print(a<"acd") #True
print(a<"abcd") #True
print(a>"Abc") #True
n=4
n >= 2 and n < 5 and n%2 == 0 #True
print(5 and False) #False
print(4 and True) #True
#空字符空表相当于False 但除0外都不等于False
#非零数与非空字符或表相当于True 但除1外都不等于True
# True == 1
# False == 0
# "" != False
# 2 != True
# [] != False
# [2,3] != True
print(not 4 < 5 and 4 > 6) #False 前面优先
print(not []) #True
print(not [1]) #False
#优先级 not > and > or
print(3 < 4 or 4 > 5 and 1 > 2) #True
print((3 < 4 or 4 > 5)and 1 > 2) #False
4 算术表达式
print(10/8) #1.25
print(10%8) #2
print(15/4) #3.75
print(15//4) #3
print(3.4/2.2) #1.5454545454545452
print(3.4//2.2) #1.0
print(2**3) #8 2的3次方
print(-9//4) #-3 往小取整
# / 的运算结果都是小数 哪怕能整除
print(10/2) #5.0
#有小数的算术表达式,结果一定是小数
print(10.0-10) #0.0
print(2+10/5) #4.0
#赋值
a=6
a/=3
print(a) #2.0
a**=3
print(a) #8.0
5 基本要素与字符串
a,b="he",12
a,b=b,a #交换
print(a,b)
c="ABCD" # 从左到右编号0,1,2,……,n-1 从右到左编号-1,-2,-3,……,-n
print(c[-1],c[0]) # 输出D A
a="ABCD"
b="1234"
a=a+b #连接
print(a) #ABCD1234
a=a+a[1]
print(a) #ABCD1234B
print("AB" in a) # 输出:True
print("12" not in a) # 输出:False
6 字符串和数的转换
a=15
b="12"
print(a+int(b)) # 27
print(str(a)+b) # 1512
c=1+float("3.5")
print(c) # 4.5
print(3+eval("4.5")) # 7.5 eval(x):将字符串x看作一个python表达式
print(eval("3+2")) # 5
print(eval("3+a")) # 18
7 条件分支语句
if "ok":
print("ok") # ok
if "":
print("no") # 无输出
a=[4,2]
if a:
print(a) # [4,2]
if 20:
print(20) # 20
if 0:
print(0) # 无输出
#嵌套
a = int(input()) # 大于零奇数good
if a > 0:
if a % 2:
print("good")
else:
print("bad")
else:
print("bad")
#温度转换
tmpStr = input("请输入带有符号的温度值:")
if tmpStr[-1] in ['F','f']: #如果输入华氏温度
C = ((float(tmpStr[0:-1]))-32)/1.8 #字符串切片:s[x:y]是s的从下标x到下标y的左边那个字符构成的子串 e.g. print("12345"[1:3])即输出23
print("转换后的温度是" + str(C) + "C")
elif tmpStr[-1] in "Cc":
F = 1.8 * eval(tmpStr[0:-1])+32
print("转换后的温度是" + str(F) + "F")
else:
print("输入格式错误")
8 列表
empty = [] #空表
list1 = ['Go', 'Run', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7]
print("list1[0]:", list1[0]) # list1[0]:Go
list2[2] = 2001 # 更改了列表中下标为2的元素(第三个元素)
a = 2
print("更新后的第三个元素为:", list2[a]) # 变量也可以做下标
lst = [1, 2, 3, "4", 5] # 判断
print(4 not in lst, 3 in lst, "4" in lst) # True True True
#将s经空格、制表符、换行符分隔得到的所有子串制为列表
print("34\t\t45\n7".split()) # ['34', '45', '7'] \t是制表符,\n是换行符
print("ab cd hello".split()) # ['ab', 'cd', 'hello']
s = "12 34"
lst = s.split()
print(lst) # ['12', '34']
#输入两个整数并求和
s = input()
numbers = s.split()
print(int(numbers[0]) + int(numbers[1]))
#题:print(input().split()[2][1]) 输入为abc def 1234 567 问输出为?
print(input().split()[2][1]) # 即['abc','def','1234','567']中第三个元素的第二个元素
#答案:2
9 全局变量与局部变量
x=4 #全局的x
def f0():
print("x in f0:",x) #该x是全局的x
def f1():
x=8 #该x是局部的x,不会改变全局的x
print("x in f1:",x)
def f2():
global x #说明本函数中的x都是全局的x
print("x in f2:",x)
x=5
print("x in f2:",x)
def f3():
print("x in f3:",x)
#此处增添语句:x=9 #错误 相冲突
f0()
f1()
print(x)
f2()
print(x)
f3()
# x in f0: 4
# x in f1: 8
# 4
# x in f2: 4
# x in f2: 5
# 5
# Traceback (most recent call last):
# File "d:/AAA 编程/python/课4/全局变量与局部变量.py", line 21, in <module>
# f3()
# File "d:/AAA 编程/python/课4/全局变量与局部变量.py", line 13, in f3
# print("x in f3:",x)
# UnboundLocalError: local variable 'x' referenced before assignment
10 一些函数
def Max(x,y): #x、y为形参
if x>y:
return x
else:
return y
n=Max(4,6) #4,6为实参
print(n,Max(20,n))
print(Max("about","take"))
# 6 20
# take
#判断是否是素数
def IsPrime(n):
if n<=1 or n%2==0 and n!=2:
return False
elif n==2:
return True
else:
for i in range(3,n,2):
if n%i==0:
return False
if i*i>n:
break
return True
for i in range(100):
if(IsPrime(i)):
print(i,end=" ")
# 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
#不返回值函数
#返回多个值的函数
def A(x,y):
return x+y,x-y
s,d=A(10,5)
print(s,d)
# 15 5