Python123第三周上

基本数据类型

3.1数字类型及操作
3.2 实例3,天天向上的力量
3.3字符串类型及操作
3.4模块2,time库的使用
3.5实例4:文本进度条

3.1数字类型及操作
整数类型: 与数学中正数概念一致;
特点1:可正可负,没有取值范围限制
特点2,:pow(x,y),计算x的y次方,想算多大算多大

数字类型及操作:
十进制,二进制(0b或0B开头),八进制(0o或0O开头),十六进制(0x或0X开头)
浮点数类型: 与数学中的实数的概念一致;
特点1,带有小数点及小数的数字;
特点2,浮点数取值范围和小数精度存在限制,但常规计算可以忽略;
特点3,取值范围数量级为10的308次方,精度数量级为10的-16次方;

数字类型及操作:
1,注意:浮点数间的运算存在不确定尾数,不是bug;
如0.1+0.2=0.30000000000000000000004,计算机中二进制和十进制不存在严格的等价关系;计算机内53位二进制表示小数部分,二进制表示小数,可以无限接近,但不完全相同;
2,round(x,d):表示对x四舍五入,d是小数截取位数;
3,浮点数间运算及比较用round()函数辅助;
4,不确定尾数一般发生在10的-16左右,round()十分有效;
5,浮点数可以采用科学计数法表示:利用字母e或者E作为幂符号,以10为基数4.3e-3;

复数类型: 与数学中的复数一致;(a+bj)
z=a+bj;在python中,可以用z.real获得实部;z.imag获得虚部;
数值运算操作符: 是一种完成运算的符号体系;
x/y 表示除(浮点数结果),10/3结果为3.33333333333;
x//y表示整数除,10//3结果为3;
+x表示x本身;
-y表示y的负值;
x%y表示余数,模运算,10%3的结果为1;
x**y 表示幂运算,当y是小数时,开方运算,10*0.5结果是根号10;

二元操作符对应的增强赋值操作符:
x op = y,即为x = x op y;如,x+=3表示x=x+3;
类型间可进行混合远算,生成结果为“最宽”类型;
三种类型存在一种逐渐“扩展”或“变宽”的关系:
整数<浮点数<复数,如123+4.0 = 127.0
数值运算函数:一些以函数形式提供数值运算功能;
abs(x),divmod(x,y)表示商余操作,同时输出整数商和余数
pow(x,y[,z]), […]表示该参数可以省略;
如:pow(3,pow(3,99),10000)结果为4587
max(1,9,5,4,3);min(3,2,1)
数字类型转换的函数:
int(x),将x变为整数,舍弃小数部分;
如:int(123.45)结果为123;int(“123”)结果为123
float(x),将x变为浮点数,增加小数部分
如float(12)结果为12.0,float(“123”)结果为123.0
complex(x),将x表示为复数,增加虚数部分
如:complex(4)结果为4+0j

3.2 实例3,天天向上的力量

dayup = 1.0
dayfactor = 0.01 #使用变量,一处修改即可

for i in range(365):
    if i % 7 in [6,0]:
        dayup = dayup*(1-dayfactor)
    else:
        dayup = dayup*(1+dayfactor)

print("dayup:{:.2f}".format(dayup))
#编写函数,
def dayUP(df):
    dayup =1
    for i in range(365):
        if i % 7 in [6,0]:
            dayup = dayup*(1-0.01)
        else:
            dayup = dayup*(1+df)#每天进步多少
    return dayup #返回最终函数值
#相当于定义一段函数输出一年进步多少,参数为df
dayfactor = 0.01
while dayUP(dayfactor) < 37.78:
    dayfactor = dayfactor + 0.001
print("工作日努力参量:{:.3f}".format(dayfactor))
### Python123 Course Week 6 Materials and Content For the sixth week of the Python123 course, students are expected to delve deeper into advanced topics that build upon foundational knowledge acquired during earlier weeks. The focus shifts towards more complex programming concepts such as object-oriented programming (OOP), file handling, and possibly an introduction to data structures like lists, dictionaries, sets, and tuples. #### Object-Oriented Programming (OOP) Students learn about classes and objects, encapsulation, inheritance, polymorphism, and abstraction. Understanding these principles is crucial for writing maintainable and scalable code[^2]. ```python class Animal: def __init__(self, name): self.name = name def speak(self): raise NotImplementedError("Subclasses must implement abstract method") class Dog(Animal): def speak(self): return f"{self.name} says Woof!" dog_instance = Dog('Buddy') print(dog_instance.speak()) ``` #### File Handling The curriculum covers reading from and writing to files using various methods available in Python. This includes opening a file, performing operations on it, and ensuring proper closure after completion. ```python with open('example.txt', 'w') as file: file.write('Hello World\n') with open('example.txt', 'r') as file: content = file.read() print(content) ``` #### Data Structures Advanced usage of built-in data types including list comprehensions, dictionary manipulations, set operations, and tuple unpacking techniques are explored extensively throughout this period. ```python # List comprehension example numbers = [i * i for i in range(5)] print(numbers) # Dictionary manipulation person_info = {'name': 'Alice', 'age': 30} person_info['city'] = 'New York' print(person_info) ``` Before proceeding with assignments or projects related to this material, participants should review all relevant tutorials and exercises provided up until now to reinforce their understanding. Additionally, adherence to academic integrity policies remains paramount when completing coursework activities[^3].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值