Showing posts with label Python Programming Language. Show all posts
Showing posts with label Python Programming Language. Show all posts

Lesson 3: A Python Program

Learn python programming in 12 lessons


Python programs are identified by the file extension “.py" for example HelloWorld.py would be the python file containing the Hello World program.

A python program would contain any of the elements below
  1. Variables: used to provide easily identifiable names to objects example of good names for variables are x, y, z, name, boy, girl, school, this_school, my_wallet, thisSchool, myWallet, etc
  2. Functions: used to perform evaluations on parameters e.g. sqrt(4)
  3. assignments statements: used to associates variables with objects e.g. x=6
  4. Input statements: used to request input from the user. This are easily identified by the input() function. e.g. input(“Please enter a number: “)
  5. Output statements: used to display items on the screen. This are given by the print() function. e.g print(“Hello world!”)
  6. Comments: used to provide clarity to future programmer reading your code. Any line of code preceded by # in python is regarded as a comment and would be ignored when the program is executed.
When naming your program, function or variable, you would want to not use a reserved python word such as def, del, is, in, import, while, etc. Look at the complete list of reserved word in Python and know them by heart. The following line of code does just that.

import keyword
keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Now that you are aware of some of the elements of a program, you should be ready to start writing your own python programs. The program below calculates the average of three numbers and displays it on the screen.
#Program to display the average of three numbers on the screen
#program name: AddThreeNums.py

num1 = 1
num2 = 2
num3 = 3


total = num1 + num2 + num3
average = total/3


print(total)
print(average)
This program would output
6
2

This would be considered as a bad program because it does not inform the user of the program as to what the 6 and 2 represents. It would be ideal to display total and average next to their corresponding values. The program below is an improvement to the first
#Program to display the average of three numbers on the screen

num1 = 1
num2 = 2
num3 = 3


total = num1 + num2 + num3
average = total/3


print(“Total”, total)
print(“Average”, average)
The correct output would be
Total: 6
Average: 2.0

If your program gives a different output other than the one above, then you are probably not using python 3. Note that average is a double number and to convert it to an integer, enclose it like int(average) to get the output below.
Total: 6
Average: 2
read more

The Python Series

Learn python programming in 12 lessons


When most people hear of Python, they think of the big snake native to most African and Asian countries. For others, Python is one of the most popular high level programming language used today in the industry and academic institutions because of its ease to learn and create code fairly quickly. 
It is mostly common amongst mathematicians, physicist, etc because of its ability to handle big number better than other programming languages such as Java.

Python was created in the late 1980s by a Dutch programmer called Guido van Rossum and he specifically chose to call it Python not after the snake but after a British sketch comedy show called Monty Python.

If you are interested in learning how to write code's, python would be the best language to learn because as powerful as its is, it has the easiest syntax and due to its popularity, you are never going to run out of reference materials.

To start programming in Python, you will need to go to the Python Software Foundation website and get the latest source code, preferably Python 3.* You can still still use Python 2 if you are using a reference book that was written before 2009 which is still going to be OK but for enhanced features and security patches, pick the latest.

The next thing you will need is an Integrated Development Environment known as an IDE and there are a lot of them on the market. Pick the one that is free, small and has a simplified graphical user interface with debugging capability. The debug tool is really important especially for an amateur programmer because it will help you spot part of the code that is producing unwanted results. There are lots of IDE on the market, but you can start with free IDEs such as PyCharm or Wingware.

Programming guru's do not really need IDE, a simple notepad will get the job done and the program is executed via the command line. Whilst we are talking about notepad, you might also want to get yourself Notepad++ and Sublime Text  to use when you reach the point where you can code without an IDE.

One of the most important resource you are going to need is a reference book to help you understand some of the concepts better. A hard copy book is important because it will still be able to guide you way better especially in cases where you have no access to the internet to look up concepts that are bothering you. If you cannot find a good book in your local bookstore, look at getting anyone of these books from Amazon.
  1. Learning Python by Mark Lutz
  2. Python Programming for Begginers by Jason Cannon
  3. Python programming by John Zelle
That's pretty much all you need to start learning programming concepts by using Python. Click and bookmark the label The Python Series to see all posts related to Python. This series of post won't cover the hardcore stuffs, just the basics to get you interested in programming with Python.
read more