Variables

In Python, we have several data types that are used to store different kinds of information:

  • Integer: Represents whole numbers without a decimal component.
  • String: A sequence of characters enclosed within quotes.
  • Float: Represents numbers that contain a decimal point.
  • Boolean: Represents one of two values, True or False.
  • List: An ordered collection of items enclosed within square brackets.
  • Dictionary: A collection of key-value pairs enclosed within curly braces.
  • Tuple: An ordered collection of items enclosed within parentheses.
my_number = 1000      # Integer
my_text = "Hello!"    # String
my_float = 7.5        # Float
my_bool_1 = True      # Boolean
my_bool_2 = False     # Boolean
my_list = []          # List
my_dictionary = {}    # Dictionary
my_tuple = (1, 2, 3)  # Tuple

These data types are essential for storing and manipulating different kinds of information in Python.

The name you see before the '=' is what's called a "variable". You can name it whatever you want! Above you can see how I named my variables eg; my_number, my_text, my_float, my_bool_1.

It's good practice to name your variable relevant to its purpose. For example if I was creating a variable that would represent the cash price of an item, I might name it something like:

item_cost = 1                       # The cost of an item, which will be 1 dollar
supply_remaining = 2_500_000_000    # The number of items in inventory that are remaining, Note that we use '_' instead of commas as a separator
name_CTO = "John Woods"
value_of_a_quarter = 0.25

You might notice that I use the underscore symbol "_" instead of spaces in my variable. Something to keep in mind is you cannot start a variable with an integer, use any special characters aside from "_", and you cannot use spaces. Here are some examples of incorrect variables that will not work in your code, and will cause an error, DO NOT USE:

3variable = 5     # Starts with a number
my-variable = 6   # Contains a hyphen
my variable = 7   # Contains a space

General Arithmetic for Integers, Floats, and Strings

General arithmetic operations are a great place to start! Let's try creating a number variable, and printing it to the console.

If this is your first time programming, or even if you have programmed before, you'll quickly find out or are already familiar that printing is one of the most commonly used functions for debugging. It's how you "see" what the value is of your variable at a specific point in your code.

my_number = 1
my_number_plus_one = my_number + 1
print(my_number_plus_one)

The steps that occur here:

  1. Created my "my_number" variable and assigned it to the integer 1
  2. Created another variable called "my_number_plus_one" and assigned it to what "my_number" is when 1 is added to it
  3. Used the "print()" function to see the value in my terminal

There are several arithmetic operators (Like "+" or "-") available in Python, and they are as follows:

a = 10
b = 3
addition = a + b          # Addition: 13
subtraction = a - b       # Subtraction: 7
multiplication = a * b    # Multiplication: 30
division = a / b          # Division: 3.3333333333333335
floor_division = a // b   # Floor Division: 3
modulus = a % b           # Modulus: 1
exponentiation = a ** b   # Exponentiation: 1000

Similar to integers and floats, you can also use arithmetic operators on strings, these are mostly limited to:

  • Addition and multiplication: +, *

The act of combining strings together or multiplying a single string is often referred to as concatenation.

result = "Hello, " + "World!"
print(result) # result is "Hello, World!"
string = "Hello"
result = string * 3
print(result) # result is "HelloHelloHello"

We won't dive into Lists, Dictionaries, or Tuples just yet, but I would like to mention, and this is something you'll see in practice later— that lists and dictionaries are mutable, whereas tuples are immutable. When you hear mutable, think "can be changed", where mutable means it can be changed and immutable means it cannot be changed.

Example:

my_tuple = (1, 2)
print(my_tuple[0])
my_tuple[0] = 2
print(my_tuple)
# ERROR
#    my_tuple[0] = 2
#    ~~~~~~~~^^^
# TypeError: 'tuple' object does not support item assignment

A more advanced concept to start early with, but another thing we will review later:

Examples of mutable and immutable parameters on an asset on Algorand

Immutable Parameters:

These parameters can only be specified when an asset is created.

  • Creator: The address of the account that created the asset.
  • AssetName: The name of the asset.
  • UnitName: The unit name of the asset.
  • Total: The total number of units of the asset.
  • Decimals: The number of digits to use after the decimal point when displaying the asset.
  • DefaultFrozen: Whether the asset is frozen by default.
  • URL: A URL where more information about the asset can be retrieved.
  • MetaDataHash: A commitment to some unspecified asset metadata.
Mutable Parameters:

These parameters can be changed after the asset is created.

  • Manager: The address of the account that can change the asset's mutable parameters.
  • Reserve: The address of the account that holds the asset reserve. (Cannot be changed if initially not declared)
  • Freeze: The address of the account that can freeze or unfreeze user asset holdings. (Cannot be changed if initially not declared)
  • Clawback: The address of the account that can revoke user asset holdings and send them to other addresses. (Cannot be changed if initially not declared)
*Note: Exception for various ARC types like ARC19 and ARC69 that use the reserve address and/or note field to point to metadata*

Quiz

Question 1

What data type is used to store whole numbers in Python?





Question 2

Which of the following is an example of a correct variable name in Python?





Question 3

What will be the result of the following code snippet?

string = "Hello" 
result = string * 3
print(result)




Question 4

Which Python data type is immutable?





Code Editor