Python Open Lab Feb 19

On week two of the open labs, we continued building on the starter kit and worked on python data structures. We discussed loops (both for and while), dictionaries and dictionary methods.

A while loop is a concept that, when implemented, executes a piece of code over and over again, while a given condition remains true:

When constructing a while loop, you have to ensure that it has all three of the following elements:

  1. The while keyword
  2. A condition that translates to either true or false
  3. A block of code you want to repeat

Example:

# Input/pick a random number

number = 2

# Set the condition of the while loop

while number < 5 :

print(“Thank you”)

# Increment the value of the variable “number by 1”

number = number+1

When you run this code you produce the following result:

Thank you

Thank you

Thank you

 

For loop:

The for loop is used to iterate over elements of a sequence, it is often used when you have a piece of code which you want to repeat “x” number of times:

Example:

Let’s say you have a list:

fresh_fruits = [“Apple”, “Banana”, “Peach”, “Strawberry”]

for fruits in fresh_fruits:

print fruits

That means, for every element that we assign the variable fruits, in the list fresh_fruits, print out the variable fruits.

  1. Dictionaries

Dictionaries are similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words, and for each of them a definition. In python, the word is called a ‘key’, and the definition a ‘value’. The values in a dictionary aren’t numbered – tare similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words, and for each of them a definition. In python, the word is called a ‘key’, and the definition a ‘value’. The values in a dictionary aren’t numbered – they aren’t in any specific order, either – the key does the same thing. You can add, remove, and modify the values in dictionaries. Example: telephone book.

In the Python dictionary, each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.

An empty dictionary without any items is written with just two curly braces, like this: {}. Keys within a dictionary must be a data type such as strings, numbers, or tuples.

Example:

dict = {‘Name’: ‘Michael’, ‘Age’: 7, ‘Class’: ‘First’}

print “dict[‘Name’]: “, dict[‘Name’]
print “dict[‘Age’]: “, dict[‘Age’]

When we run this code, it produces the following result:

dict[‘Name’]:  Michael
dict[‘Age’]:  7

Dictionary Methods:

  1. clear(): Removes all items from the dictionary in place
  2. copy(): Returns a new dictionary with the same contents
  3. fromkeys(): creates a new dictionary from the given keys
  4. get(): A way to access dictionary elements. Returns None when element not present in the dictionary.
  5. has_key():  boolean method to check if a key is present in the dictionary
  6. pop(): returns value corresponding to the key and also removes the pair from the dictionary
  7. popitem(): pops out a random item from the dictionary

 

Leave a Reply

Your email address will not be published. Required fields are marked *