DEV Community

Cover image for My first Python script running on Windows
Hannu-Daniel Goiss
Hannu-Daniel Goiss

Posted on

My first Python script running on Windows

I don't have anything Python-related yet on my computer, so I'll start with the complete basics.

Let's go to python.org to download the windows installer of the latest release (https://www.python.org/downloads/release/python-3102/)

I'm installing the standard installation and adding Python to the PATH.
Python Windows Installer
I'm now trying to access python from PowerShell to verify it's accessible from anywhere.

PS C:\Users\hgois> py
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
Enter fullscreen mode Exit fullscreen mode

My next step is to access the tutorial and skim through some parts to get a glimpse - The Python Tutorial — Python 3.10.2 documentation
What I can see (with Java/C experience) is:
there are no brackets {} - the body of control flow objects (like while, if for,...) is indented.
There is no “:” at the end of each line.
Stuff looks familiar, so it should be quite easy.

I can also see that I can just declare variables and write functions using the “python command”. So I'm trying to declare two Strings and to use the print function.

PS C:\python> python
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> name = 'Hannu-Daniel'
>>> city = 'Abu Dhabi'
>>> print (name + ' lives in ' + city)
Hannu-Daniel lives in Abu Dhabi
>>> print ('%s lives in %s' %(name, city) )
Hannu-Daniel lives in Abu Dhabi
Enter fullscreen mode Exit fullscreen mode

In the above example, we can see two ways of combining the strings name and city with 'lives in'.
1) concatenating with + (this should be familiar for any programmer)
2) using the % operator and String formatting. (this is familiar for C programmers)

Let's try to create a first Python script, where we prompt the user for input and react to it.
The script will simply repeat the response and ask for another input until “quit” is entered.

First I'll look at control flow structures (More Control Flow Tools — Python 3.10.2 documentation). At least at first glance I cannot see a “do...while” contract similar as in C. Hence I will break the loop with an if.

I will save the following script as firstsample.py

print("Hello! We are just here go greet each other!")

while (True) :  # boolean True with capital letter
   yourmessage = input('What is your name? (or type "quit" to exit): ')
   if(yourmessage == 'quit') : # break the while-loop, if the user typed "quit"
      break
   print('Hello %s! Nice to meet you!' %yourmessage)
Enter fullscreen mode Exit fullscreen mode

I will now start this using python and process the user input:

PS C:\python> python firstsample.py
Hello! We are just here go greet each other!
What is your name? (or type "quit" to exit): Hannu-Daniel
Hello Hannu-Daniel! Nice to meet you!
What is your name? (or type "quit" to exit): Goiss
Hello Goiss! Nice to meet you!
What is your name? (or type "quit" to exit): quit
PS C:\python>
Enter fullscreen mode Exit fullscreen mode

Success! First Python script is DONE!

Top comments (0)