Getting Started With Python

March 12th, 2012 (Guest) Leave a comment
Like the article?
Getting Started With Python

Python is a high-level programming language that is highly readable and allows the programmer to use English-like words to write their programs. This interpreted language is powerful but easy to learn and can be used to write a multitude of different programs from simple text programs to games. If you are new to programming, Python is a great language for beginners and this tutorial will help you get started writing Python programs.

For those of you just getting started with Python, you will need to set up your environment. Python is an open-source language available under the GUN General Public License (GPL), which makes it installable on a wide variety of platforms such as Unix, Windows, Mac, OS/s and several others. Your first step is to get Python installed on your system.

Installing Python

You will need to obtain the latest source-code, documentation and other items from the official website at:

Click the download button on the menu and find the installer that goes with your operating system. The documentation section can be found on the menu or by going to:

Once you have downloaded the installer, run it on your computer so that you have the binary code available on your platform.

Getting Your Development Environment

It is recommended that you run Python from a graphical user interface (GUI) if you are not used to working in the command line. There are several environments available, some of which you may already have installed if you have already been programming in other languages. The top 5 IDEs for Python are:

  • Komodo
  • NetBeans
  • Python Tools for Visual Studio – an add-on for Microsoft Visual Studio
  • PyStudio
  • PyDev – an open-source plug-in for Eclipse

You will need to download and install one of these IDEs if you do not already have one. Most installations are straight-forward and do not require any configuration. Before proceeding, make sure your environment is setup and working. Check the documentation for your chosen IDE if you are having trouble.

Your First Python Program

Python programs are written in .py files and since they are interpreted, do not require any compilation before execution. To test your environment and be sure that you can run your Python scripts without a problem, try the following:

print('Hello World')

If everything works OK you should see Hello World in the command window when you run. If you have a syntax error the program will stop and will try to help you identify the problem.

Next, let’s set up an identifier. An identifier, or variable, is a way of naming a stored piece of information such as a function, class, module or other data. In Python, identifiers must start with a letter A to Z, a to z or an underscore ( _ ), followed by a zero or more letters, underscores or digits 0 to 9.

Python does not allow punctuation or special characters as part of identifier names. It is also case sensitive, so the identifier MyNum is not the same as Mynum. Finally, there is a list of reserved words that can be found in the documentation which cannot be used as identifier names.

To get started, let’s try the following code:

print('Please enter your name')
Name = input()
print('You said your name is ' + Name)

In this code we are prompting the user for their name. Once they have typed in their name and pressed Enter, we should see the statement “You said your name is X”, where X is the name they typed. Pretty easy, right? Let’s continue.

Decision making statements are the foundation of modern programming and are necessary to determine a course of action based on a value or result. Here we will ask the user for their age and give a response:

print('How old are you?')
Age = input()

Age = int(Age)

if Age <= 10:
        print ('Wow, you are young, ' + Name)
elif Age < 65:
        print ('That is still pretty young, ' + Name)
else:
        print ('Sorry, thats pretty old, ' + Name)

In this section we are prompting the user again to enter their age. Once they enter it we place it into the Age identifier. Because data that come in from the Input() function is string, we must convert it to integer data to work with it in our if statement. The if statement determines if they are under 10 and give a response or over 10 but less than 45 and give another response. If they do not meet any of that criteria they are given the “Sorry, thats pretty old” response.

Comments in Python

Comments are an important part of programming since they allow you to write notes about the code, make TODO lists and communicate about the functionality inside the program. In Python, the hash sign (#) is used at the beginning of a line to make a comment. Such as:

#Prompt the user
print('Please enter your name')
#take the name they input and assign it to "Name"
Name = input()
#print the result
print('You said your name is ' + Name)
input()

Comments are ignored by the interpreter and only for your use. Here is the whole program now, with comments:

#Prompt the user
print('Please enter your name')

#take the name they input and assign it to "Name"
Name = input()

#print the result
print('You said your name is ' + Name)

#prompt the user for their age
print('How old are you?')

#assign the age value to "Age"
Age = input()

#convert the string Age to integer
Age = int(Age)

#if statement that determines the correct response
#based on the given age
if Age <= 10:
        print ('Wow, you are young, ' + Name)
elif Age < 65:
        print ('That is still pretty young, ' + Name)
else:
        print ('Sorry, thats pretty old, ' + Name)

There! Now you have written your first Python program. You are now ready to learn more about the Python language and there is much more to explore! Python supports a range of functions and object-oriented style that you can master to create a wide variety of applications. The key to being a good programmer is practice, so go find some tutorials and begin working through them. You will be good at Python in no time!

Help us spread the word!
  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Delicious
  • DZone
  • Reddit
  • Sphinn
  • StumbleUpon
  • Google Plus
  • RSS
  • Email
  • Print
If you liked this article, consider enrolling in one of these related courses:
Don't miss another post! Receive updates via email!

About Alex Dawson

Alex is a freelance web developer who has by now built a dozen of web applications using the Python and Django combo. He has written several articles on this and other subjects related to e-commerce and web hosting, which can be found on his EzineArticles author profile.

Comment