基本概念和数据类型

变量:使用英文字母,下划线和数字组成
要求:见名知意
注意:数字不能开头,变量名中间不能有空格和点符号,关键字、
内置函数不能做变量名。

引用和回收:pig=15,当pig再被赋值为16时,15的内存被回收。-1~256不会被回收 常用的

>>> pig=15
>>> pig
15
>>> id(pig)
1723053568
>>> pig=16
>>> pig
16
>>> id(pig)
1723053600

关键字:关键字不能做变量名

>>> import keyword
>>> print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

内置函数(BIF):内置函数也不能做变量名

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']

程序执行的三大流程:顺序执行 选择执行 循环执行
输入函数:input是用户和python代码最基本的交互
注意:input接收值,会转成字符串类型

in_a = input('请输入内容:')
print(in_a)

==================== RESTART: C:\Users\Guai\Desktop\1.py ====================
请输入内容:i love you
i love you
>>> 

输出函数: 默认end = “\n” \n表示换行

print('i love you',end = ' & ')
print('i love you')
print('i love you')

==================== RESTART: C:\Users\Guai\Desktop\1.py ====================
i love you & i love you
i love you

数值类型
整型 int

>>> type(1)
<class 'int'>

浮点数 float

>>> type(2.3)
<class 'float'>

布尔型 bool

>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> True + False
1

复数 complex
#type()查看数据类型
#+ - * /:加减乘除
#% 取余 // 取整 **幂运算

字符串:str字符串 单引号 双引号 三引号可以换行

str1 = ('你没有对象')
str2 = ("他没有对象")
str3 = ('''
我没有对象
        ''')

字符串拼接:+号拼接 %s占位符拼接 format()函数:简写f’{}’ join([])

str1 = ('你没有对象')
str2 = ("他没有对象")
str3 = ('''我没有对象
 ''')
print(str1+str2)
print('%s&%s&%s'%(str1,str2,str3))
print('{}&{}&{}'.format(str1,str2,str3))
print(f'{str1}&{str2}&{str3}')
print('&'.join([str1,str2,str3]))

==================== RESTART: C:\Users\Guai\Desktop\1.py ====================
你没有对象他没有对象
你没有对象&他没有对象&我没有对象
 
你没有对象&他没有对象&我没有对象
 
你没有对象&他没有对象&我没有对象
 
你没有对象&他没有对象&我没有对象

字符串的格式化

>>> '%6d' % 123 #%d 格式化整型
'   123'
>>> '%09.2f' % 357.268 # %f 格式化浮点数
'000357.27'
>>> '%c' % 97 # 格式化成ASCLL码
'a'
>>> '%o' % 8 # 八进制输出
'10'
>>> '%e' % 10000 # %e 格式化输出科学计数法
'1.000000e+04'
>>> '%r' % '123' # %r 对象原样输出
"'123'"
>>> 
>>> '%s' % '你好'
'你好'

高精度计算
ipmort 导入
import decimal
decimal 高精度模块 传的值必须字符串类型

import decimal
num = decimal.Decimal('1.155') - decimal.Decimal('1')
print(num)

==================== RESTART: C:\Users\Guai\Desktop\1.py ====================
0.155
>>> 

math模块
import math
print(math.pi)

>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

就不会吃辣辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值