深度学习入门(基于python的理论与实现)-01python入门
·
本节主要介绍python数据类型、变量、列表、字典、bool、if语句、for语句、函数等python 基础知识的了解,还未涉及深度学习
编程语言:python 版本:python 3.x
库:NumPy<用于数值计算>、Matplotib<用来画图>
安装:确认python版本
python --version
启动python解释器
C:\Users\Lenovo>python
Python 3.13.1 (tags/v3.13.1:0671451, Dec 3 2024, 19:06:28) [MSC v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
了解数据类型
type()用于获取数据类型
>>> type(10)
<class 'int'>
>>> type(2.12)
<class 'float'>
>>> type("hello")
<class 'str'>
>>> type('dd')
<class 'str'>
>>> type('d')
了解变量
>>> x=10
>>> print(x)
10
>>> x=100
>>> print(x)
100
>>> y=2.5
>>> x*y
250.0
>>> type(x*y)
<class 'float'>
>>>
了解列表和字典
>>> a=[1,2,3,4,5,100] # 创建列表list
>>> print(a)
[1, 2, 3, 4, 5, 100]
>>> len(a) #列表长度
6
>>> a[0]#访问第一个元素
1
>>> a[5]
100
>>> a[5]=6 #赋值
>>> print(a)
[1, 2, 3, 4, 5, 6]
#切片
>>> a[0:2]
[1, 2]
>>> a[2:]
[3, 4, 5, 6]
>>> a[:3]
[1, 2, 3]
>>> a[:-1] #第一个元素到最后一个元素的前一个元素
[1, 2, 3, 4, 5] #第一个元素到最后一个元素的前二个元素
>>> a[:-2]
[1, 2, 3, 4]
>>> type(a)
<class 'list'>
了解字典dict
列表根据索引,按照0,1,2, ...的顺序存储值,而字典则以键值对的形 式存储数据。
>>> me={'height':180} #创建字典
>>> me['weight']=70 #添加元素
>>> me['sex']='man' #添加元素
>>> me[haircolor]='black' #添加元素
Traceback (most recent call last): #注意报错
File "<python-input-42>", line 1, in <module>
me[haircolor]='black'
^^^^^^^^^
NameError: name 'haircolor' is not defined
>>> me['haircolor']='black'
>>> print(me)
{'height': 180, 'weight': 70, 'sex': 'man', 'haircolor': 'black'}
>>> me
{'height': 180, 'weight': 70, 'sex': 'man', 'haircolor': 'black'}
>>> type(me)
<class 'dict'>
#创建字典第二种方法
>>> I={'weight':50,'height':188,'sex':'woman'}
>>> I
{'weight': 50, 'height': 188, 'sex': 'woman'}
>>>
了解布尔型bool
>>> hungry=True
>>> sleepy=Flase
>>> sleepy=False
>>> type(hungry)
<class 'bool'>
>>> not hungry
False
>>> hungry
True
>>> hungry and sleepy
False
>>> hungry and not sleepy
True
>>> hungry or sleepy
True
>>>
if语句
>>> hungry=True
>>> if hungry:
... print("I am hungry")
...
I am hungry #输出的结果
>>> hungry=False
>>> if hungry:
... print("I am hungry")
... else:
... print("I am not hungry")
...
I am not hungry #执行if语句输出的结果
>>>
for语句
>>> for i in [1,2,3]:
... print(i)
...
1
2
3
>>>
函数
>>> def hello():
... print("hello world!")
...
>>> hello()
hello world!
>>> def hello(object):
... print("hello"+object+"!") #字符串的拼接可以使用+。
...
>>> hello()
Traceback (most recent call last):
File "<python-input-67>", line 1, in <module>
hello()
~~~~~^^
TypeError: hello() missing 1 required positional argument: 'object'
>>> hello("cat")
hellocat!
>>> hello(222222)
Traceback (most recent call last):
File "<python-input-69>", line 1, in <module>
hello(222222)
~~~~~^^^^^^^^
File "<python-input-66>", line 2, in hello
print("hello"+object+"!")
~~~~~~~^~~~~~~
TypeError: can only concatenate str (not "int") to str
>>>
关闭python解释器时,windos输入ctrl+z,后enter
linux or mac 输入ctrl+d
>>> ^Z
更多推荐



所有评论(0)