Vanliga datatyper

Introduktion till Python för utvecklare

Jasmin Ludolf

Senior Data Science Content Developer

Decimaltal

  • Heltal: hela tal

$$

  • Flyttal: tal med decimaler, t.ex. 1,5
  • Inga citattecken behövs

$$

$$

new_ingredient_quantity = 1.5
Introduktion till Python för utvecklare

Booleska datatyper

$$

  • Boolean: True eller False
  • Passar för att lagra ja/nej-information
  • Versalbegynnelse
  • Inga citattecken

$$

is_in_stock = True
is_in_stock = False
Introduktion till Python för utvecklare

Funktionen type

  • Datatyper:
    • Strängar
    • Heltal
    • Flyttal
    • Booleans
type()
  • Visar datatypen
Introduktion till Python för utvecklare

Funktionen type

Sträng

ingredient_name = "tomatoes"

print(type(ingredient_name))
<class 'str'>

Heltal

ingredient_quantity = 2

print(type(ingredient_quantity))
<class 'int'>

Flyttal

new_ingredient_quantity = 1.5

print(type(new_ingredient_quantity))
<class 'float'>

Boolean

is_in_stock = True

print(type(is_in_stock))
<class 'bool'>
Introduktion till Python för utvecklare

Förstå datatyper

  • Förstå vad koden gör och varför
  • Använd type() för att identifiera typen
  • Avgör vilka operationer som är möjliga

Person som arbetar med kod på flera skärmar

1 Bild av wutzkoh
Introduktion till Python för utvecklare

Operatorer

  • Symboler kallade operatorer används för beräkningar

Aritmetiska operatorer:

  • + för addition
  • - för subtraktion
  • * för multiplikation
  • / för division

Tal:

  • Fungerar som vanlig matematik
    print(2 + 1.5)
    
3.5

Strängar:

  • Bara + och * fungerar

"Hi" + "There" = "HiThere"

"Hi" * 3 = "HiHiHi"

Introduktion till Python för utvecklare

Nu kör vi en övning!

Introduktion till Python för utvecklare

Preparing Video For Download...