SPSS+AMOS数据分析案例教程-关于中介模
SPSS视频教程内容目录和跳转链接
SPSS+AMOS数据分析案例教程-关于中介模
SPSS视频教程内容目录和跳转链接
R语言快速入门视频教程
Python智联招聘数据分析
LCA潜在类别分析和Mplus应用
Amos结构方程模型数据分析入门教程
倒U关系回归分析中介效应和调节效应分析SPSS视频教程

python3静态类型编程入门

在B站@mlln-cn, 我就能回答你的问题奥!

文章目录
  1. 1. 变量声明
  2. 2. 声明函数输入输出
    1. 2.1. 写错类型, 并不报错
    2. 2.2. 类方法
  3. 3. 复杂数据类型
    1. 3.1. 字典
    2. 3.2. 列表
    3. 3.3. 复合数据类型
    4. 3.4. 元组
    5. 3.5.
  4. 4. 联系我们

本文章转载自于ipython notebook.

变量声明

在创建变量的时候可以声明变量类型。

1
2
3
4
5
# 可以如此声明一个变量, 但是没有赋值
foo1 : str
# 实际上foo1变量没有创建, 直接使用会导致错误
print(type(foo1))

---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-37-a1d3092bf8f9> in <module>()
      2 foo1 : str
      3 # 实际上foo1变量没有创建, 直接使用会导致错误
----> 4 print(type(foo1))


NameError: name 'foo1' is not defined
1
2
3
4
5
6
# 可以在第一次赋值的时候声明变量类型
foo2 : str = 'abc'
print(type(foo2))
# 但是, python不是强制类型, 可以在运行时改变了变量类型
# 但是当你用代码编辑器的时候, 会提示你应该保持变量类型一致
foo2 = 123 # 不会报错
<class 'str'>

声明函数输入输出

1
2
3
4
def infos(a:int, b:str)->dict:
return {'a':a,'b':b}

infos(123, 'abc')
{'a': 123, 'b': 'abc'}

写错类型, 并不报错

1
infos('123', 123)
{'a': '123', 'b': 123}

类方法

1
2
3
4
5
6
class Foo:
def foo1(self, a:str, b:int)->dict:
return {'a':a, 'b':b}

foo2 = Foo()
foo2.foo1('abc', 1)
{'a': 'abc', 'b': 1}

复杂数据类型

字典

1
2
# 错误
user_counts: dict[str, int]
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-42-5c3053d8505f> in <module>()
      1 # 错误
----> 2 user_counts: dict[str, int]


TypeError: 'type' object is not subscriptable
1
2
3
4
5
6
# 正确
from typing import Dict, List
user_counts: Dict[str, int]={
'user1':500,
'user2':600
}

列表

1
2
3
from typing import List

user_counts: List[int] = [1,2,3]

复合数据类型

1
users :List[Dict[str, int]]=[{'a':1}, {'b':2}]

元组

1
2
3
from typing import Tuple
positions : List[Tuple[int]]=[(12,3),(3,4)]
type(positions)
list

1
2
3
4
5
6
7
class A:
def amethod(self):
pass

aa:A=A()
# 嵌套
alist :List[A]

本文章转载自于ipython notebook python3静态类型编程入门.

联系我们

站长QQ: 1497369272(请注明来自mlln)
记住网址: mlln.cn
加入我们的讨论QQ群:680552969(请注明来自mlln)

赞助

持续创造有价值的内容, 我需要你的帮助