Becoming a Pythonista

Angel Mariano
The Startup
Published in
7 min readSep 24, 2020

Here we will be covering the basics of Python Programming:

i. Strings and Lists
ii. Variable Declaration
iii. Comments
iv. IF and ELSE Statements
v. FOR loops
vi. Functions, Range ( )Functions

Last time in our FTW Data Science class, we had an activity where we had to create a 10-Question Python Quiz Code that counts the score in which you need to have 70% correct answers to pass the quiz.

So, here is the output of what I have created:

To give context to the quiz taker, it says that it is a “fun” physics quiz! Also, the code is able to ask a personal detail (Name).

After I have entered my name, it welcomes me and the quiz begins….
Let’s try to answer!

Oopsie, seems like I didn’t get anything right.

Let’s try it again!

Yay! Now we got 10 out of 10! And it says that we passed the quiz. 😀

If you are curious on how I created this mini quiz code, well here’s how it went:

I used the function “input” for me to get the quiz taker’s name. Also, I used “\n” whitespaces to create a well formatted quiz in the output.

Then, we can also see that I have defined a variable called score then set it score = zero to start with.

Also, we can observe comments that starts with the hash mark (#). These comments do not execute, when you run a program you will not see any indication of the comment there. Comments are in the source code for humans to read, not for computers to execute.

Here, I have printed the question. Then defines a new variable called “answer1” which is the correct answer for the question, and a variable called “response1” which asks the quiz taker to input their answer. I then used the if and else statements that evaluates if the response is not equal to the correct answer then the score will just be the same as the initial score, otherwise if it’s correct then 1 point will be added up to the score.

Then here comes the sad part..

This procedure goes on and on repetitively for every questions down to 10.

The only difference for the every questions are the question itself, the correct answer and the variable numbers 1–10 (answer and response). I thought that this procedure was quite efficient since this activity was time-pressured when it was given to us. And if you were to ask me, the most challenging part I did was the “copy and pasting” part 🤦 🤦‍♂️ 🤦‍♀️

As for the scoring:

Here I have converted the score into string for me to print it with the remarks. Also, it can evaluate whether the taker passed the quiz; if the score is greater than or equal to 7 which is 70% of 10 then it means that they passed, or else if not then they failed. Another problem here is that this code will not be reusable if the quiz does not have 10 questions and still have 70% as the passing rate.

Upping our game to be a Pythonista means we need to practice more and more and more Python coding. So then I decided to improve the quiz code by using more efficient functions to AVOID this kind of repetitious method I created before and make it more dynamic.

.

As a result, here is my Upgraded Quiz Code:

Output:

As we can see, I managed to create the same output with fewer lines of code using functions and loops.

Let’s try to go through each part of the updated code:

Just like before, the code gave context to the quiz taker on what the quiz was about. Also, asks the name of the user.

For this part of the code, I introduced 2 functions that will be used. The first function, called “checker” checks if the correct answer is equal to the input response of the taker, and if yes then it will add up 1 point to the current score. The other function called “pass-fail” will be used to know whether the taker passed out 70% passing benchmark.

This is the Main Code which consists of the list of questions and its corresponding list of answers. Unlike before in which I have separated each questions, here I have consolidated them in just 1 part of the code. Also, I set the score named “fin-score” to zero at first, then defined the variable length that counts the number of questions which I will be using for the next part:

Here, I used the for loop statement which iterates for every item “i” in the range of the length, in this case range (0,10), that prints out the question with its corresponding index in the questions list. Then, it will ask for the response/answer of the taker for that question. And with that, the function “checker” will be called to check if the response input is equal to the correct answer to add up 1 point to the score, or else it won’t.

Lastly, this part of the code prints out the final score out of the total, then the “pass-fail” function will be finally called to evaluate whether the quiz taker passed the exam.

Then that was it! I have learned that even if your code primarily works, if you think that it can be improved then work on it. Always practice your way to be better! Be versatile and adapt to different ways/platforms.

So, for the next section, I will try this code in a different code editor called the Spyder IDE:

.

First thing to notice is that the Spyder IDE displays the details of variables in the Variable Explorer after running the code, this is not present in any Notebook type of code editor like Jupyter/Colab. However there is an available variable inspector extension for Jupyter notebooks. Also, one thing that I have noticed when I was running the code, the Colab tends to be a little slower compared to Spyder.

Spyder IDE Output

Colab Output

Comparing the output of the Colab vs. Spyder, I find that the Colab version looks more interactive and a lot user friendly especially for those 1st time users as compare to the Spyder output.

There are lots of code editors/ IDEs to try, so it is great to find what’s best for you, or try to be versatile!

--

--