Here is the Tutorial to write your first program in python, well this is lesson 3, as far as our tutorial goes, we number it, and make it lesson wise, so it will be easy for readers and visitors, and I hope you didn’t miss our lesson 2: Programming in Python: Setting up the Environment and lesson 1: Programming in Python: Introduction
This is a very short lesson to demonstrate how to enter and run a python program. We will print a simple line of text (Yes, it is the traditional “Hello, World” ) to the console window.
Let’s begin. and before that make sure you read lesson 2: to set up the python app in your pc
Open up your command prompt. By default it will take you to C:Users<username> subdirectory.
We need to go to the Python sub-directory. So, type cd python33
Hit enter and it takes to the Python sub-directory
now type the following and hit enter:
notepad helloworld.py
This will open up the Notepad text editor.
Note that the extension .py is very important. Do not forget to include it because it is the only way Python interpreter knows that the file we are trying to run is indeed a python program.
To print “Hello, World” to the screen, type exactly the following
print(‘Hello, World’)
What print() does is that it prints whatever is inside the parentheses. Basically it is a Function. Any function in Python is always followed by a parentheses. We will go into the details of Function later.
We will use this print() function a lot and even if you don’t understand anything now, don’t worry, you get comfortable with it gradually.
That’s all we need now. Save and Exit Notepad and we are back to the command prompt window.
Now, to run the program, enter the following:
python helloworld.py
Hit enter and it displays the phrase Hello, World
Alternatively, we can also type only the filename along with the extension like this:
helloworld.py
and it works. This is because the extension .py is automatically recognized as Python program by Operating Systems.
You may have noticed that the file helloworld.py is located inside the C:Python33 directory. Any file that we create while inside this directory will be located within it.
You can also create and save python files in any location of your computer and run the programs from there. I suggest you create one folder within the Documents directory and save all the python programs of this tutorial there.
To do this type the following in the command prompt:
cd users<username>documents (replace the <username> with your username)
md PythonExercise
cd PythonExercise
Now you can repeat the earlier steps for creating a python file and then running it from command line. You don’t need to provide any extra command to run the programs just because you changed the directory. This is because the link between the subdirectory we created and the Python Interpreter is in our system path.
So this is the end of this chapter. See you in the next tutorial that will be lesson 4!!
Further Reading:
- See the official documentation of running Python scripts on Windows