Intro to Programming with Python – #1
October 21, 2018
This tutorial is a series of posts that will introduce you to the world of programming using Python as the means. We will be discussing what is programming? Why we do it? How we do it? and unrolling many other questions to reach the point of a descent understanding.
This course is for total beginners and is a part of giant free course I am building to go from a beginner to a Deep Learning expert.
WHAT IS COMPUTER PROGRAMMING?
Throughout your life you interact with a computer, a smartphone or any other smart device to complete basic tasks (well checking those instagram pics is also a task. May be!). All these tasks require computer to understand the input you give and what are the internal actions it needs to perform to give the desired output. Every task requires a set of rules and these set of rules are conveyed to computer using powerful yet human understandable languages called Programming Language. These languages are so powerful that they are used by companies like NASA, Google, Facebook to handle their ginormous tasks.
So, in simple terms Computer programming is all about giving computer step by step instructions to perform a certain task.
WHAT WILL WE NEED?
This course is an introductory course on programming with Python as the programming language used. So the basic requirements will be:
- A descent internet connections to stream videos – I don’t like to stick to one medium to learn anything and I certainly don’t want you do to it. So I’ll be linking various external sources (Video links, blog posts etc.) that gives you more in-depth command on Python and programming in general.
- Python 3 – I’ll be guiding you with installation in few minutes.
- Any descent text editor – I don’t like Advanced text editors which helps beginners and don’t let them learn the syntax on their own. Thus I prefer Notepad++ for beginners. Once acquainted with programming languages I’ll advice you to fiddle through multiple text editors and select one that makes your life easy. 🙂
INSTALLING PYTHON 3 ON WINDOWS
Go to download section of python.org and download any version >=3.5.. Prefer the executable installer. Make sure you choose x86 installer for 32-bit systems and x86-64 installer for 64-bit systems.
After download is complete run the executable file and make sure you select Add Python to path before installing python.
Allow the installer to do its work and after few minutes you hit the bullseye. To verify that python is running open a new command prompt using win + R and then write cmd in the popup and click OK. In the command prompt write command python –version and press enter, it will display the version of python you are using.
INSTALLING PYTHON 3 ON LINUX
Most Linux distros come with python 2 preinstalled. To install Python 3.6 on your Ubuntu 16.10 or newer (well for other distros the commands will differ slightly only) follow these commands.
- sudo apt-get update
- sudo apt-get install python3.6
To check if Python is installed successfully execute command python3 –version on a new terminal
FIRST PYTHON PROGRAM
Now as you have python installed we can go on and make our first python program. We will be using python’s print function to print something on screen. For some it might be like no this isn’t programming. But remember that many great coders started from this first step.
We will first start Python in interactive mode and see python in all its glory with a line by line check and execution. To start python in interactive mode open a terminal or command prompt on your linux or windows respectively. Write command python3 if you are on linux or python if you are on windows and press enter. You will be in the python’s interactive mode.
When you are in python interactive mode write code:
Press enter to see the result:
Considering that same output would have taken 4 to 5 lines in other programming languages like Java and C++ is what amazes most. For most programs written in Java and C++ you can have same in python in fewer lines of code.
This was all fine but working in interactive mode when you have same code to run more than once or you have code of more than 1 line doesn’t make sense. There comes python script files to rescue. Start a new window of your favorite text editor instance. Write the following code:
Now save the file as test.py, where .py is extension of python script. Now start up a new terminal or command prompt and then cd path\to\your\test.py. Remember to change your directory of terminal or cmd to the directory where test.py is present.
Now write command python3 test.py if you are on linux or python test.py if you are on windows and press enter to run the script and see the result:
Now that you have executed your first program let’s understand some bits about programming languages which will go long way in your programming career.
A token is the smallest possible element of a programming language. To understand this consider an English sentence – “Cat ate all the food.” This one sentence has noun, adjective, verb, connectors (if you go deep in grammar). These are the smallest possible units of the sentences which collectively bring the meaning out. Similarly, tokens in programming language collectively give instructions to the computer.
Every Programming language is usually made of 5 components:
Here I’ll give you a brief introduction to these and make sure to just brush them off quickly so that I don’t bore you to death!!
To bring meaning to special statements in a program, a programming language usually have reserved which cannot be used for any other task. These reserved words are known as keyword arguments.
Python has 33 keywords:
In programming we require various names to hold intermediate values when an operation is being performed. Consider this as we use x variable in mathematics. We use it to account for a particular value or value output by a particular function sometimes.
Python has some rules to make a valid identifier:
1. Identifiers are case sensitive. So, “piyush” is not same as “Piyush”.
2. Can be composed of any combination of A-Z, a-z, 0-9 and underscore ( _ ).
3. Cannot begin with numerical values.
|Some Valid Identifiers|
|Some Invalid Identifiers|
Constant values that are used to perform various calculations or operations are called literals.
1. Integer Literals: 123, -10
2. Floating Point Literals: 12.3, 1e3 (read as 1.0 X 10^3), -12.7
3. String Literals: ‘abc’, “abc”, ‘Piyush’, “Piyush”, ‘123’.
In some languages, like c/c++ and Java, string and character literals are treated differently. Single characters are represented as ‘1’, ‘a’ and so on and combination of characters (string) is treated as “123”, “abc”.
Notice usage of ' and "
These are nuts and bolts of any programming language. They let us perform various arithmetic and logical operations on data.
Type of operators in Python:
1. LOGICAL OPERATORS
- not – unary negation. not of any conditional statement whose answer is True will be False and vice-versa.
- and – conditional and. and of two True statements will be True otherwise False.
- or – conditional or. or of any two statements where either anyone is True or both are True is True else False.
The and and or operators short-circuit. In case of and it means that when statement 1 is False then there is no point in checking whether statement two is False or not as the result is False. In case of or it means that when statement 1 is True then there is no point in checking statement two as the result is True.
2. EQUALITY OPERATOR
Python supports the following operators to test the notion of equality:
- is – same identity. `a is b` will result in `True` if `a` and `b` are aliases to the same object.
- is not – different identity.
- == – equivalent. `a == b` is `True` if value of objects are same.
- != – not equivalent.
3. COMPARISON OPERATORS
Data type define a natural order via following operators: <, <=, >, >=
They have expected behavior for numerical types, and are defined lexicographically and case-sensitively for strings.
4. ARITHMETIC OPERATORS
- + : Arithmetic addition. (a + b)
- – : Arithmetic subtraction. (a – b)
- * : Arithmetic multiplication. (a * b)
- % : Modulus operator gives remainder of first operand divided by the other. (a % b)
- / : True Arithmetic division. (a / b gives a decimal value. 10 / 4 gives 2.5)
- // : Integer division. (a // b gives value of division removing all values after decimal. 10 // 4 gives 2)
There are many other operators which can be included here but aren’t and will be disclosed as use arises.
This brings us to the end of this post. But don’t worry we aren’t even scratching the tip of the iceberg. I recommend doing following experiments before going to next post:
- Give `a` value as 10 and `b` value as 5 and try to print there sum, difference, multiplication, true division, integer division each on a new line. (Hint: print(a) prints value of stored in a and moves cursor in next line).
- Give `a` value as 10 and `b` value as 5 and try to print value of a > b, a == b, a >= b, a != b each on new line.