随笔 - 75
文章 - 0
评论 - 0
阅读 -
17万
随笔分类 - Python
Python 生成随机数
摘要:import random x = int(input('Enter a number for x: ')) --随机数最小值y = int(input('Enter a number for y: ')) --随机数最大值 n = int(input('How many numbers do yo
阅读全文
异常处理
摘要:--raise >>> raise IOError('This is a test!') Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> raise IOError('This is a test!
阅读全文
类
摘要:>>> class Person: def __init__(self, name, age): self.name = name self.age = age def display(self): print(self.name, self.age) >>> p = Person('john',
阅读全文
文件处理
摘要:os.getcwd() 表示当前目录,可以换成其他目录路径 --返回当前目录中的文件夹和文件 >>> import os >>> os.listdir(os.getcwd()) ['01成品代码', '@test.py', 'addtofile1.py', 'class.py', 'class1.p
阅读全文
字符串格式
摘要:--设置字符串格式 >>> x = 0.123456789 >>> print('value : %.2f' % x) value : 0.12 >>> >>> x = 0.123456789 >>> y = 0.123456789 >>> print('x = %.2f y = %.3f' % (
阅读全文
if
摘要:# if.py pwd = input('What is the password? ') if pwd == 'apple': print('Logging on ...') else: print('Incorrect password.') 运行结果: >>> What is the pass
阅读全文
for
摘要:>>> for i in range(5): print(i) 0 1 2 3 4 >>> for i in range(0, 5): print(i) 0 1 2 3 4 >>> for i in range(1, 5): print(i) 1 2 3 4 >>> for i in range(5
阅读全文
数据结构
摘要:--type命令 >>> type(5) <class 'int'> >>> type(5.0) <class 'float'> >>> type('abc') <class 'str'> >>> type(None) <class 'NoneType'> --序列 字符串、元组、列表 正索引从0开
阅读全文