Andela Boot Camp Day — 3
This was a very educative day and interesting too. After our awesome warm ups we got to coding and learning.
Our first order of business was to recap on data types. We did a function that returns specific commands based on the data type given in the argument. I did it in a different manner, actually I will just post it here.
def data_type(x):
“””
Takes in an argument x:
— For an integer , return x ** 2
— For a float, return x/2
— For a string, returns “Hello “ + x
— For a boolean, return “boolean”
— For a long, return squaroot of x
“””
if isinstance(x, int):
return x ** 2
elif isinstance(x, float):
return x / 2
elif isinstance(x, str):
return “Hello {}”.format(x)
elif isinstance(x, bool):
return “boolean”
elif isinstance(x, long):
return “long”
else:
return “That’a not a primitive data type”
The isinstance function checks if the input x is of type integer, float, string, boolean or long integer.
Next in line was data structures. Python has four data structures i.e list, tuple, set and dictionary. A list is like an array in other programming languages though in python it is really powerful as it can hold all data types in one list. A dictionary on the other hand is an associative list. This means that it is represented in key, value pairs and it is not sequential. So to access values in a dictionary you use keys. A tuple basically is an immutable list, once created it can’t be modified. Lastly a set holds unique values but it is mutable.
Now comes the interesting stuff, do you know that programming is basically looping through data structures and performing conditional operations and some other basic stuff, only that it is done on a large scale,that’s what I realized yesterday. Anyway we were to write a code that iterates through a list and return its value in reverse. There are a number of ways to do that, but what caught my eye was using the range function. The range function has this syntax
range(start, stop, step)
So basically you just have to give the start as the last value in your list, stop to be the first value and step to be the negative direction of how the original list is moving. For it to work you can use a while loop.
We also did a *args and **kwargs. Let not this names fool you, it is a very simple concept. Suppose you are working on a project and you have no limit for the number of arguments you want in a function then *args comes in handy just make sure to unpack it. **kwargs on the other hand is for key value pairs.
Before lunch we went through the concept of classes. As you know classes is the implementation of object oriented programming. Nandaa(our trainer) used a very interesting example. So we have this class called Person which basically has name and age of a person. The funny part was inheritance. So to demonstrate inheritance you create a class called Kenyan which inherits the two properties in class Person and has an action of probing a person to know if they are corrupt or not. You can imagine how Kenya is recognized for corruption. Anyway it made the concept sink in. So the assumption will be a Kenyan is not corrupt until he/she is probed and proven to be corrupt. We used a class variable to initialize that. So class Kenyan is a subclass or child to class Person.
After lunch we learnt more about importing modules and differentiating it from inheritance. So importing implies carrying some features of a particular module and using it whereas inheritance is like the concept of humans giving birth. You inherit some traits from your parents but still have your own unique traits.
The best part of the day, we were given some tips on becoming good programmers. It’s this simple, CODE EVERYDAY…and to put in other words LBD — Learn By Doing and don’t forget to push your work on github, you never know who is watching.
Finally when the day ended I just got motivated even more and I have set even greater goals for myself. Before I forget we are to take a course by Udacity on Introduction to Computer Science which was recommended to us by Nandaa. That is now top on my priorities.
I have started working on the code katas that we were given and hopefully when the week ends I will have completed them.
Dont forget LBD!!!!!!