【Day 03】- Python 基础操作与常见资料型态(整数、浮点数、布林值、字串、串列、元组、字典)

前情提要

在前一篇文章内容中带大家建立了 pipenv 虚拟环境,并在虚拟环境中装了套件。又安装了 Vscode ,并透过设定 venv 路径让 Vscode 能够在左下角选择开发环境。

开始之前

Python 是一个十分易读且易用的语言,操作相对来说人性化,今天要带大家操作过一遍 Python 的基础语法,包含了基础操作跟常见资料型态,如果有这些基础的可以先跳过等之後内容欧。

▲ 在 Vscode 中建立 python 档案 (副档名为 .py)

Hello World!

读者可以在 Vscode 中打入以下程序,之後按下右上角的 Run Python File in Terminal(形似播放键)

print('Hello World')

▲在刚才创建好的档案内打入 hello world 程序码,并按下执行使程序印出 hello world

基础操作

变数与赋值(assign)

python 是个动态语言,意思是在执行期才确定变数型别,也就是说在使用变数之前不须宣告变数(指定资料型别)。

变数是在记忆体中的一块位置,有自己的变数名称和值,方便我们储存资料。 = 称为赋值运算子,会将等号右方的值赋予(assign)给左方的变数。以下为一些赋值的例子。

a = 77495358     #将 77495358 这个值赋予给 a 这个变数
b = 3.14159      #将 3.14159 这个值赋予给 b 这个变数
c = 'Hello Here' #将 'Hello Here' 这个值赋予给 c 这个变数
# 以下为一些方便语法
a, b, c = 77495358, 3.14159, 'Hello Here' # 分别将 77495358, 3.14159, 'Hello Here'赋予给 a, b, c
x = y = z = 77495358 # 将 77495358 这个值赋予给 x y z 三个变数

变数型态

这边列出常见 7 个 Python 资料型态,使用 type() 函式会回传该变数的型态为何。

  1. 整数 int、浮点数 float
  2. 布林值 bool
  3. 字串 str
  4. 串列 list
  5. 元组 tuple
  6. 字典 dict
  • 整数 int、浮点数 float

浮点数就是数学中的小数。

'''
整数、浮点数范例
'''

a = 5 # 将 5 这个整数赋予给 a 这个变数
b = int('2016') # 将 '2016' 这个字串转换为整数 2016 之後赋予给 a 这个变数

#  整数的 + - * / 运算
c = a + b
print(c) # Output: 2021
print(type(c)) # Output: <class 'int'>

c = a - b
print(c) # Output: -2011
print(type(c)) # Output: <class 'int'>

c = a * b
print(c) # Output: 10080
print(type(c)) # Output: <class 'int'>

# 除法有分为两种,真除法、无条件舍去除法
# 真除法与会将数字的小数点保留
c = a / b
print(c) # Output: 0.00248015873015873
print(type(c)) # Output: <class 'float'>

# 无条件舍去除法会将运算後的小数点去除
c = a // b
print(c) # Output: 0
print(type(c)) # Output: <class 'int'>
  • 布林值 bool

用於逻辑判断的值,值有两种 True False

'''
布林值范例
'''

a = 87 < 5478
print(a) # Output: True
print(type(a)) # Output: <class 'bool'>

b = 878 >= 0
print(b) # Output: False
print(type(b)) # Output: <class 'bool'>
  • 字串 str

字串的表示方法为文字前後包上 '" ,若要在字串中放入引号,需在要放入的那个引号前方加上反斜线 \也就是斜率为负的那个斜线将欲加入的引号跳脱

注意字串在程序中仅为文字不具意义 e.g."我要 10000000万" 你的程序不会理你(执行动作),你的银行也不会变多钱。

後天会介绍字串的更多使用技巧。

'''
字串范例
'''

a = 'Hello '
b = ',World'
c = a + b
print(c) # Output: Hello, World
print(type(c)) # Output: <class 'str'>

c = a*5 + b
print(c) # Output: Hello Hello Hello Hello Hello ,World
print(type(c)) # Output: <class 'str'>
  • 串列 list

类似於其他语言中的 array , 但它的功能更多。串列中能存在多个元素(变数),其中第一个元素的索引值(index)为 0,第二个为 1 ... 以此类推。

  • 元素可为不同资料型态
  • 支援多种操作
  • 在许多场合十分方便、易用
  • 存取速度较慢

串列以中括号包住一系列以逗号相隔的元素表示。

▲说明了串列中第一个元素的索引值(index)为 0 、第二个为 1...

'''
串列范例
'''

a = ['1', '2', '3', '4']
print(type(a)) # Output: <class 'list'>
print(a[0]) # OutPut: '1'
print(a[2]) # Output: '3'

# 范围取值
# myList[start(开始的index):end(结束的index):sep(间隔)]
print(a[1:3]) #Output: ['2', '3']
print(a[0:3:2]) #Output: ['1', '3']
print(a[::-1]) #Output: ['4', '3', '2', '1']

#串列操作
a.append('6') #在串列最後方放入元素
print(a) #Output: ['1', '2', '3', '4', '6']

a.insert(4, '5') #在串列中 index 为 4 的地方放入 '5' 这个元素
print(a) #Output: ['1', '2', '3', '4', '5', '6']

a.remove('3') #在串列中移除 '3' 这个元素,若有多个 '3' 则移除 index 较小的
print(a) #Output: ['1', '2', '4', '5', '6']

b = a.pop() #移除在串列中的最後一个元素,并回传该元素
print(a) #Output: ['1', '2', '4', '5']
print(b) #Output: '6'

#串列长度
print(len(a)) #Output: 4

#元素可为不同型态
a = [77495358, 3.14159, 'Hello Here', [1, 2, 3]]  #串列中能存在不同资料型态的元素,甚至是另一个串列
print(type(a[0])) #Output: <class 'int'>
print(type(a[1])) #Output: <class 'float'>
print(type(a[2])) #Output: <class 'str'>
print(type(a[3])) #Output: <class 'list'>
  • 元组 tuple

元组是固定板的串列,是个不可变物件(immutable),当被定义後状态就不可以被改变,特色是

  • 占用空间较小
  • 存取速度较快
  • 可被字典当成 key (hashable)
  • 常出现於函式回传值中(Python 中实现多值回传方式)

元组以小括号包住一系列以逗号相隔的元素表示。

'''
元组范例
'''

a = 1, 2, 3, 4 #封装(packing)
a1, a2, a3, a4 = a #拆封(unpacking)
print(a1) #Output: 1
print(a2) #Output: 2
print(a3) #Output: 3
print(a4) #Output: 4
  • 字典 dict

跟一般的字典一样,是个一对一的结构(key:value),一个 key 会对上一个 value。

  • list 是根据 index 取得值, dict 则是以 key 来取得值
  • key 必须为 hashable 的(list、dict 就不是 hashable ,所以 list、dict 不能当作 dict 的 key)
  • value 没有限制,甚至能是另一个 dict
  • python 预设的 dict 底层是用 hash table 实作的

元组以小括号包住一系列以逗号相隔的元素表示。

'''
字典范例
'''
dic = {
    '1': 500,
    '2': 600,
    90: 'OwO'
}

print(dic['1']) #Output: 500
print(type(dic['1'])) #Output: <class 'int'>
print(dic[90]) #Output: 'OwO'
print(type(dic[90])) #Output: <class 'str'>

dic['new'] = 'hello' #也能给值
print(dic) #Output: {'1': 500, '2': 600, 90: 'OwO', 'new': 'hello'}
print(type(dic)) #Output: <class 'dict'>

dic['new'] = 'world' #修改值
print(dic) #Output: {'1': 500, '2': 600, 90: 'OwO', 'new': 'world'}
print(type(dic)) #Output: <class 'dict'>

del dic['new'] #删除对应 key-value
print(dic) #Output: {'1': 500, '2': 600, 90: 'OwO'}
print(type(dic)) #Output: <class 'dict'>

输入输出

变数在电脑内如果没有告诉他印在萤幕上,它就不会主动告诉你变数内的值。上面告诉大家如何在程序内定义变数的值,接下来要跟大家介绍 python 程序输入及输出的语法,大家都能在自己的 Vscode 中试试。

# 输入
userInput1 = input() #读到这行时,程序会停在这边等你输入一个值,并将输入的值以字串的型态赋予给 userInput1
userInput2 = input('your name: ') #与上一个类似,不同在於它会先输出 your name: 之後再等待使用者输入

userOutput1 = 'OwO'
userOutput2 = '87'

#输出
print(userInput1) #将 userInput1 的值印在萤幕上,预设会在输出後自动换行
'''
OwO
'''
print(userInput1, userInput2) #可用逗号隔开想一次输出的值,预设每个变数以空白相隔,
'''
OwO 87
'''
print(userInput1, end = ' ') #传入的参数加个 end 可以更改 print 结束後需输出的文字,预设是换行,范例中是以空白相隔
print(userInput2)
'''
OwO 87
'''

print(userInput1) #不加 end 参数则是以换行隔开每个 print
print(userInput2)
'''
OwO
87
'''

结语

今天跟读者介绍在 Python 中基础的操作与资料类型,对於这些资料型态尤其是 list 与 dict 都会在接下来的几天经常用到,读者们能再熟悉这些基本用法,并在自己的 Vscode 上面试试。

明日内容

明天会带给大家 Python 的条件判断及回圈的用法,没有接触过或有一些基础的读者都能关注欧~

补充资料

Python 基本语法 : https://ithelp.ithome.com.tw/articles/10200505

Python 基础教程 : https://www.runoob.com/python/python-tutorial.html

Python3.9 document : https://docs.python.org/3/


<<:  [Day02] 第二章- 初探金流API文件-1

>>:  Day05 - Gem-paranoia 软删除介绍与应用

[Day22] Esp32用STA mode + AHT10 - (程序码讲解)

1.前言 这边主要是为解说前几篇关於AHT10的程序码,此次主要讲的部分是loop中的程序码,因为透...

[访谈] APCS x 武陵科学班 Jayinnn

今天邀请到旺宏科学奖的旺宏奖得主及以学测 APCS 组入学交大的 Jayinnn 来分享~ 访谈连结...

JavaScript 之旅 (26):String.prototype.replaceAll()

本篇介绍 ES2021 (ES12) 提供的 String.prototype.replaceAl...

MySQL学习_Day1

学习内容 关联式资料库、创建资料库、建立表格 在进入MySQL学习之前,Icebear特别去了解一下...

[Python 爬虫这样学,一定是大拇指拉!] DAY12 - HTTP / HTTPS (3)

了解 HTTP Message 的结构後,接下来要讲解的是 HTTP Method,这对爬虫来算是重...