cristien.

Week 1 Study Notes - Learning Python with Code First Girls


Error Messages #

One of the things that I love about the classes so far is that we are encouraged to experiment with code.


I am more of a Front End engineer and use JavaScript with HTML and CSS. When I first learnt JS or programming in general (that was 2 years ago), I was worried about making mistakes but over the time I’ve been coding, I’ve learnt that understanding error messages are more important than remembering syntax and that’s exactly what they taught us in the first class.


Sjoukje said “Try and remove the parentheses then see what happens. Will it still print?”, “What if you don’t put the quotation marks?”, etc. By introducing us to error messages, how to understand them and eventually how to fix them.


That is valuable and I think anyone who are new to coding will feel more empowered through this.


This week I’ve learnt about: #


1. Data Types

We were introduced to different Data Types in Python:


2. Functions

To write function you need the keyword def

eg:

If you want to have a function that prints hello world in the console:


def say_hello():
  print('hello world')

Now, if you want the output to be more flexible so you don't have to keep going back to that function to change it, you can put an argument (some sort of placeholder variable) inside the parentheses.

eg:


def say_hello(name):
  print('hello ' + name)


3. For loop

For loop is a way for us to run a program without having to repeat the same code over and over again. It's to keep the code DRY (Donot Repeat Yourself).

You need the keyword for and in.

eg:
Let's say I want to write a code where I print number 1 to 10 in the console.

  # non DRY code example

  print(1)
  print(2)
  print(3)
  print(4)
  print(5)
  print(6)
  print(7)
  print(8)
  print(9)
  print(10)


  # DRY code example

  for number in range(1,11):
    print(number)

Both output the exact same thing but the first one is repetitive.

range([start],[stop]) in the example above is a built-in function in python where it generates a number between specified start number to specified stop number (not including) in another word start <= number < stop

There is another way to loop by using while loop.

  num = 1

  while num < 11
   print(num)
   num +=1

This generates 1 to 10. It will execute as long as the condition is true. So if for instance you do this:

  num = 1

  while num < 11
   print(num)

The program will go to infinite loop because it will keep running.


4. Library or Modules

Module can be considered as code library.
You can create your own module and import it in the file that requires it.
You can use built-in module such as turtle.py which is a cool little library that they introduced in the class - you can draw with it.

The key thing about module is to ensure that we import the module in the file.

eg:

You want to generate random numbers. There is a built-in module called random.py

  import random

  # use random module to generate random number

  random.randint(1,10)


  1. Variables

Variables act as storage! So you store the value that you want to keep and reuse for later on.

eg:

  # I want add 2 numbers.

  number_1 = 10
  number_2 = 5
  result = number_1 + number_2

  print(result)


That's all for now :)

I have thoroughly enjoyed the class this week and looking forward to learning more!
See you in the next study note.