Programming in Python: Writing Your First Program

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 missed our lesson 2Programming in Python: Setting up the Environment and lesson 1Programming in Python: Introduction

Chapter 3
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 setup 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

take1

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

take3

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 sub directory 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

Last Updated on by Vamsee Angadala

Get Your Daily Dose of Tech Delivered To Your E-Mail!

Get updates on all the latest news, offers and more. Subscribe to our newsletter now!

By subscribing, you agree to our Privacy Policy.

Latest Stories

Q Mic – Designed for ASMR, Amateur & Professional...

With the rise of ASMR & other content types, recording high-quality sound is quite necessary. One particular problem content creators face having different mics...

Fossil sport review – Bringing out the fitness beast...

Fossil, one of my favorite watch manufacturers came up with a new smartwatch, Fossil Sport, recently. Fossil is not new to Google wear OS...

EasySMX VIP002D Review

Recently, we tried quite a few gaming accessories and a cheap gaming headset was among them. While I quite liked the product, in terms...

ROG Phone II Hands-on Review – My First Impressions...

ROG Phone II is finally official now. Yet again, the leaks have come true, and boy, the device is lit AF. I was invited...

Related Stories

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

5,550FansLike
851FollowersFollow
381FollowersFollow
9FollowersFollow
1,262FollowersFollow
547SubscribersSubscribe