In the last article we learned about how to "declare" variables and learned what lists are in Python. Today we were learn about how to write functions and loops.
Let's take a look at functions in Python. To bein, a function is declared with the word "def". After 'def' write the word 'function', in paranthesis put what you want to name your function, then a colon. The next line should be indented. That will be the body of the function. Side note, Python has what is called docStrings which allows a developer to write comments about what the function does. Let's take a look at an example:
name = 'The knights who say Niii!' def beware(name): """This function says beware of the persons passed in as parameter""" print('Beware of the' + name)
name = 'The knights who say Niii!' def beware(name): """This function says beware of the persons passed in as parameter""" return 'Beware of the' + name beware(name)
Time to move on to Loops!
The For Loop
roman_names = ['Brian', 'Pilate', 'Biggus Dickus', 'Incontinentia Buttocks', 'Sillius Soddus'] for name in roman_names: print(name) //'Brian' //'Pilate' //'Biggus Dickus' //'Incontinentia Buttocks' //Sillius Soddus'
One cool thing about for loops is they have a few extra things you can do with them, using specific key words. One key word that can be used is 'range'. Range lets you iterate over a set of numbers.
for x in range(5): print(x) //prints 0, 1, 2, 3, 4 for x in range(3, 6): print(x) //prints 3, 4, 5
The While Loop
count = 0 while count < 5: print(count) count += 1 //prints 0, 1, 2, 3, 4
That was some exciting stuff! We're slowly getting the hang of Python! I encourge anyone who is interested to take a further dive into learning Python. It is an easy language to learn and much sought after by employers! It can really help beef up your reseme and make your coding projects more diverse! I hope to continue writing more about Python because there is so much more to talk about! Stay tuned!