Learn Python With Dhawal - 18 - Interacting with Python on terminal

In this chapter we will learn to interact with python as it actually is, in its default form to understand how the language functions at a basic level. Since we are already quite familiar with Jupyter and writing basic level codes right now. We have to get a deeper understanding of how our commands actually are executed before we move ahead and start working on much deeper concepts.

1. Getting Started :

1. 1 Open Python Terminal :

  1. If you already know how to open terminal or command prompt then you can skip to next part.
  2. First way to open python terminal ( for anaconda python ) is to search in your windows for anaconda prompt. Clicking on it, it'll open a terminal for you.
  3. Another way to access the terminal is to open Anaconda Navigator -> Command Shell
  4. Another way would be simply opening up a terminal ( this method will work only if you are working with Ubuntu which has a default python installed already, or if you have added your python path into system path variables )
1.2 Start with Python :

Once you've opened up your choice of terminal/s just try this command to check if the installation works as expected.

Input :

python --version
Output :

Python 3.7.6

Next, as we move ahead, you can try a single print statement as we had done in the first exercise. But before that we have to start Python so that we can begin interacting with python in the terminal, in its native format.

Input :


Output :

Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.

Note : >>> denotes the line where you have to give input. Pressing enter will show you the output to get executed in python.

Input :

print("hello world!")

Output :

hello world!

Try yourself : 

Try using it like a calculator and give it inputs like 2+5, 5*4, 4/2, etc. and see what's the output. Does it give any error? If yes, try finding out why? and if no, know the reason why it works the way it does?

1.3 Taking Input into variables 

Just like the usual method, you can either declare a variable with a value or take input from the user. We will learn both from the example below.

Code :

>>> a=10
>>> b=input("Enter a value to be divided by 10")
Enter a value to be divided by 102

As you can see from the example above, a=10 makes a variable a with value 10. Next when you take input into b, the statement appears as prompt which you've written and then a cursor blinks next to it without a space. For the proper format to get input you'd need to format the statement properly like shown below.

>>> b = input("Enter a value to be divided by 10:\n")
Enter a value to be divided by 10:

Next try dividing a with b and store the result in a newly created variable c, see what you get.

>>> c = b/a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'int'

This is because you need to convert first the input into the int as shown in the example below.

>>> b = int(input("Enter a value to be divided by 10:\n"))
Enter a value to be divided by 10:
>>> c = b/a
>>> print(c)

If you noticed, you won't get output like Jupyter-notebook when after operation you'd get a result but here you'd have to print to see the output.

>>> c

If you just type the variable name like this then too you can get output. But this only works in the terminal as a single step input not in the python programs with a lot of collective statements.

2. Diving Deep Into Functional Programming.

1. Creating Functions :

Before you read ahead, I'd like you to give creating function a try yourself.  Once you have 

>>> def myfunc():
...     print("Hello world!")
>>> myfunc()
Hello world!
>>> myfunc
<function myfunc at 0x000001E8B62E8168>

Here as you can see, you need to put 4 spaces as tab to have the indentation which you were getting by default inside the jupyter notebook. Apart from that if you just write function name, you'd find the location of the function in the memory space. Try out experimenting with the functions as you like.

2. Write a Program and execute it from terminal.

Now, I assume you'd be quite familiar with the terminal and how to write code using it. Next I'd like you to learn is that how to make a python script and execute it from terminal.

1. Write a Python Script

  1. Open a Notepad, Notepad++,Gedit, Nano, Vim, or IDLE if you're using random python distribution or if you are fancy enough to use IDE like Visual Studio Code, PyCharm or even Spyder ( comes within Anaconda distribution ) then surely you can but that would just go against of our purpose of running a script from terminal. But I'd recommend sticking with Notepad, or any other such simple text editor for the beginning.
  2. Create a new file and save it with the extension '.py'. It means it's not a normal text file but rather a python script.
  3. Enter the code ( basically whatever program that you want to run ) and save and exit.
2. Executing the Python Script

Here, it gets a bit tricky as there are various types of shells and various configuring ways to run a python code. Navigating to the folder is a huge task here therefore I'd stick to a very direct and layman approach. You can explore around and gather information from the internet to learn more on how to use different shells.

  1. Open anaconda powershell prompt or whatever terminal you're using python with. Then use the command below to navigate to the folder where your file is located.
    example : cd Desktop
  2. To execute the script just type the following command.
    example : python Helloworld.py
3. Extra tips in navigating to folder.

Since I have used Linux ( ubuntu) and Windows, I can guide a shortcut way to do the same thing, but do these shortcuts with a pinch of salt. Google and confirm before you execute any shell commands.

  1. Go to the folder using file explorer and press shift and right click ( for windows) or just right click ( for ubuntu ) to get a menu. In that menu look for any option which says open terminal here or  open powershell prompt here click on it to open a new window there.
  2. Try running the python --version  command first to see if python works in that shell.
  3. If it doesn't work and If you're running Anaconda Distribution of Python use command 'conda init' followed by the name of that shell. It'll be powershell or terminal for windows depending on what you're using or bash for linux. ( if you have added it to the path )
  4. If you want to navigate to a folder to execute a file then you'd need to cd to that address. you can copy the folder address from the location bar in the explorer and then just paste it with cd command in the terminal window.

With this Now I hope you understand why we always used jupyter notebook or even things like other IDE to execute things in a simpler manner. These things would just waste time and add complexity to the procedure and get in the way of actual coding.

This was very important as from next chapter onwards I'd be working on Visual Studio Code. As in Next chapter we'd learn to use packages, create our own package and a few other things before we move on to the UI creation part of the system.



This series is totally authored by me (Dhawal Joshi). Any similarities found on the text, or codes or anything is purely accidental. All the sources of reference will be mentioned, linked and will be given the proper credits. If I miss anything or there's anything wrong, feel free to comment or send me an email and I'll try to edit it out. I am not a Python expert, I am sharing whatever I have learnt on my own and with a few sources around to refer from which will be mentioned. Also feel free to share this series with others so most can benefit out of it.


Do comment and share your thoughts about it! I'd love to know what do you think. Also, I'd keep updating it quite often so do follow the Website to get all the updates by clicking here.

Also, a minor headsup.... Obsessed is free to read on Kindle Unlimited! Do check it out. I'd be glad to read your reviews!
Share :

Are you getting regular fresh content updates? If not click on the button below.


Post a Comment

Do comment and share your thoughts.