«

»

Dec 08 2012

Print this Post

Introduction to programming using Python

Python 3Python is a powerful high level programming language that is relatively easy to learn. It is used in several different industries from network based gaming(eg. WOW) to database management (payroll). This is why most high educational institutions offer Python 3 (the most recent version as of 2012) as an introductory course. Python is very similar to Java and while it may not provide a comprehensive codebase for solving complex problems like C#, it can lay the foundation for programming for novice users.

Most students will not be surprised to hear that learning curve for most subjects depend on both the student and the teacher. The style of teaching and style of learning can have a significant impact on your precipitation towards subjects(I used to hate Calculus!). Python 3 is an easy language to learn if you get hold of fundamentals. Let’s look at the fundamentals of Python(and also be found in many other languages).

#
# This is my very first code. 
# This program will prompt the user to enter their name and where they are from.
# It will print a statement on the terminal (Python GUI/CMD)
#
print ("What is your name?")
name = input()
# input func. with a print statement)
city = input("Please enter where you are from?")
print ("My name is", name, "and I am from", city)
#
# END of Program
#

The very definition of a computer is a unit that takes an input, process it in someway and provide an output. The above accomplish exactly that by requesting the user for two inputs and then printing some meaningful output. Some of the Python 3 built in features(functions) can be identified as;

– the hash tags (#) are used for block comments function where a user readable comments can be written into the program. Anything within these tags will not add value to the programming code itself. However, it will add the value to the file because they are often use as algorithms and indicators for others. Good programmers always make comments in their programs.

– “print”: take either a statement in text format or a value of a variable and display it to the user

– ” = “: the assignment operator is one of the fundamental constructs which is used to copy values to an object

– the value on the left of “=” is known as the variable(in this case they are, “name” and “city”)

– the variables store values for later use by the program

– the value on the right of “=” is the “input()” which will call in the input function from Python library

– the “input()” function is somewhat flexible. On the prompt to the user, separate print() statement was used while when the program asked for where the user from, a single print() and input() statement was used. In general, programmers usually use the later method. Both will result in exact same operation.

NOTE: this is an introductory programming lesson and proper formatting is ignored.

– the last “print()” statement has several features. It will display a message in text format My name is. As soon as the program hits the end of the quotation (“), it will look for a comma (,) followed by a previously defined variable, name. The name variable is followed by another comma for termination for the variable name. This is optional because you can avoid the comma after a variable, if and ONLY if there is no other variables or print statements followed.

– in my example, a print statement and another variable is followed after the first variable, name. Therefore a comma (,) is placed right after the first variable followed by quotation (“) for the second print statement. Note that the second variable, city does not have a termination comma (,). Assuming the user entered “Sanuja” for the first prompt and “Calgary” for the second, The final print statement for the following user input would look like;

#
# This is my very first code. 
# This program will prompt the user to enter their name and where they are from.
# It will print a statement on the terminal (Python GUI/CMD)
#
print ("What is your name?") # user entered Sanuja
name = input()
#
# The program now holds "Sanuja" in name holder
#
# input func. with a print statement
city = input("Please enter where you are from?") # user entered Calgary
#
# The program now holds "Sanuja" in name holder and "Calgary" in city holder
# The only process the program now perform is printing the values for variables
#
print ("My name is", name, "and I am from", city)
#
# END of Program
#

Output ==========> My name is Sanuja and I am from Calgary

Note that the termination commas do not appear on the print statement that was displayed by this program.

Support Materials

Python 3 packages download for Mac/Windows
Notepad++ Editor
MS Visual Studio Express

No, you do not require a compiler to test Python codes. Yes, you must install the Python module in your computer to run scripts. You can run a program either on a terminal or on any interface that supports Python 3. You can also find more information though many other websites and free help guides. Python is an open source programming language with a growing community of academics, professionals, students and enthusiasts.

Permanent link to this article: http://sanuja.com/blog/introduction-to-programming-using-python