Python as a choice of programming language

Daizy Obura
6 min readSep 11, 2018

If you need a general- purpose, extensible and dynamic language to build up your desktop application GUI, website or web application, python is the right language for you to use.

For starters, it is a high level programming language and therefore is close to natural language, making it easy to read and understand. You do not need to first learn the language syntax. All you need to do is follow the good practices that it enforces for example proper indentation.

Summing two numbers and displaying the output is as easy as:

1. num1 = 21
2. num2 = 7
3. sum = num1 + num2
4. print("The sum of " + str(num1) + " and " + str(num2) + " is " + str(sum))

When you run this code snippet in your terminal by typing ‘python <filename.py>’ in the directory of the file, for example:

daizy@daizy-Satellite-C55-B:~/Andela WIT C2/Daily Challenges/Day Four/Recursion$ python recursion.py

It would yield such output:

The sum of 21 and 7 is 28

That was quite simple, wasn’t it? We did not even have to specify the data types. However, notice that on line 4 as we were concatenating the words/strings and numerical values, we had to cast the integers to strings. Otherwise, it would yield a TypeError because strings and integers cannot be concatenated.

The code is also readable and python focuses on core functionality of an application by taking care of common programming tasks. It’s syntax allows us to write out programs/applications in considerably fewer lines of code than other languages. It allows us to express concepts that lead us to achieve functionality.

Also note that we did not have to first compile then run the code, because it was processed at runtime by the interpreter; that is how python works.

For those who love to know about history, python was created by Guido van Rossum and first released at alt.sources in 1991.

I remembered all my experience and some of my frustration with ABC. I decided to try to design a simple scripting language that possessed some of ABC’s better properties, but without its problems. So I started typing. I created a simple virtual machine, a simple parser, and a simple runtime. I made my own version of the various ABC parts that I liked. I created a basic syntax, used indentation for statement grouping instead of curly braces or begin-end blocks, and developed a small number of powerful data types: a hash table (or dictionary, as we call it), a list, strings, and numbers — Guido van Rossum

Python is developed under the Open Source Initiative (OSI) approved open source license, making it freely usable and distributable; for different purposes. Therefore, you do not have to purchase anything, just go ahead and install it. Lucky for those who use Linux no installation step has to be done, only upgrading to the latest version of python.

An outline of more python properties:

  • After you have written out your code, it can be run from anywhere, on any OS platform that is, making it portable.
  • Python supports object oriented programming concepts like classes and multiple inheritance, making that a plus for this language!
  • It also supports clearer error handling by allowing for raising and catching exceptions.

Python has a number of concepts including data structures like dictionaries, lists and tuples; functions, control flow, among others. All these concepts are very useful and help us get a great job done.

Let us go through an exercise to write a program that takes 2 lists and returns Fizz if the combined length of the lists is divisible by 3 OR Buzz if it is divisible by 5, OR Fizzbuzz if it is divisible by both 5 and 3 OR returns the combined length of the list.

We have come across and will explore: input as a list, length of the list, conditions, arithmetic and some desired output.

First off, creation of a function that holds the user input:

#function get_input that captures the input
def get_input():
#the first list
list1 = [int(n) for n in input("Enter: ").split(",")]
#the second list
list2 = [int(n) for n in input("Enter: ").split(",")]
#print out the result yielded from manipulating the lists
print(Fizzbuzz(list1, list2))

We use def to define and identify our functions then brackets() to hold arguments if any, followed by a colon: to indicate the content of the function.

list1 = [n for n in input("Enter: ").split(",")]

The expression above is simply making use of list comprehension that allows us to concisely create new lists, most times from an existing list. In this case, we are using list comprehension to say that for some variable n, capture it as input from a user and separate the values using a comma (,), so that we can end up with an actual list with comma-separated values.

We will then go ahead and create a function called FizzBuzz that will do the magic for us:

#function FizzBuzz
def Fizzbuzz(a, b):

The brackets are holding 2 arguments a and b, to represent the 2 lists that will be manipulated.

Next, we define a variable totallength which will hold the sum of the length of list a and list b:

#total length of the two strings   
totallength = len(a) + len(b)

Then we start stating conditions that will work around totallength to return output, using the if/elif conditional statements:

#statement that will return Fizz if the combined length of the lists is divisible by 3
if totallength % 3 == 0:
return 'Fizz'
#statement that will return Buzz if the combined length of the lists is divisible by 5
elif totallength % 5 == 0:
return 'Buzz'
#statement that will return FizzBuzz if the combined length of the lists is divisible by both 3 and 5
elif totallength % 5 == 0 and totallength % 3 == 0:
return 'FizzBuzz'
#statement that will return the combined length of the lists is not divisible by 3 or 5 or both
else:
return totallength

Finally, we make a call to the function that holds the input to be manipulated by FizzBuzz:

#a call to the function that holds the input to be manipulated
get_input()

The whole program should look something like this:

#function get_input that capture the input
def get_input():
#the first list
list1 = [int(n) for n in input("Enter: ").split(",")]
#the second list
list2 = [int(n) for n in input("Enter: ").split(",")]
#print out the result yielded from manipulating the lists
print(Fizzbuzz(list1, list2))
#function FizzBuzz
def Fizzbuzz(a, b):
#total length of the two strings
totallength = int(len(a)) + int(len(b))
#condition that will lead us to an output Fizz if the combined length of the lists is divisible by 3
if totallength % 3 == 0:
return 'Fizz'
#condition that will lead us to an output Fizz if the combined length of the lists is divisible by 5
elif totallength % 5 == 0:
return 'Buzz'
#condition that will lead us to an output Fizz if the combined length of the lists is divisible by both 3 and 5
elif totallength % 5 == 0 and totallength % 3 == 0:
return 'FizzBuzz'
#condition that will lead us to an output Fizz if the combined length of the lists is not divisible by 3 or 5 0r both
else:
return totallength
#a call to the function that holds the input to be manipulated
get_input()

Go ahead and run it. Happy Coding with python 😄

--

--

Daizy Obura

Believer in our Lord Jesus Christ | Techie | Web Developer | Interior Decorator | Jeweller | Learning, Unlearning, Re-learning