def python基本语法():
x, y, z = "Orange", "Banana", "Cherry"
x = y = z = "Orange"
"(多行)字符串,没有被赋值使会被忽略"
# 如果要在函数内部更改全局变量,请使用 global 关键字。
global gx
gx = "global value fixed"
# 随机数(整数:0,1,2,3,4,5,6,7,8,9)
import random
print(random.randrange(0, 10))
# 多行字符串,也可以用三个单引号'''
a = """Python is a widely used general-purpose, high level programming language.
It was initially designed by Guido van Rossum in 1991
and developed by Python Software Foundation.
It was mainly developed for emphasis on code readability,
and its syntax allows programmers to express concepts in fewer lines of code."""
print(a[0]) # 字符串是数组
a.strip() # strip()方法删除开头和结尾的空白字符:
a.lower()
a.upper()
a.replace("Python", "Java")
a.split(" ")
print("My name is {}, and I am {}".format("Alex", 100))
print("My name is {1}, and I am {0}".format(100, "Alex"))
if 5 > 2 and 9 > 0:
pass # 各种语句不能为空时,可以用pass
elif 5 < 4:
print("case2")
else:
print("case3")
a = 10
if 5 > 2: print("case1") # if简写
print("case1") if 5 > 2 else print("case2") # if-else简写
i = 1
while i < 7:
print(i)
if i == 5:
break
i += 1
for x in range(10):
print(x)
for x in range(3, 10):
print(x)
for x in range(3, 50, 6):
print(x)
else:
print("for end")
try:
print(x)
except:
print("An exception occurred")
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
# x = -1
# if x < 0: # 抛出异常。
# raise Exception("Sorry, no numbers below zero")
# x = "hello"
# if not type(x) is int:
# raise TypeError("Only integers are allowed")
quantity = 3
itemno = 567
price = 52
myorder = "I want {0} pieces of item number {1} {1} for {2:.2f} dollars."
print(myorder.format(quantity, itemno, price))
myorder = "I have a {carname}, it is a {model}."
print(myorder.format(carname="Porsche", model="911"))
print("Enter your name:")
x = input()
print("Hello ", x)
python数据类型
def python数据类型():
x = "Hello World" # 文本类型: str
x = 29 # 数值类型:int
x = 29.5 # 数值类型:float
x, a, b, c = 1j, 27e4, 15E2, -49.8e100 # 数值类型:complex
x = ["apple", "banana", "cherry"] # 序列类型:list
x = ("apple", "banana", "cherry") # 序列类型:tuple
x = range(6) # 序列类型:range
x = {"name": "Bill", "age": 63} # 映射类型:dict
x = {"apple", "banana", "cherry"} # 集合类型:set
x = frozenset({"apple", "banana", "cherry"}) # 集合类型:frozenset
x = True # 布尔类型:bool
x = b"Hello" # 二进制类型:bytes
x = bytearray(5) # 二进制类型:bytearray
x = memoryview(bytes(5)) # 二进制类型:memoryview
# 类型转换
print(8 > 7)
print(bool("Hello")) # false: ()、[]、{}、""、0、None,其它都是true
print(type(x))
a = float(1)
b = int(2.2)
c = complex(3.0)
d = str(4)
def python运算():
a = 1 + 1
a = 1 - 1
a = 1 * 1
a = 1 / 1
a = 102 % 10 # 取余 = 2
a = 2**10 # 取幂 = 1024
a = 67 // 10 #整除 = 6
x = 10
a = x > 3 and x < 10
a = x > 3 or x < 4
a = not (x > 3 and x < 10)
a = "Python" in "Python is a language"
a = "Python" not in "Python is a language"
def python文件操作():
# 文件处理: r读取a追加w写入x创建 t文本b二进制
filePath = "Python学习/fileTest.txt"
f = open(filePath, "xt")
f.write("我是测试文件中的内容")
f.close()
f = open(filePath, "rt")
# print(f.read())
# print(f.readline())
for x in f:
print(x)
import os # 删除文件夹(空):os.rmdir()
if os.path.exists(filePath):
os.remove(filePath)
print("delete file")
else:
print("The file does not exist")