coding script

Special Topics in Coding: Python Dictionaries

By Tristan Martello, Co-Founder at Kickstart STEM

Graduates of our Kickstart Python class (or coders familiar with Python) may recall the importance of lists and tuples when making programs. These structures are crucial to Python, and programming in general; the ability to store multiple variables as ordered items in a list is incredibly powerful. Dictionaries are another type of variable used to organize data in a program. They are similar to lists, but also have some very important differences. 

The first, and most obvious difference is the notation. Lists in Python are declared with square brackets, while dictionaries are declared with curly braces. In the code segment below, line one is the declaration of an empty list, and line two is the declaration of an empty dictionary. 

The second, and most important difference between lists and dictionaries is their structures. Lists are known as an ‘Ordered Variable’: this is fairly straightforward, their contents are ‘in order’, organized by index. The first element in a list is always found at index zero, the second element is always at index one, and so on. If you have a list and want to find the third element, all you have to do is ask for the element at index 2:

Code:

Result:

Since the elements are all stored in order, running this code segment will print the element at index 2 in the list, which is the string ‘Blueberry’.

Dictionaries, however, are known as an ‘Unordered Variable’. This means that unlike lists, the elements are not stored in order, and you cannot use any commands relating to index. Instead, dictionaries are organized with ‘Key value pairings’. Since these structures are called dictionaries, you can think of keys and values as words and definitions. Each key in a dictionary has a value associated with it, just like each word in a dictionary has a corresponding definition. The following code defines a dictionary variable that behaves like a real life dictionary:

This dictionary definition contains three key-value pairings. The keys are the strings “Apple”, “Peach”, and “Blueberry”, and the values are descriptions of each fruit. In this type of dictionary definition, the key and value are separated by a colon, and there is a comma between each key-value pair. 

In this example, all the keys and values are strings, but you can mix and match variable types however you want; strings, floats, ints, and even lists are acceptable options for both keys and values. 

The previous dictionary definition shows how to define a whole dictionary at once. But what if you want to add items to the dictionary after you initially create it? Or what if you want to change the value for a certain key? Just like how you change the element at a certain index of a list, you change the value for a certain key in a dictionary by using brackets. 

On line 3, this code segment creates a new key called “Coconut”, and pairs it with the value “A round, shelled, hairy, fruit”. On line 4, this code segment takes the key “Apple” and pairs it with a new value, “Big red fruit grown in the fall”. 

Since dictionaries are unordered, you can’t use indexes to find items. If I typed in myDictionary[0] to try and get the description of an apple, the program would crash. The notation for finding items in the dictionary is similar to the definitions in lines 3 and 4 of the code above; you use keys inside brackets to find values that already exist.

Code:

Result:

Line 6 of the code segment above will print out “A round, shelled, hairy fruit”, since that is the value associated with the key “Coconut”. 

The last important aspect of dictionaries is how they work with loops. Since dictionaries are unordered, loop structures that correspond to indexes (while loops and for loops) are not intuitively compatible with dictionaries. Foreach loops, however, work wonderfully with the key-value pairings of a dictionary. The following code shows how to use a for loop to iterate through all the items in a dictionary.  

Code:

Result:

There are two important takeaways from the foreach loop in that code segment. First, in line 6 (the loop declaration), the foreach loop iterates through all the keys in myDictionary- this means that the loop runs one cycle for each key. In this case it runs four times, for Apple, Peach, Blueberry, and Coconut. This also means that each time the loop runs, the variable fruit is declared as a different string- those four fruits. 

The second important takeaway is in line 8 where we use the same accessing notation as before. Except this time, instead of having a specific string inside the brackets, we have the variable fruit. Since fruit is declared as a different string each time the loop runs, line 8 prints a different definition each time. This structure is why we can use two lines of code (lines 7 and 8) to print 4 different key-value pairings. 

If you can recreate the code above and explain how it works, you have mastered the basics of python dictionaries! Remember, the examples shown here only demonstrate a small fraction of what you can do with dictionaries, I’d recommend practicing with these on your own to explore different applications and variable type combinations. Good luck!

product_3_labeled

Top 5 Most Earth-Like Planets

By Andrew Dunbar, Co-Founder at Kickstart STEM

Ever look out into space and wonder if alien life may be out there in the distance? Well, since we know that our planet Earth is rich of organic matter, we can only imagine that the most earth-like planets may be the best candidates for otherworldly existence.

Based on Earth Similarity Index (ESI), scientists have found a metric to compare the relative earth-likeness of various planets in our viewable universe. So far, the top 5 earth-like planets within reasonable distances have been argued to be:

  1. Kepler 438b: ESI = 0.88
  2. Gliese 667Cc: ESI = 0.85
  3. Kepler 442b: ESI = 0.84
  4. Kepler 62f: ESI = 0.83
  5. Kepler 452b: ESI = 0.83

Let’s explore what these planets are really like!

  1. Kepler 438b: Earth Similarity Index = 0.88

The planet exists 470 light years away from Earth, and at only 10% larger than Earth and receiving roughly the same amount of solar energy from its star as the Earth does from the sun, individuals have argued for a long time that life may be viable on this planet. However, its sun is only about half the size of ours, and its on average 2,000 degrees cooler–a mostly red and infrared radiating star. Though human life may not be viable on this planet for these reasons, experts argue that general life may in fact be in some other form.

2. Gliese 667 Cc: Earth Similarity Index = 0.85

Gliese 667Cc orbits within the “habitable” zone of a red dwarf star approximately 22.18 light years away from Earth. The exoplanet (a planet which orbits a star) has a mass approximately 3.7x that of Earth’s, and has an equilibrium temperature of 39.6 degrees Fahrenheit (compared to -1 degrees Fahrenheit for Earth). According to Nasa studies, based on black body radiation the planet should absorb only slightly more electromagnetic radiation as Earth, explaining the slightly higher equilibrium temperature. Moreover, the sun that this planet orbits is a red dwarf star with about a third of the mass of the sun and a life expectancy 10-15 times longer than our sun.

3. Kepler 442b: Earth Science Index = 0.84

Kepler 442b is considered a “super Earth” planet. Orbiting its star 1,206 light years from Earth in the constellation Lyra, the planet has an orbital period of 112.3 days and a mass approximately 2.3 that of Earth’s. In fact, its star is approximately 40% less massive than the Sun, so it can live for about 30 billion years compared to our Sun’s 5 billion years. Fun fact: this planet was discovered by the “transit method” in which the planet’s “dimming effect” caused by crossing in front of its star is measured. Oh, and also, gravity on Kepler 442b is 30% stronger than Earth’s, so if you weigh 100 pounds on Earth, you’ll instantly be 130 pounds on Kepler!

4. Kepler 62f: Earth Similarity Index = 0.83

This super Earth planet is located about 990 light years away from Earth in constellation Lyra. Kepler 62f orbits its star at roughly 66,000,000 miles from its host star, with an orbital period of roughly 267.3 days. Its mass is 2.8 times that of Earth’s, and has a radius of approximately 1.41 times that of Earth (think square root of two!) Its parent star is relatively quiet, and has less mass than our Sun so it can live significantly longer. According to experts, its atmospheric viability is not currently known, but its physical properties so far discovered have been deemed quite promising for potential habitability.

5. Kepler 452b: Earth Similarity Index = 0.83

Called the “bigger, older cousin to Earth,” Kepler 452b exists in the habitable zone around a G2 type star–the same type of star as our very own sun. Liquid water is known to pool on the surface of this planet, likely because of its proximity to the star, and the planet has roughly 60% larger diameter than Earth. While its mass and composition aren’t yet determined, Nasa says this planet is rocky, and can offer insight into the potential future of Earth. Kepler 452b is 1400 light years away in the constellation Cygnus, so speedy travel will be necessary to ever get there. Are there aliens already on this planet? Maybe!!

Thanks for enjoying our blog about the top 5 earth like planets. For more cool info, subscribe to our newsletter and get weekly updates about new cool articles!