What you should know?
Before taking this course you should be familiar with basics of programming like what is variables, functions etc. If you have previous experience with any programming language like java, c++, javascript or even basics of PHP you are good to go.
Python Installation
Before going to the basics of python, first, check if python is installed on your system, if not we will first install python.
Note: In mac and Linux you likely have python already installed.
Check if python is installed
Open the terminal and type
1 |
>>python –version. |
If you get an error that means you have to install python on your computer.
Download Python
Note: 2.7 version is the original version of python and 3 version is a newer upgraded version of python.
Download python and run the setup file. Once the python is installed you are ready to try your first python program.
Basic Hello World
Following the trend, I will start with hello-world programme. Let’s do it on the command line first.
Open the terminal and run the following code
1 |
>>python |
You will come inside python interpreter. Then type print command
1 |
print("Hello World") |
Note: Python is interpreted language like javascript. It is not like c,c++ or java that means the python need not be compiled before running. Python interpreter takes each line of code interpreters and executes it. For example, if you type 2+2 in python interpreter it will execute.
To come out of python interpreter type
1 |
exit() |
Python Code inside a file
We will write same code inside a file instead of the command line. Let us create a file named hello.py
1 |
print("hello world") |
Save
Go to cmd > directory > python hello.py
The program runs and prints the Hello World.
Let’s do this by defining a function
1 2 3 4 |
Def main(): print("hello world") If __name__ == “__main__”: main() |
Note: This line of code means that python programme is executed as the main programme.
Save this and run. Python works differently from other languages in terms of writing the code it doesn’t use any braces for the code. It uses indentation for determining a particular block of code.
Note: I will discuss more python function in later sections