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.
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:
- Strings: represented by quotation marks
"this is string"or
- Numbers: if it's a whole number it's called integers
- Floats: a fancy way of saying it's a decimal number
To write function you need the keyword
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.
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
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
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.
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)
Variables act as storage! So you store the value that you want to keep and reuse for later on.
# 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.