Noocyada Xogta Python iyo Nooca Beddelidda

Hordhac ku saabsan noocyada xogta Python iyo sida loo sameeyo isbeddelada nooca ah.

Noocyada Xogta Python

Markii aan abuureyno ama ku dhawaaqeyno doorsoomayaasha Python, doorsoomayaasha waxay hanan karaan noocyo xog kala duwan ah.

Python wuxuu leeyahay noocyada xogta ee la dhisay:

  • bog
  • int, sabayn, kakooban
  • liiska, tuple
  • dig
  • dhigay
  • bool
  • byte, bytearray

Nooca qoraalka: str

The str nooca xogta ayaa la adeegsadaa markaan rabno inaan ku dhawaaqno a xarig doorsoomaha.

Tusaale:

x = 'some string' y = str('another string')

Nooca Tirooyinka: int, sabayn, kakanaanta

Markaan dooneyno inaan abuurno doorsoomayaal tirooyin ah waxaan isticmaalnaa int, float ama complex.



Tusaale:

//int a = 5 b = int(5) //float c = 5.5 d = float(5.5) //complex e = 1j f = complex(1j)

Nooca isku xigxiga: liiska, tuple

Si loo abuuro doorsoomayaal nooc taxane ah waxaan adeegsanaa list ama tuple.

  • A list waa aruurin la dalbaday lana beddeli karo. Kuu ogolaanayaa xubnaha nuqul.
  • A tuple waa aruurin la dalbaday oo aan la beddeli karin. Kuu ogolaanayaa xubnaha nuqul.

Tusaale:

//list colors = ['red', 'green', 'blue'] colors_list = list(('red', 'green', 'blue')) //tuple fruits = ('apple', 'orange', 'banana') fruits_tuple = list(('apple', 'orange', 'banana'))

Nooca Khariidaynta: dict

Si loo abuuro khariidad ama qaamuus waxaan isticmaalnaa dict.

KU Qaamuus waa aruurinta aan la xakamayn karin, la beddeli karo oo la tilmaami karo. Xogtu waa lammaanaha qiimaha muhiimka ah.

Tusaale:

people = {'name': 'John', 'age': '45'} people_dict = dict(name='John', age=45)

Nooca Deji: dhigay

A set waa aruurinta aan la xakamayn oo aan la tirin.

Si loo abuuro xirmo, waxaan isticmaalnaa set.

Tusaale:

status_codes = {'200', '300', '400', '500'} status_codes = set(('200', '300', '400', '500'))

Nooca Boolean: bool

The bool keyword waxaa loo isticmaalaa in lagu abuuro doorsoomayaal leh nooca xogta boolee.

is_valid = False valid = bool(is_valid)

Nooca Binary: byte, bytearray

Noocyada macluumaadka binary waxaa loo abuuri karaa sidan soo socota:

//bytes a = b'some_text' b = bytes(5) //bytearray c = bytearray(3)

Sidee loo helaa Nooca Isbedelka

Si aan u helno nooca doorsoomaha waxaan ku duubnaa doorsoomaha gudihiisa gudaha type() shaqo.

Tusaale ahaan:

colors = ['red', 'green', 'blue'] colors_list = list(('red', 'green', 'blue')) print(type(colors_list)) print(colors_list) fruits = ('apple', 'orange', 'banana') fruits_tuple = tuple(('apple', 'orange', 'banana')) print(type(fruits_tuple)) print(fruits_tuple)

Wax soo saarka:

['red', 'green', 'blue'] ('apple', 'orange', 'banana')

Beddelaadda Nooca Macluumaadka Python

Python wuxuu qeexayaa nashaadaadka nooca beddelaadda si toos ah loogu beddelo nooc xog ah mid kale, taas oo aad u faa'iido badan.

Hoos waxaa ku qoran tusaalooyin:

Ka beddelo int si aad u sabbayso

x = 5 y = float(x) print(y)

Wax soo saarka:

5.0

U beddel Ka sabeynta illaa int

x = 5.0 y = int(x) print(y)

Wax soo saarka:

5

Ka beddelo xarig ilaa liistada

s = 'devqa' t = list(s) print(t)

Wax soo saarka:

['d', 'e', 'v', 'q', 'a']

Ka beddel xarig ilaa tuple

s = 'devqa' t = tuple(s) print(t)

Wax soo saarka:

('d', 'e', 'v', 'q', 'a')

Ka beddelo xarig si aad u dejiso

s = 'devqa' t = set(s) print(t)

Wax soo saarka:

{'d', 'e', 'a', 'v', 'q'}