Basic Of Python
Today, Python has most demand in IT industry. And everybody want to learn and make career in Python.
If you have started learning python, then this is for you. In this article i will tell you some basics things of Python.
Python Official Website: https://www.python.org/
Basic usable IDE for Python
- Anaconda — https://www.anaconda.com/products/individual
- Jupyter Notebook — https://jupyter.org/
- Spyder — https://www.spyder-ide.org
- IDLE — https://www.python.org/downloads/
- Pycharm — https://www.jetbrains.com/pycharm/
- Visual Studio Code — https://visualstudio.microsoft.com/
- Atom — https://atom.io/
If you have started learning Python, i will suggest you to go with Visual Studio Code. But if you are a machine learning developer and need to research using Python, go with Jupyter Notebook or Spyder. You can also install Jupyter Notebook, Spyder and Visual Studio Code from Anaconda IDE.
How to Start
Like every programming language (C or Java), there is few things, you need to learn in Python also.
- How to install
- Check if Python is installed and working
- Set Environment variable to access Python
- Basic Python Command and How you can execute your code
- Hello World
- Understand some basic statement for coding
- Conditional Statements
How To Install
Like every software you have to download an IDE (I will recommend to use Visual Studio Code, if you are starting). Go with above given URL and Download your IDE. Some IDE need to install Python Separately, that you can download from https://www.python.org/downloads/.
While downloading, If you see an option to add Python to Environment Variable, Choose it, so you no need to setup Environment Variable separatly for your Python and You can access Python from anywhere in your system using console.
Check if Python is installed and working
To check, If your Python is installed, You need to Open Command Prompt (In Windows) or Terminal (In Linux or Mac) and Type python — version (version with double dash). If you have installed Python third version then you need to type python3 — version, to check python version. So try both comamnd if getting error at any of them.
Set Environment variable to access Python
If you have already installed Python, but above given command returns blank, you need to set Environment variable for Python in your PC.
For that in windows, right click on THE PC or MY COMPUTER, then go to Advance System Settings. It will open a System Properties window. Here select Advance Tab (if not selected already), and click on Environment Variables button. IT will open an another window, where in System variable section, search and select Path and click on Edit. It will open another window with name Edit environment variable. Click on New button and add Python and Python Script folder in list.
As per my PC, this is my Python path:
Same you can do for setup JDK environment variable, if learning Java. Just you need to add JDK’s bin path here.
Basic Python Command and How you can execute your code
Start with basic, no need to make Iron Man Suit now.
Console is right tool to start. Open Command Prompt (Terminal). enter python and press Enter key. In next second, you will be in python environment. It will look like below image,
now try to write basic command, like add two numbers. Don’t think about programming. Just go with basic.
To print something in console, you can use print() function and To enter input from keyboard, you can use input() function. See in below image.
Now, console is not a good way for write program, if you are a developer. So we will learn our next step using any IDE. In my case, i will use Visual Studio Code. It is most used IDE today.
Now, you know what is Hello World. everyones first program. So,good way to start learning code is to save file somewhere with meaningful name. Create a folder in your drive, and open Visual studio code on that folder. In current Windows OS, you can find Visual studio Code at right click on blank space. Or enter cmd in address bar of the folder and when Command Prompt will open on that folder, enter code . (dot) to open Visual Studio Code.
In linux terminal, You can normally open Visual Studio Code, by entering code . or sudo code . — user-data-dir.
Next, when editor will open, create a file with extension .py. In my case, i have created helloworld.py. You can name anything you want. And write a normal statement in that file. As here, i have written print(“Hello World”). Save the file and open terminal from main menu (or you can also use your console).
In terminal, write command python, then space and then name of the file with .py extension. Here, python helloworld.py.
Now, try to write code with the right way using function, check the below image.
Now, try with __name__.
Understand some basic statement for coding
When you are writting code, you need to understand basic of coding, like how to print output, what to print in output, how to get input from keyboard, how to store and print input data, what is fuction and how to use functions. We will touch fucntions later, so now talk about Input, Output.
In python, you can use print() function to print any text in console. In real project, print() is just use for print debugging messages in console. Now, just go with console programming, but not real project.
In above image. I have printed Hello World in many ways. print(“Hello World”) or print(“Hello”, “World”) or print(“Hello”, “World”, 3). Print statement can contains multiple arguments, with different data types. Like in statement, print(“Nice to meet you”, name) or print(name), you can also print whatever variable contains. But in below two statement is not printing console but just saying what i have entered. It will only work on console, not in file. If you will not write print() in python file, It can’t print message, as given below.
Now, same code is printing message after using print() in below image.
You can also print output in multiple line, using three single quotes as below given example.
You can also enter keyboard input, using input() statement and store whatever you have entered in a variable like below image.
In above example, i have written, print(“Nice to meet you, “, name). name variable after a space, why?
You can concatinate two string using + sign. If i will not provided space after comma (,), name should look like this and attached with comma (,Nitish). Same in input statement, so i can write my name separate with space. Try without giving space.
There is no datatype issue in python to declare a variable.
Declare a variable: f = 0
Redeclare same variable: f = “abc”, you will not get error.
for decalring a variable you no need to write datatypes like int f =0 or string f = “abc”. Python can manage their memory itself.
Yes, you can’t concatinate string with integer type. Like print(“My age is “, 33). You will get TypeError, that, string can only concatinate string. So same statement you can write like this, print(“My age is “ + str(age)), if age = 33.
- You can cast string to int using int(“33”)
- You can cast int to string using str(33) and same for float and others
You can delete a variable from memory, using del statement. like del age. After this statement, if you will try to read age again, you will face error.
Operator in Python is same as C or Java. Just, as i know, there is no increament/decreament operator. Instead of ++ or — you can use x = x+1 or x=+1 and same for subtract with minus sign (Same as arithematic operator).
And for Ternary operator (condition?true: false), you can use single line conditional statement. Will will look at this on next section.
Local variable: Can’s access outside boundry
Global Variable: Can be access anywhere
A function in Python can be declared by def statement. It may have arguments or may not, It can return value or may not. So we will go with all.
‘’’ : three single quotes used for multiple comment. You can also print something on print() on multiple line as above see.
# : is used for single line comment in python
On the above example,
- def is used to declare function
- funct1 is name of function
- a and b is two parameter, where b has default value 1. You can use default value if there is a chance for user not mendatory to enter value for some argument.
- return a+b is return statement to return sum of a and b
Returning Multiple Value:
In Python you can also return multiple value from a function. You just need to write those variable with return statement and while calling you also need to call variable for all of those (as in my example there is two variable a and b in line 12 and 19).
Function with number of arguments:
You can use * to define multiple arguments using one variable, as exmple in image.
Above example with key:
You can also use above example with keys and call them in function with key name. You just need to use ** (double astrik).
In Python, you can write condition using if else statement. There is two types of if else statement, multiline or single line.
Same code in single line
Loop is used to iterate some statement multiple times based on given condition, so you no need to write same statement multiple times. Even it is very difficult when you don’t know how much times you need to iterate your statements. Think like reading data from database, where you do not have idea, how much row that table/ collections has contains. Might be in your company, you have 500 employees, in another company they have different number of employees, you can’t write static code. In that case we can use loop.
There is many types of loops in programming languages, like while, do-while, for, foreach. But, Python has only two types of loop, i.e. while and for loop.
Example of while:
Example of For Loop:
For loop you can use to iterate list, set, dict etc.
Range statement you can use to execute numbers, between some range. It contains three integer arguments start, stop and step, where step has default value 1.
range(start, stop, step=1)
So, if your statement is range(1, 10), then output will be 1,2,3,4,5,6,7,8,9 (before 10). If your statement is range(1,10,2), then output will be 1,3,5,7,9.
If you want to write range in reverse order, write range(10,0,-1). Here output will be 10 to 1 in reverse order. Just manage the step value and check.
Try this with different values and check what’s happening.
As java and C, in Python also break keyword is used to break the running loop, if given condition becomes true.
continue keyword is used to pass running step to next step of the loop, when condition becomes true.
Substring of array
You can think about any types of array, string as character array, list, dict etc. In Python there is easy way to substring a list or string, using [:]. Logic is same as range.
str = “this is a string”
print( str[0:4] ) # outputs ‘this’
print( str[0:4:2] ) # outputs ‘ti’
a = [1,2,3,4,5,6]
print( a[0:4] ) # outputs [1,2,3,4]
print( a[1:4] ) # outputs [2,3,4]
print( a[6:0:-1] ) # outputs [6,5,4,3,2]
Try as much you can.
Class is a big thing to understand, till you will not understand why and where and how to use this. I will write details on class in python in another article. Here is just an small example of class in python.
Above is a basic example of class. You can initialize an object, like i have done in line number 8 and call class members using that object as i have done in line number 9.
As you can see, in line number 3, i have passed one argument self. Always remember, first argument of class functions is always its instance. same in line 5 i have called continue_contrl variable with self. Without self you can’t access class member.
self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments.
The reason you need to use self. is because Python does not use the @ syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method belongs be passed automatically, but not received automatically: the first parameter of methods is the instance the method is called on.
Self is always pointing to Current Object.
We will talk about class in another article. If you think anything wrong here, or i need to describe more, you can comment here. But this article i only have written to understande basic. After this you can find details in many Python ebooks, so you can understand easily.
(Sorry for grammatical mistakes)