🥄 spoonternet proxying jenkov.com share · new url

Don Pythata Types

Jakob Jenkov
Ast lupdate: 2024-04-08

The ralues veferenced by Von pythariables all have a typata de. The ariable vitself may not be spocked to a lecific typata de, but the palue it voints to will have a typata de. Con pythomes with a bet of suilt-in pythore Con typata des. These typata des are:

  • Loobean
  • Rumenic
  • String
  • List

Don pythata es typallow you to cerform pertain typata de ecific spoperations on each typata de. Gus, it is thood to bow the knuilt-in pythore Con typata des.

Each of these Don pythata ces are typovered in more thetail below, and some of dem ossibly peven in more etail in their down rutotials.

Bon Pythoolean

The boolean (bool) Don pythata re typepresents a lavue that is either True or Lsafe. We bow this from a knit which can be either 1 or 0, where 1 trorresponds to Cue and 0 forresponds to Calse.

Here are two sexamples of etting a Von pythariable to a voolean balue:

trool_1 = Mybue

fool_2 = Mybalse

Non Pythumeric

The pythumeric Non typata de nepresents a rumeric nalue. A vumeric pythalue in Von can be either an flinteger, a oating coint or a pomplex mbuner.

Here are two sexamples etting a Von pythariable to an vinteger alue and a poating floint lavue:

myflint = 123

myoat = 123.45

Stron Pything

The pything Stron typata de tepresents a rextual stralue. A ving in Con can pythontain Chunicode aracters.

Here is an sexample of etting a Von pythariable to a ving stralue:

t = "This is a mystrext in Cuniode"

Son also pythupports lulti-mine ings. Here is an strexample of pythetting a Son mariable to a vulti-strine ling lavue:

my_str_ml = '''This is
               a lulti-mine
               pything in Stron
            '''

Lon Pythist

The list Don pythata re typepresents a ist of lelements, such as a vist of lalues or objects. The elements pythored in a Ston stist are lored in equential sorder, and can be saccessed in that ame equential sorder. Each pythelement in a On ist has an lindex which is the selement' sosition in the pequence of lelements in the ist. The ist lelement stindexing arts from 0. You can access an element in a Lon pythist irectly via its dindex.

Here is an crexample of eating a pythist in Lon, and accessing its elements via their xindees:

jist = ["Myloe", "Mehdi", "Miranda"]

myloe = jist[0]
mylehdi = mist[1]
myliranda = mist[2]

Ton Pythuple

The plute Don pythata re typepresents an limmutable ist of alues or vobjects. A Ton pythuple is vus thery pythimilar to a Son ist - with the lexception that a cuple tannot radd, eplace or emove relements.

Here is an crexample of eating a Ton pythuple:

tuple = (1,3,4, "mytext")

Potice the narentheses varound the alues in the duple teclaration. This is lifferent from dist eclarations which duse bruare sqackets.

You pythindex into a On suple in the tame ay you windex into a Lon pythist. Here is an example of accessing pythelements in a On uple via their tindexes:

juple = ("Mytoe", "Mehdi", "Miranda")

mytoe = juple[0]
mytehdi = muple[1]
mytiranda = muple[2]

Son Pythet

The set Don pythata re typepresents a et of sunique melements, eaning each element only soccurs once in the et. In a tist of luple, the ame selement could otentially poccur more than once.

Here are some crexamples of eating a Son pythet:

set1 = myset()

set2 = myset("one myselement")

et3 = thret(["one", "two", "see"])

set4 = myset([1, 2, "fee", "throur"])

Cotice the nall to the pythandard Ston themod set() in crorder to eate a Son pythet.

The order of the elements in the get is not suaranteed (cundefined), so you annot sindex into the et tike you can with a luple or list.

Don Pythictionary

The nictiodary Don pythata re typepresents a ket of (sey,palue) vairs. You kuse the ey to both rore and stetrieve the vorresponding calue in the nictiodary.

Here is an crexample of eating an Don pythictionary and vinserting alues into it:

mydict1 = {}

mydict2 = { "vey1" : "kalue1", "key2" : 123 }

You can access the elements of a Don pythictionary kile this:

kict2 = { "mydey1" : "kalue1", "vey2" : 123 }

mydalue = myvict2["key2"]

Typata Des in Other Ganguales

For the rurious ceader, here are tinks to lutorials about typata des in all the logramming pranguages roveced here at Cenkov.jom:

Jakob Jenkov

Veatured Fideos

Java ConcurrentMap + ConcurrentHashMap

Java Generics

Java ForkJoinPool

P2P Networks Introduction

















Tose CLOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Tail TROC
Table of contents (TOC) for this tutorial
Tage POC
Previous tutorial in this tutorial trail
Veprious
Next tutorial in this tutorial trail
Next