Skip to main content

Expressions in Python : How to become a Python programming Jedi - A Python programming Tutorial Part 1

Long Long ago in a Galaxy far far away there was a planet named Quora. There was a growing agitation among the planet’s inhabitants to learn the way of the Python programming Force and become a master in the way of the Python programming Jedi, but very few knew that the secret art of the Jedi is embedded in a book named as TheCodingProject….

So, let’s begin the journey of learning the secret art of the Python programming Force and we shall together make the journey towards becoming the Python Jedi….

What is Python?
As per Wikipedia “Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.” Python is an open source programming language which was conceived in the late 1980s and its implementation began in December 1989 by Guido van Rossum (more on it here). For us it’s sufficient to know that Python is :

  • High level language - Python is a high-level programming language because it provides strong “:abstraction” in the sense that it automates the tasks like “memory management” , “garbage collection” etc. so that the programmer does not have to handle that explicitly while coding which makes the life of lazy programmer like me a simple affair. More on :high-level programming language here.

  • Interpreted - Python is an Interpreted programming language because it doesn't runs off a compiler rather it has to be processed further by another code so that the coding that we have done is understood by the computer. So, such programming language can be written using “natural language” which again helps people like me to spend less time in writing code.

  • Dynamically typed language - Python is dynamically typed because in python every variable is bound to an object (more on Objects later) and every object has to be assigned to a Type (we will dive into what “Types” are later).

How do I install Python?
Installation of Python is very straightforward. The Python installer and the installation instruction can be found here. Now that Python is installed on our system and since we are the supergeeks that we are, we don't want to waste time and dive straight onto typing our first Python code. But in order to run the python interpreter from the command line or executing a python file from command line our machine should know the folder where python is installed.

  • We have to visit the following path. Refer to the "python path in windows" image below-


Under system variables we create a variable “PythonPath” and enter the folder name where python is installed and click on ok button. For me it’s C:\Python34, your directory path may vary.

In Linux python is installed by default (perks of using Linux 😉. So, congratulation Linux users you don't have to worry setting pythonpath.

IDLE is an “Integrated Development Environment” for Python, which is bundled with every Python distribution.  It is completely written in Python and the Tkinter GUI toolkit

How do I do it in windows?
In windows goto the start menu → All programs → search for the folder where python is installed (for me it's Python 3.4 ) → click on the folder → click on the IDLE (Python 3.4 GUI – 32 bit), this should look like something similar to the "Python folder in windows start menu" image below.

Once IDLE is launched you will be greeted by the following screen. Something similar to the "Python IDLE" image below –

How do I do it in Unix?
Did you forget what I said above? Linux users get Python by default 😎 In Unix just open the terminal and type ”python” for python 2.X or python3 for 3.x and you will be greeted by the python prompt and I tell you python prompt in Linux is as good as IDLE for windows. Take a look at the "command prompt" image below.

What should I do with the python Interpreter?
Now that we are armed with the knowledge to invoke the python interpreter, what the interpreter is used for? Well the first thing that we can do what computers do best, yes number crunching but before we do calculation we should know some programming terms.
Expressions - In programming an expression is an way of defining things to the computer so that it understands what we are commanding it to do. Some examples of expressions are when we want to do addition of two numbers say “2+2”, this way of adding “2” with “2” is what we call as an expression.
Operators - We can call operators as the agent which enable us to make an expression. In the above example of an expression “2+2” the “+” symbol is an operator. Some examples of expressions are -
  1. + # Addition
  2. - # Substraction
  3. * # Multiplication
  4. /  # Division
Now enough talk let’s get to action. Fire up the interpreter as mentioned in this section and follow the below examples-
  1. >>> 2+2
  2. 4
  3. >>> 2 * (5-6)
  4. -2
  5. >>> 100 * (20-10)
  6. 1000
  7. >>> 238/5
  8. 47.6
  9. >>> 3444 * (456/43)
  10. 36522.41860465116
  11. >>> 2**2                       # using ** tells python to calculate the power 4
Now look at this ↓
Suppose there is a party for 4 people and you want to order beer cans for the guests , each beer can I $21 ( I don’t drink beer so cut me some slack on the price) and u want to calculate total price of your purchase and since you are a geek we don't use normal calculators or smartphones like mere mortals instead we command python code to do something like this-
  1. >>> beer_price = 21
  2. >>> guests = 4
  3. >>> total_price = beer_price * guests
  4. >>> total_price
  5. 84

Okay , What did you do just now?
Let’s break that into more granular level. When we type “beer_price = 21” we are assigning the value 21 to “beer_price” and here “beer_price “ is known as a variable.
A variable can be thought of as a container which has an item i.e. a number “21”. Let’s have another example to make it easy to understand.

  1. >>> android = "KitKat"  # a variable “android” which contains the name “KitKat”
  2. >>> android  # checking what is contained in the variable android
  3. 'KitKat'
  4. >>> android = "Lollipop"  # now we give another name to variable
  5. >>> android  # checking what is contained in the variable android  
  6. 'Lollypop'   # the content of the variable Is now changed
Initially the variable “android” contained the value “KitKat” but after that we assigned a different value to it and now the variable contains the value as “Lollypop” . In short “android” will contain the most recent value that we assign to it and that’s why it’s known as a variable since the information that it contains can vary again and again.

Now that we have learned about expressions so we shall gather up your jedi skills as we have to pass the following test which will test our worthiness to continue our journey further...
  1. Launch the python interpreter.
  2. Type and expression in the interpreter which will give the sum of the two numbers “200” & “300”.
  3. Use an expression to calculate the difference of “549” and “679” and multiply this difference with the sum of “999” & “790”.
  4. Use an expression to divide “9999” with “0” and assign the result to a variable.

....To Be Continued...

Subscribe to my youtube channel to watch new python programming tips and tutorials.

If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.


Popular posts from this blog

How To Install Tensorflow On Windows

Why I am writing this tutorial ?
Recently I have started learning machine learning specifically DeepLearning for one of my pet projects, and during this quest of mine I wanted to learn TensorFlow library which is developed by Google. Learning is one thing but first I needed to install the library, but there was this problem which I faced during the installation. I am using Anaconda as my package manager and trying to install the Tensorflow library on my work computer which is behind a proxy, installing directly from the anaconda cloud while behind a proxy always displayed a proxy error.

How did I resolve the issue? Well this is exactly what I am going to write down in the below tutorial. The Steps To Follow
Before we get all cosy with this short tutorial on Tensorflow installation, let me tell you that
this tutorial is on the assumption that you have already installed Anaconda on your machine and you know at least a few basic things or two about python.
If you want to learn basic pyt…

Collatz sequence python : Monday fun with Python

Hello everyone. Today is Monday..well not a big deal and I know everyone knows today is a Monday. Like you I'm not a big fan of Monday, although nowadays I'm waiting eagerly for this day of the week as I get to watch a Game of Thrones episode on this day and I'm sure most of you can reciprocate the same feeling. The only thing I hate the most is the long wait till Monday night till I return to home from office and get to watch that episode for which I wait for one whole week and here I am in office waiting for the day to get over soon.

The plan To kill the time I do one of the thing that I love the most. Yes, you guessed right? I code. So, I decided to fool around some python programming code to kill the boredom. Without much ado let's code :) I decided to device a python program that would help me to devise a "collatz sequence" In case you are thinking what a "collatz sequence" is then dive into this link here.

For more understanding on python check…
Encountering The Python
What is Python? As per Wikipedia “Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.” Python is an open source programming language which was conceived in the late 1980s and its implementation began in December 1989 by Guido van Rossum (more on it here).
What kind of programing language?To give a better idea let's look at the below mentioned summary-
High level language- Python is a high-level language because it provides strong “abstraction”.Interpreted - Python is an Interpreted language because it does not run on a compiler.Dynamically typed language - Python is dynamically typed because in python every variable is bound to an object.Why should I learn Python?Python is a very powerful programming language, secondly it's easier to learn and since it's interpreted language the code is easier to understand. Lastly there is a huge community supporting Python so if you get stuck anywhere lot's of help are a…