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.


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

python-path-in-windows

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.


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


WHAT IS IDLE ?
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.


Python-folder-in-windows-start-menu
Once IDLE is launched you will be greeted by the following screen. Something similar to the "Python IDLE" image below –
Python-IDLE-editor-in-windows


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.
Python-prompt-in-linux


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.


Endnotes
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...
Quiz
  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.


Comments

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 y...

While Loop in python : How to become a Python Jedi - Beginners Python Programming Tutorial - Part 6

Welcome back friends to another post on TheCodingProject and we are back with the sixth chapter of the Beginner Python programming tutorial - How to become a python Jedi. This time we are going to learn how to use while loop in python. The While Loop statement? In Python Programming, There are 3 types of loops → While loop For loop Nested loops Now to get into some action→ Let’s consider that two groups of aliens have landed on earth  out of these two one of the group is good & the other are the bad guys. The govt. Has given you the task of designing a system that takes in input from different eyewitnesses & then classify that whether the aliens that these eyewitnesses encounter are the good guys or bad guys. Copy & paste the above code in your editor and run this code. What do you get ? When the input is Autobots it matches the condition aliens == "Autobots" & it enters the loop to prints the statement pointed b...

For Loop in python 3 : How to become a Python Jedi - Beginners Python Programming Tutorial - Part 7

I am back again with another part in the beginner python programming tutorial series. We will continue with "Loops" but this time we are going to learn how to use for loop in python 3.  Difference between For loop and While Loop A "For Loop" and "While Loop" have a common connection that both are looping statement, but major difference is that unlike while loop, a for loop gets executed a specified number of times. We can break down the For loop into the following components(Refer to the for loop flow chart below). for keyword variable name call to the range function colon operator clause or block of code Hovering over the flow-chart The above flow-chart is..well horrible I know, but I make my own drawings because I can't afford an artist. So, bear with me. In the above flowchart is a very basicy, laymanish representation of a for loop. The decision box contains the  For  statement along-with it's sou...