06. Data Types (+10 XP)


# Variables


In programming, variables are used for storing data values. Each variable has a name and they hold a value.

The variable name can consist of letters, numbers, and the underscore character _.

These are all valid variable names and values:

          
          name = "Sonny Li"
          id = 420202269
          completed = 5
          xp = 70
          streak = 3
          verified = False
          
        

The = equal sign means assignment:

  • We are assigning the string value "Sonny Li" to the variable name.

  • We are assigning the number value 420202269 to the variable id.

  • We are assigning the Boolean value False to the variable verified.

We can also change the value of a variable, or print it out:

          
          xp = 70

          xp = 80

          print(xp)    # Output: 80
          
        

Here, we are assigning the number value 70 to the variable xp. Then, we are reassigning the integer value 80 to the same variable. And printing it out.


# Data Types


## Int


An integer, or int, is a whole number. It has no decimal point and contains the number 0, positive and negative counting numbers. If we were counting the number of people on the bus or the number of jellybeans in a jar, we would use an integer.

          
          year = 2023
          age = 32
          
        

## Float


A floating-point number, or a float, is a decimal number. It can be used to represent fractions or precise measurements. If you were measuring the length and width of the window, calculating the average test score, or storing a baseball player’s batting average, we would use a float instead of an int.

          
          pi = 3.14159
          meal_cost = 12.99
          
        

## String


Strings is a type for storing text strings. Strings are wrapped in double quotes " " or single quotes ' '.

          
          message = "good nite"
          user = '@sonnynomnom'
          
        
Back