日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python基础入门(2)

發布時間:2025/3/15 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python基础入门(2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

一、Python數據類型

1)置數據類型

2)獲取數據類型

3)設置數據類型

4)設置特定數據類型

6)練習題

二、python數字-

1)整數

2)浮點數

3)虛數

4)類型轉換

5)隨機數

6)練習題

?三、python指定變量類型


一、Python數據類型

1)置數據類型

默認情況下,Python 具有以下內置數據類型,在這些類別中:

文本類型: str 數字類型: int, float, complex 序列類型: list, tuple, range 映射類型: dict 套裝類型: set, frozenset 布爾類型: bool 二進制類型: bytes, bytearray, memoryview

此時你大可不必知道這些類型到底是什么,在后續的深入中,你一定會用得到,所以是需要記住的。



2)獲取數據類型

您可使用以下type()函數獲取任何對象的數據類型。
例如,打印變量 x 的數據類型:

x = 6 print(type(x))

我們可以看到返回為:int類型

3)設置數據類型

在 Python 中,數據類型是在為變量賦值時設置的。
例如以下例子。
str字符串:

x = "Hello World" print(x) print(type(x))

int整形:

x1 = 6 print(type(x1))

float浮點類型:

x2 = 6.5 print(type(x2))

complex復數類型:

x3 = 2j print(x3) print(type(x3))

list列表類型:

x4 = ["apple", "banana", "cherry"] print(x4) print(type(x4))

tuple元祖類型:

x5 = ("apple", "banana", "cherry") print(x5) print(type(x5))

后面還有其它一些類型,我就不以完整代碼形式演示了,直接以例子形式讓大家看看什么樣子是什么類型,當然如果你能親自動手像我上面的例子一樣進行操作打印看一看就再好不過了。
range范圍類型

x = range(6

dict字典類型

x = {"name" : "John", "age" : 36}

set集合類型:

x = {"apple", "banana", "cherry"}

不常用的凍結集類型:

x = frozenset({"apple", "banana", "cherry"})

bool布爾類型:

x = True

不常用byte字節類型:

x = b"Hello"

不常用bytearray字節數組類型:

x = bytearray(5)

更有冷門到爆的memoryview內存試圖類型

x = memoryview(bytes(5))

4)設置特定數據類型

我會舉一些例子說明,盡量很全,大可不必擔心。先舉一個完整例子,后面的是一樣的打印就不演示了。
強調特定x為字符串:

x = str("Hello World") print(x) print(type(x))

返回為:

強調x為整形:

x = int(20)

強調x為浮點:

x = float(20.5)

強調x為復數:

x = complex(1j)

強調為列表

x = list(("apple", "banana", "cherry"))

強調為元祖

x = tuple(("apple", "banana", "cherry"))

強調為范圍

x = range(6)

強調為字典

x = dict(name="John", age=36)

強調為集合

x = set(("apple", "banana", "cherry"))

強調凍結集(沒啥用的類型)

x = frozenset(("apple", "banana", "cherry"))

強調布爾類型

x = bool(5)

強調字節類型

x = bytes(5)

強調字節組類型

x = bytearray(5)

強調內存試圖類型(又是沒啥用的類型)

x = memoryview(bytes(5))

6)練習題

回答下面的問題結果為什么類型?
1-

x = 5 print(type(x))

2-

x = "Hello World" print(type(x))

3-

x = 20.5 print(type(x))

4-

x = ["apple", "banana", "cherry"] print(type(x))

5-

x = ("apple", "banana", "cherry") print(type(x))

6-

x = {"name" : "John", "age" : 36} print(type(x))

7-

x = True print(type(x))



二、python數字-

Python 共有三種數字類型:

  • int
  • float
  • complex

三種類型分別對應如下例子:?

x = 1 # int y = 2.8 # float z = 1j # complex

要驗證 Python 中任何對象的類型,請使用以下type()函數:

print(type(x)) print(type(y)) print(type(z))

因此你可以運行如下代碼:

x = 1 # int y = 2.8 # float z = 1j # complex print(type(x)) print(type(y)) print(type(z))



1)整數

Int 或 integer,是一個整數,正負,不帶小數,長度不限。
例如:

x = 1 y = 3562254887 z = -35522 print(type(x)) print(type(y)) print(type(z))



2)浮點數

浮點數或“浮點數”是包含一位或多位小數的正數或負數。
例如:

x = 1.10 y = 1.0 z = -35.59 print(type(x)) print(type(y)) print(type(z))

浮點數也可以是帶有“e”的科學數字,表示 10 的冪。
例如:

x = 35e3 y = 12E4 z = -87.7e100print(type(x)) print(type(y)) print(type(z))

3)虛數

復數寫有“j”作為虛部。

x = 3+5j y = 5j z = -5jprint(type(x)) print(type(y)) print(type(z))



4)類型轉換

比如你可以從一種類型轉變成另一種同int(), float()和complex()方法。
例如:(你可以親自運行一下)

x = 1 # int y = 2.8 # float z = 1j # complexa = float(x)b = int(y)c = complex(x)print(a) print(b) print(c)print(type(a)) print(type(b)) print(type(c))



5)隨機數

Python 有一個內置模塊 random可以用來生成隨機數。
示例:導入 random 模塊,并顯示 1 到 10之間的一個隨機數:

import random print(random.randrange(1, 11))



6)練習題

1-插入正確的語法將 x 轉換為浮點數。

x = 5 x = _(x)

2-插入正確的語法以將 x 轉換為整數。

x = 5.5 x = _(x)

3-插入正確的語法以將 x 轉換為復數。

x = 5 x = _(x)

?三、python指定變量類型

python 中的轉換是使用構造函數完成的:

  • int() - 從整數文字、浮點文字(通過刪除所有小數)或字符串文字(提供字符串表示整數)構造整數
  • float() - 從整數文字、浮點文字或字符串文字構造浮點數(提供字符串表示浮點數或整數)
  • str() - 從多種數據類型構造一個字符串,包括字符串、整數文字和浮點文字

我將每一個類型都舉例子說明。
整數

x = int(1) # x will be 1 y = int(2.8) # y will be 2 z = int("3") # z will be 3

浮點

x2 = float(1) # x will be 1.0 y2 = float(2.8) # y will be 2.8 z2 = float("3") # z will be 3.0 w2 = float("4.2") # w will be 4.2

字符串

x1 = str("s1") # x will be 's1' y1 = str(2) # y will be '2' z1 = str(3.0) # z will be '3.0'

總結

以上是生活随笔為你收集整理的python基础入门(2)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。