python语言思想

本文介绍了Python语言的基础,包括解释器的选择(PyCharm)、创建项目、代码规范、数据抽象、大数据处理、基本数据类型(整数、浮点数、逻辑值、字符串)、序列(字符串和列表)、变量、动态类型、错误处理以及字典、集合等概念。同时强调了实践中需要注意的错误和异常处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python语言基础与应用
超级计算器
在这里插入图片描述
python语言解释器
为啥选用PYCHARM
在这里插入图片描述
create new project:
NANE+ 选择解释器
open ,选择打开文件或者加入project
在这里插入图片描述
注意对齐与缩进
注意字母大小写、空格
注意左右括号配对

错误是常见的,跟BUG和缺陷斗争得到过程
观察代码文本要求:注意上述的注意事项

对数据的抽象,对问题的处理采用的是对数据的抽象,避免处理其他杂糅的数据
何为大数据:
在这里插入图片描述
大数据分析处理语言
多种多样的数据类型
简单类型用来表示值
容器类型用来组织这些值
在这里插入图片描述
在这里插入图片描述
何为计算:
对现实世界处理和过程的抽象
控制流语句
定义语句:把一系列语句集合起来起一个名字 描述了一个包含一系列处理过程的计算单元 标签
基本类型:数值
整数类型:
双大小比较号 为比较判断 得到逻辑值 真或假
是很遵循数学逻辑
数的进制 所谓进制 9>0变两位加1 为10
整数的各种进制表示
0b 0o 0x
浮点数收到17位有限数字的限制
科学计数法:2.3*10^5
有效数字为2位
在这里插入图片描述
除了内置数学
还有math模块 面向整数浮点数
cmath 面向复数
基本类型:逻辑值
逻辑运算:双目运算 :与 and 或or ,
单目运算:非not:
优先级not最高 and ,or最低
关于优先级往往会用括号进行提示,并非读代码人员运用所掌握的优先级方法转换
在这里插入图片描述
not None 变为真
字符数据类型:
三个连续单引号表示多行字符串 也就是包含了换行的位
转移符号:
在这里插入图片描述
字符串有哪些操作?:
小于0 的反方向编号
在这里插入图片描述
字符串是数据的本身
名字是数据的标签
名字和字符串是“名”与“值”之间的关系
常见的字符串操作:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
序列:
字符串是一种序列
在这里插入图片描述
给数据的命名:

名字与变量
名字像一个标签
名字和数值之间的关联称为引用
在这里插入图片描述
设么是变量、何为变量?
与数值关联的名字也称作变量,
在这里插入图片描述
在这里插入图片描述
python语言是动态的语言
可以随时指向任何的类型,其他常见语言都是静态的
何为赋值? 名字与数值关联的过程,称为给变量赋值,数值也称为数据对象
在这里插入图片描述
python 是语言大师创造的语言
在这里插入图片描述
上机练习时间:
在这里插入图片描述
要更注意错误,需要学会看错误,
在这里插入图片描述

Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 4.2+2.1==6.3
False
>>> print4.2+2.1)
  File "<stdin>", line 1
    print4.2+2.1^
SyntaxError: invalid character in identifier
>>> print(4+1)
5
>>> 4.2+2.1
6.300000000000001
>>> 1+3j
(1+3j)
>>> (1+2j).real
1.0
>>> (1+2j).imag
2.0
>>> import math
>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
>>> h=88
>>> m=g="dd"
>>> m
'dd'
>>> g
'dd'
>>> h
88
>>> type(h)
<class 'int'>
>>> type (m)
<class 'str'>
>>> len(m)
2
>>> n="qwertyuiop"
>>> n[1:6]
'werty'
>>> n*2
'qwertyuiopqwertyuiop'
>>> c=n*2
>>> c
'qwertyuiopqwertyuiop'
>>> c.isalpha
<built-in method isalpha of str object at 0x000001A0A09619E0>
>>> h.isdigit
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'isdigit'
>>> n=" ppq"
>>> c=n*3
>>> c
' ppq ppq ppq'
>>> c.split('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: empty separator
>>> c.split(' ')
['', 'ppq', 'ppq', 'ppq']
>>> me="how are you, i an fine thankyou"
>>> me.split(" ")
['how', 'are', 'you,', 'i', 'an', 'fine', 'thankyou']
>>>  me[1]
  File "<stdin>", line 1
    me[1]
    ^
IndentationError: unexpected indent
>>> me[1:]
'ow are you, i an fine thankyou'
>>> me[1;2]
  File "<stdin>", line 1
    me[1;2]
        ^
SyntaxError: invalid syntax
>>> me[1:2]
'o'
>>> me[-1:-3]
''
>>> me[1:10]
'ow are yo'
>>> me[-3-1]
  File "<stdin>", line 1
    me[-3-1]
         ^
SyntaxError: invalid character in identifier
>>> me[-1:-3:1]
''
>>> me[-1:-10]
''
>>> me.upper
<built-in method upper of str object at 0x000001A0A0961940>
>>> me.upper()
'HOW ARE YOU, I AN FINE THANKYOU'
>>> "+++".join(me)
'h+++o+++w+++ +++a+++r+++e+++ +++y+++o+++u+++,+++ +++i+++ +++a+++n+++ +++f+++i+++n+++e+++ +++t+++h+++a+++n+++k+++y+++o+++u'
>>> "+".join(me,c,"nihaoma")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: join() takes exactly one argument (3 given)
>>> "+".join(me,c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: join() takes exactly one argument (2 given)
>>> "+".join([me,c])
'how are you, i an fine thankyou+ ppq ppq ppq'
>>> "".join(["123456789"])
'123456789'
>>> "+".join("123456789")
'1+2+3+4+5+6+7+8+9'
>>> "+".join(["111"])
'111'
>>> me[0]
'h'
>>> me[-1]
'u'
>>> me[1]
'o'
>>> me[2]
'w'
>>> me.in("h"0
  File "<stdin>", line 1
    me.in("h"0
       ^
SyntaxError: invalid syntax
>>> me.in("h")
  File "<stdin>", line 1
    me.in("h")
       ^
SyntaxError: invalid syntax
>>> me.in"h"
  File "<stdin>", line 1
    me.in"h"
       ^
SyntaxError: invalid syntax
>>> me.in(h)
  File "<stdin>", line 1
    me.in(h)
       ^
SyntaxError: invalid syntax
>>> hex(33)
'0x21'
>>> oct(7)
'0o7'
>>> bin(7)
'0b111'
>>> 1.str()
  File "<stdin>", line 1
    1.str()
      ^
SyntaxError: invalid syntax
>>> str(1)
'1'
>>> int("abc")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc'
>>> int(abc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'abc' is not defined
>>> int(1)
1
>>> float(1)
1.0
>>> double(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'double' is not defined
>>> str(False)
'False'
>>> bool(1)
True
>>> bool(False)
False
>>> bool(false)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'false' is not defined
>>> int(sbc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sbc' is not defined
>>> 1 is None
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False
>>> 1 is None
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False
>>> me+c
'how are you, i an fine thankyou ppq ppq ppq'
>>> d=me+c
>>>
>>> d
'how are you, i an fine thankyou ppq ppq ppq'
>>> ord("A")
65
>>> chr("A")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)
>>> chr(65)
'A'
>>> chr(64)
'@'
>>> d.in
  File "<stdin>", line 1
    d.in
      ^
SyntaxError: invalid syntax
>>> d
'how are you, i an fine thankyou ppq ppq ppq'
>>> "h" in d
True
>>> p in d
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'p' is not defined
>>> "w a" in d
True
>>> "wa"in d
False
>>> ord("猪")
29482
>>> chr(29482)
'猪'
>>> ord(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 43 found
>>> ord("你好")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found
>>> len("你好“)
  File "<stdin>", line 1
    len("你好“)
             ^
SyntaxError: EOL while scanning string literal
>>> len("nihao")
5
>>> len("你好")
2
>>> d[1]
'o'
>>> s[-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 's' is not defined
>>> d[-1]
'q'
>>> d[-1:-5]
''
>>> print(d[-1;-5])
  File "<stdin>", line 1
    print(d[-1;-5])
              ^
SyntaxError: invalid syntax
>>> d[-1:-5:]
''
>>> d[-1:]
'q'
>>> d[-2]
'p'
>>> d[::-1]
'qpp qpp qpp uoyknaht enif na i ,uoy era woh'
>>> d[-1:-5:-1]
'qpp '
>>> d.split(" ")
['how', 'are', 'you,', 'i', 'an', 'fine', 'thankyou', 'ppq', 'ppq', 'ppq']
>>> d.center
<built-in method center of str object at 0x000001A0A08D4BD0>
>>> d.center()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: center expected at least 1 argument, got 0
>>> center(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'center' is not defined
>>> d.center(30)
'how are you, i an fine thankyou ppq ppq ppq'
>>> "ppp".center(10)
'   ppp    '
>>> "ppp".center(100)
'                                                ppp                                                 '
>>> d.replace("how","mememe")
'mememe are you, i an fine thankyou ppq ppq ppq'
>>>
>>>> d.isdigit(
...
...
... )
False
>>> d.isdigit()
False
>>> d.isalpha()
False
>>> d.isalpha(h)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: isalpha() takes no arguments (1 given)
>>> f.isalpha("h")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'f' is not defined
>>> "123 ".isdigit()
False

数据收纳盒:
面临得需要处理的问题:如何使用? 名字[n]
列表,
元组(不可变序列)
在这里插入图片描述
在这里插入图片描述
字符串用“”
列表用[]
元组用()
列表的操作
容器,何为容器?是从简单数据类型引申出来的容器将简单类型数据组织起来
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

函数reversed不返回列表,而是返回一个迭代器。可使用list将返回的对象转换为列表。
在这里插入图片描述

>>> box["1",1,"a"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'box' is not defined
>>> box[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'box' is not defined
>>> box=["ss","1",1]
>>> box
['ss', '1', 1]
>>> len.box
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'box'
>>> box.len
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'len'
>>> me.len
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'len'
>>> len(box)
3
>>> type(box)
<class 'list'>
>>> a1= list(m
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值