cristien_

Week 2 Study Notes - Learning Python with Code First Girls


This week I learnt #

We have covered the fundamentals in python with data types, variables, for loop, while loop etc last week!

This week we went deep into decision making logic and other data types such as list and dictionaries.


Decision Making Logic #

If we want to run a program but we want the program to do different things when it meets a criteria, we should apply decision making logic using the if else statements, comparison operators and logical operators

Let's say I want to run a program to check whether the weather is good or not... and if yes, maybe you go out to take your baby for a walk and otherwise, stay at home.

# user input to find out if weather is good or not
weather = input("What's the weather like? good/bad")

# using comparison operators to find out if weather is good is True
is_weather_good = weather == 'good'

# using if else statement to get output when it meets a criteria
if is_weather_good == True:
    print("Take baby out for a walk")
else:
    print("Stay at home")

Now what if I want to run a program where if the weather is good and baby is not grumpy, I take him out for a walk... otherwise, maybe wait till he's not grumpy anymore.

# user input to find out if weather is good or not
weather = input("What's the weather like? good/bad ")
baby_mood = input("Is baby happy? y/n ")

# using comparison operators to find out if weather is good or baby is grumpy
is_weather_good = weather == 'good'
is_baby_happy = baby_mood == "y"

# using if else statement to get output when it meets a criteria
if is_weather_good == True and is_baby_happy == True:
    print("Take baby out for a walk")
elif is_weather_good == True and is_baby_happy == False:
    print("Think twice about going out!")
else:
    print("Stay at home")



List and Dictionaries - Python built in data types #


What is List? #

List is a built-in data type that essentially allows your to store data (can be numbers, strings, dictionaries or even list within list) and it is a sequence type!


What is a sequence type? It means it is iterable.


Properties of list that's really useful:

  • It's mutable so they are changeable.
  • It's ordered. It has index so when you put a new item in the list, it will be added to the end of the list.
  • It can have duplicate items within the list as it's indexed.


How to create list?
Use a square brackets []


# this list contains a list of numbers from 1 to 4
this_list = [1,2,3,4]


#to find out the length of the list
length_of_list = len(this_list)

#when we print this to the console it should output 4
print(length_of_list)

#if you want to pull an item out the list eg the second item in the list
second_item = this_list[1]


What is Dictionaries? #


Dictionaries are used to store data with key-value pairs.


Properties of dict:

  • It's changeable.
  • Can't have duplicates of key.
  • It has defined order. You can't change the order.
  person = {
    "name": "John",
    "age": 33,
    "games": ["Pokemon", "Zelda", "Blackbox"],
  }

  # to check length of the person dictionaries. Should output 3.
  len(person)

  # eg if you want to obtain the person's name
  person['name']

Homework buddies #


During the course, we were encouraged to buddy up for homework so we can help each other out. In this occasion, I got together a few girls to go through one of the homeworks, which is to generate lottery numbers.


I love helping other people when they get stuck and I love being able to bounce ideas with someone else on to work out a problem or a bug. I got together with Haleema and Chloe and we went through how to generate random numbers and getting logic for the first set of random generated numbers. Our session was cut short as I had to tend to my little boy but we kept in touch on Slack to help each other out.


I also got to help Jackie to get unstuck with her while loop code. She used while loop to generate the random numbers and it was a different approach then what I took.


It was great to see how people come to solution differently and you learn a lot from others' train of thoughts. Being able to communicate my train of thoughts help me figure out which topic I should go back and read to have a better understanding.


Onto week 3 we go! Wooo hoo 🐍