Python学习记录-day1

将数据输入到文件中
fp = open(“E:/tet.doc”, ‘a+’)
print(‘hello测试’, file=fp)
fp.close()
变量定义

name = ‘测试’
name2 = 90 # 变量多次赋值
print(name)

from decimal import Decimal

n1 = 1.1
n2 = 2.2
print(n1 + n2) # 注意精度区别
print(Decimal(‘1.1’) + Decimal(‘2.2’))

将str转换int 时 必须为数字串 不能有小数

输入函数input

gift = input(“需要啥呢?\n”)
print(gift)
a = int(input(“整数1”))

b = int(input(‘整数2’))
print(a + b)

一正一副向下取余

print(9 / 3)
print(9 // 3)

顺序结构
选择结构
range 的使用方法
import numpy as np

r = np.arange(0.1, 1, 0.1)
print®
print(list®)
if 1 in list®:
print(‘存在’)
else:
print(‘不存在’)
for _ in range(2):
print(“qwe”)

lst = [i ** 2 for i in range(11)]
print(lst)

字典是{ } 键值对
zip打包函数
items = [‘a’, ‘d’, ‘f’]
prices = [12, 12, 23]
d = {item.upper(): price for item, price in zip(items, prices)}
print(d)
元组 () tuple t3=(‘qweqwe’,) 一个元素需要加上逗号 元素不可以修改

扫描二维码关注公众号,回复: 13290440 查看本文章

a = (1, 2, [1, 2, 3], 4)
a[2][0] = 4
print(a)

集合 {} 没有重复值 有子集 超集 交集 求交集 并集等操作函数调用
字符串操作 有很多操作 具体的查看API
字符串格式化输出
name = ‘测试姓名’
age = 12
print(“我是{0},今年{1}岁” .format (name, age))
函数的创建和调用

def calc(a, b):
c = a + b
return c

print(calc(1, 2))

异常捕获 try expect else

类的使用 类名称的第一个字母大写 其余小写
class Student:
naspcae = ‘吉林’

def __init__(self, name, age):
    self.name = name
    self.age = age

def eat(self):
    print('吃饭')

@staticmethod
def method():
    print('静态方法')

@classmethod
def cm(cls):
    print('类方法')

创建对象
stu1 = Student(‘张三’, 20)

stu1.eat()

stu1.method()

stu1.gender = ‘女’ # 可以动态绑定对象和方法
stu1.cm()
print(stu1.gender)

封装 继承 多态
__双下划线表示不希望被外部调用使用 可以通过_类名__对象查询使用 方法重写
strs = list(map(int, input().split()))
print(strs[0] + strs[1])
import numpy as np
import matplotlib.pyplot as plt

n = 1000
x = np.random.randn(1000)
y = x ** 2 + 10
plt.scatter(x, y)
plt.show()
loadtxt 加载数据 scasand散点图 plot折线图 bar条形图 hist 直方图 pie饼状图

猜你喜欢

转载自blog.csdn.net/weixin_44135909/article/details/120402564