1.print函数
print(2.3334) # 输出数字
print(3 + 2) # 输出算数结果
print(‘String’) # 输出字符串
fp = open(‘D:/text.txt’, ‘a+’) # 打开一个文件流
print(‘Hello world!’, file=fp) # 注意点file= 流
fp.close()
print(‘hello’, ‘world’, ‘python’) # 一行输出
print(‘hello\nworld’)
print(‘http:\\www.baidu.com’) # 打开网页的写法
print(r’hello\nworld’)
s = ‘hello’
n = len(s)
print(‘字符为:%s 长度为:%d’ % (s, n))
name_01 = ‘A’
name_02 = ‘B’
name_02 = name_01
print(name_02, name_01)
n = n * 10
n *= 10
a = 10
a = 20
print(a) # 20 再一次赋值会让a指向内存中的新的位置
str1 = ‘a’
str2 = “b”
str3 = ‘’‘c’’’
str4 = str1 + str2
str5 = str4 * 3 # 即3个连在一起的Str4的字符
print(str4, str5)
demo_tuple = (1, 2.3, “hello”, 4, 5, 6)
demo_list = [1, 2, 3, ‘world’, 4, 5, 6]
print(demo_tuple[3], demo_tuple[3])
dome_dict = {1: “a”, “2”: “b”, 3: [1, ‘b’, 2]}
print(dome_dict[‘2’])
dict1 = {} # 空字典,不是集合
none_dict = set() # 空集合
dome_set1 = {1, 2, 3, 4, “a”, “b”, “ddd”}
dome_set2 = (1, 4, 6, 8, “a”, “c”)
if ‘ddd’ in dome_set1:
print(“dome_set1中有ddd”)
else:
print(‘没有’)
dome_list3 = [5, 6, 7, ‘a’, ‘b’, 5, 6, 7]
dome_set3 = set(dome_list3)
print(dome_set2, dome_set3)
data1 = 23
data2 = ‘23’
n = 1
while n < 10:
n = n + 1
print(n)
dome_tuple3 = (1, 2, 3, 5)
for i in dome_tuple3:
print(i)
for j in range(9): # i从0-9 不包含9
print(j)
print(iter([“l”, “i”, “s”, “t”]), iter((“t”, “u”, “p”, “l”, “e”)), iter(“string”), iter((“abc”, “efg”)))
fil = open(“D:/text.txt”, “ab+”)
content = fil.read(4)
content1 = fil.readline()
content2 = fil.readlines()
print(content, content1, content2)
content4 = fil.readline(3)
print(content4)
fil.close()
try:
1 / 0
except ZeroDivisionError: # 一次只能写有一个错误类型
print(“有错误”) # 有这个错误执行
else:
print(“没有这个错误”) # 没有这个错误执行
finally:
print(“不管有没有错误都执行”)
def fun_1(num1, num2): # 函数本身不需要写返回类型
print("")
return num1 + num2 # return返回的值是被传到被调用的地方,如果不写return则返回None
s = fun_1(3, 4) # 函数的调用
print(s)
class Person(object):
def man(self):
print(“我是类的方法,”)
p = Person() # 生成一个对象
p.man() # 对象调用方法
#import main 导入main模块,这种导入使用main中的函数要 main.fun()
#import main as b 导入main 命名为b 使用 b.fun()
#from main import fun/* 导入main中的一个函数或者全部,调用函数直接是 fun()