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 :

python


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:
3
>>>

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:
3
>>> c = b/a
>>> print(c)
0.3
>>>

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
0.3
>>>

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.
    cd<space>Location_of_the_Folder_in_your_system.
    example : cd Desktop
  2. To execute the script just type the following command.
    python<space>FileName.py
    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.


<< PREVIOUS CHAPTER || NEXT CHAPTER >> --INDEX--


~*~*~*~

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 :

Review: Tales of Demons and Gods



Tales of Demons and Gods by Mad Snail
My rating: 5 of 5 stars
Link : https://www.wuxiaworld.com/novel/tales-of-demons-and-gods#


Book Description : 

Killed by a Sage Emperor and reborn as his 13 year old self, Nie Li was given a second chance at life. A second chance to change everything, save his loved ones and his beloved city. He shall once again battle with the Sage Emperor to avenge his death. With the vast knowledge he accumulated in his previous life, he shall have a new starting point. Although he started as the weakest, without a doubt, he will climb the steps towards the strongest.

Cultivating the strongest cultivation technique, wielding the strongest demon spirits, he shall reach the pinnacle of Martial Arts. Enmities of the past will be settled in this new lifetime.

“Since I’m back, then in this lifetime, I shall become the King of Gods that dominates everything. Let everything else tremble beneath my feet!”  


Note :  I read this book on Wuxiaworld, hence if the original book source differs, feel free to comment down and it'd update the information.

Let me begin this with a piece of warning. This book already has 485 chapters and possibly more if someone reads this review later. It's an ongoing book on Wuxiaworld with a new chapter published every month. So that being only major con, if you can let that 'ongoing' tag go and enjoy the book's already uploaded chapters then this review will be worth reading for you.

So, I was just randomly looking for some new good stuff to watch and stumbled upon anime version of this series. The anime was of 5 mins each, like seriously?! Phew! Anyway, I still enjoyed the anime so much I decided to take a look if it was based on some book. So guess what? I googled and stumbled upon link to the goodness of the textual realm. ( I am someone who prefers books over movies :P ). Anyway, I decided to get reading from where the story had stopped in anime and damn! I enjoyed this one so much I decided to give it a review even when this one is an ongoing book and the chapter rate is horrible at one chap per month. ( If you know me, you'd know how much of a big deal it is for me to get reading onto such a book.) Well, to be fair enough, 485 chapters are a good enough amount to read hence, I started reading.

Where do I begin with? If I want to summarise it in one line then you can say it is like a 'Rajnikanth' movie story. The character building, world building is super complex and amazing. I loved the new concepts. Even after being a fantasy, everything was really well built and everything had a really good logical reasoning. It was really hard to find issues in the plot unless I really start being a crappy person and nit picked on everything. I'd say just give this book a try. Only huge con being you'd have to wait very long for the updates.

The plot is fantasy based with a lot of teen fiction, gamer, spiritual influence over it. It's most probably adventure based plotline in terms of how the story progresses. I won't reveal much about the story and I'd just recommend reading it. I wish the anime was much longer in terms of duration and was released faster. I can only hope though because even though I read this, I'd really want to see the visualisations. I'd recommend watching those episodes before watching as it'll give you an actual idea of how things are in this universe as the descriptions aren't really strong point of this story.

One more thing that I noticed was that the author was probably getting stagnant with the plot ideas as everything felt recursive. Although there were minor changes here and there but the major plotline after 300 chapters started to feel the same. Remember, I said 'same' not 'boring'. Well, I ended up reading all 485 chapters in less than 3 days and it was a great read. Especially when there's quarantine emphasis all around you.

Link : https://www.wuxiaworld.com/novel/tales-of-demons-and-gods#

Note : If you want to download and read in your kindle, here's the link. I don't support rampant piracy but well you should know what should be done and what shouldn't be done. So here's the link : https://www.asianovel.com/series/tales-of-demons-and-gods




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 :