Python tietotyyppi
- Edellinen Sivu Python muuttujat
- Seuraava Sivu Python Luku
Sisäänrakennetut tietotyypit
Ohjelmoinnissa tietotyyppi on tärkeä käsite.
Muuttujat voivat tallentaa erilaisia tietoja, ja eri tyypit voivat suorittaa erilaisia operaatioita.
Näissä luokissa Pythonilla on oletusarvoisesti seuraavat sisäänrakennetut tietotyypit:
Tekstityyppi: | str |
Numerotyyppi: | int , float , complex |
Jono tyyppi: | list , tuple , range |
Merkistötyyppi: | dict |
Koko tyyppi: | set , frozenset |
布尔inen tyyppi: | bool |
Binaarinen tyyppi: | bytes , bytearray , memoryview |
Hanki tietotyyppi
Voit käyttää type() -funktiota saadaksesi minkä tahansa objektin tietotyyppin:
Esimerkki
Tulosta muuttujan x tietotyyppi:
x = 10 print(type(x))
Tietotyyppien asettaminen
Pythonissa, kun asetat muuttujalle arvon, määrität tietotyyppin:
Esimerkki | Tietotyyppi | Kokeile |
---|---|---|
x = "Hello World" | str | Kokeile |
x = 29 | int | Kokeile |
x = 29.5 | float | Kokeile |
x = 1j | complex | Kokeile |
x = ["apple", "banana", "cherry"] | list | Kokeile |
x = ("apple", "banana", "cherry") | tuple | Kokeile |
x = range(6) | range | Kokeile |
x = {"name" : "Bill", "age" : 63} | dict | Kokeile |
x = {"apple", "banana", "cherry"} | set | Kokeile |
x = jäädytetty_set({"apple", "banana", "cherry"}) | frozenset | Kokeile |
x = True | bool | Kokeile |
x = b"Hello" | bytes | Kokeile |
x = bytearray(5) | bytearray | Kokeile |
x = memoryview(bytes(5)) | memoryview | Kokeile |
Määritä tietty tietotyyppi
Jos haluat määrittää tietotyyppin, voit käyttää seuraavia rakentajia:
Esimerkki | Tietotyyppi | Kokeile |
---|---|---|
x = str("Hello World") | str | Kokeile |
x = int(29) | int | Kokeile |
x = float(29.5) | float | Kokeile |
x = complex(1j) | complex | Kokeile |
x = list(("apple", "banana", "cherry")) | list | Kokeile |
x = tuple(("apple", "banana", "cherry")) | tuple | Kokeile |
x = range(6) | range | Kokeile |
x = dict(name="Bill", age=36) | dict | Kokeile |
x = set(("apple", "banana", "cherry")) | set | Kokeile |
x = frozenset(("apple", "banana", "cherry")) | frozenset | Kokeile |
x = bool(5) | bool | Kokeile |
x = bytes(5) | bytes | Kokeile |
x = bytearray(5) | bytearray | Kokeile |
x = memoryview(bytes(5)) | memoryview | Kokeile |
- Edellinen Sivu Python muuttujat
- Seuraava Sivu Python Luku