Posts Tagged ‘hello world with python’

How to make your computer display the “Hello World!” message using the popular programming language Python. I will also introduce you to installing programs on your computer and how to create shortcuts to programs, folders, or even other shortcuts. Even if you never use the skills you are about to learn for the rest of your life you will still benefit from an insight into the mindset that every programmer uses every day to accomplish this and many other programmaticall tasks every day.

The first thing you would need to do is download the Python programming language from python.org. The install is simple, but could get complicated, so I will walk you through it. Go to python.org and download the Python installer which will be in the form of an .exe file. Once your download of the installer is finished, double-click it to start the install. A dialog box will pop-up with an open file security warning: click Run. The first screen you will see is one asking you to decide whether to install Python for all users, or to install it for just yourself. I recommend that you select the option to install Python for just your user because it will make the install quicker, and take up less hard drive space. After this decision all you will have to do is click ‘Next’ until the install is finished. By continuing to click ‘Next’ you will install Python with all the default settings for your user, which is perfectly fine.

With python installed on your computer you could start programming, but there’s one more thing that you can do to make it easier once you start writing your program. Right-click on your desktop and navigate to the ‘New’ submenu. Once there click on ‘Shortcut’ to create a new shortcut on your desktop. A window should pop up asking you to decide where the shortcut should look to run a program. In that dialog box you can either type in or copy-and-paste the following text: C:/python26/lib/idlelib/idle.bat and click ‘Next’. Now you can name your icon whatever you wish in this dialog box. I suggest just clicking the Next button without changing the name, as the default name for this shortcut will be sufficient for our purposes. This shortcut you just created will now make it much easier for you to quickly create and debug your Python programs. I cannot make this next point clear enough: debugging your python programs is very important. If you don’t debug often you can have many problems and not know about it until you have hundreds, or even thousands of lines of code. Once you’ve got more than 50 lines of code, debugging becomes much more complicated than if you had just done it systematically. Double-click the shortcut you just finished creating. Doing this will open a window that looks similar to Notepad as far as visual simplicity, but that is where the similarities end. This new window is called the Python IDLE: you will create and debug all of your Python programs in this window.

Congratulations, you have finished the hardest part!  Now it is time for you to start your first program! All python programs must start with what is called the ‘shebang’ statement, followed by the location to your python installation. The ‘shebang’ statement is important because it tells the computer that this file is a python file, which changes how the computer handles the file. Programmers, when referring to the shebang statement usually imply that the path to your python install is included in the actual shebang statement. In your case, your shebang statement, along with the path to your Python install will look like this:
#!/usr/bin/python
The red text is the actual shebang statement, which, again, tells the computer that this file is indeed a python file. The ‘shebang’ is then followed by the location that Python is installed on your computer in the UNIX format denoted by the green text. This will start all of your programs: if you do not have the shebang then your programs will fail.

Since you now have the shebang statement in your program you will want to create a variable. A variable is a name or string of names concatenated with underscores (_) that represents a numerical formation such as 12345678910, or a string. A String can be grouping of characters such as “Python is cool” or “I like programming in python,” Or a string could be a number formation, or just one number or letter. Strings are denoted by encasing the intended contents of the string within double or single quotes as we did above. For example, Python is cool could not be a string in Python because it does not have double quotes: “Python is cool” is a valid string, however, because it includes double quotes. Another example of a string could be “1234”, or “123abc”. As you can see, a string can contain letters, numbers, or a mix of both. For your first variable let’s call it my_first_var and make it a string. Notice that we use underscores to attach the words in our first variable ‘my’, ’first,’ and ‘var’. This is because no programming language can read spaces. When declaring variables spaces are a problem and are going to be dealt with through an error message. You will also have noticed that instead of writing my_first_variable we wrote my_first_var That is because ‘var’ is short for Variable, so we can use those two interchangeably and still know what we’re talking about. In all programming languages to define what a variable is equal to, what it is representing, you must use the equals (=) sign with your number or string on the other side of the equal sign. For our purposes, we will define my_first_var to “Hello, World! This is my first string in python!” To do this you will need to write the following code on a separate line from your Shebang statement:
my_first_var = “Hello, World! This is my first string in python!”
Congratulations! You have no created your first string with Python! Notice that we used my_first_var­ concatenated with underscores, an equal sign to show what the variable was equal to or representing, and double quotation marks to encase our string.

It is always a good idea to run your programs periodically so that you can be sure that none of the changes you’ve made or additions you have added in are going to make the program fail. Now would be a great time to run your new program and make sure that you have gotten all your Python code correct. To do this push the ‘F5’ key located above the number ‘5’ key. If you haven’t saved your program yet, Python will ask you to save your program before continuing. Save your program to an easy-to-remember spot such as your Desktop, and then run your program again. When you get around to running your new program nothing should appear in the new window that pops up. This is because we have not told the program to display, or ‘print’, anything to the screen. If something did appear to the screen, there is a problem with your program and you should go back and make sure it is all typed in correctly.

Our next step in our programming journey is going to be to print our newly defined variable my_first_var to the screen. Printing our variable to the screen will not print “my_first_var”; rather, printing my_first_var will print “Hello, World! This is my first string in python!” because my_first_var is just a placeholder for the string it is assigned to through the equals sign. To go about printing your variable to the screen you must use the ‘print’ statement. Create a new line under the line where you define my_first_var and type in the following code:
print my_first_var
Save your program and run it again by pushing the F5 button. You should see
“Hello, World! This is my first string in python!” appear in the command line window that pops up onto your screen.

Congratulations, you have successfully completed your first program using python! Not only have you learned how to start Python programs, but you have also learned how to create and print variables containing strings and numbers. You have also learned more about how to use your computer by installing programs and creating shortcuts to virtually anywhere on your computer.