Introduction to Python Programming

Python is a powerful programming language and a buzzword in today’s programming world. Python was introduced by Guido Van Rossum.
Python is so easy to learn that a kid who understand English well can write programs in Python after little guidance. File extension for Python programming language is .py

What type of applications can be developed in Python ?

Python can be used to develop numerous kind of application. One can develop a web application, windows based application, One can develop application related to Artificial Intelligence (AI) and Machine Learning.

Benefits of Python

Python is a platform independent programming language i.e. Python can work on Windows, Mac, Linux etc.

Python syntaxes are very easy and looks like an English sentences, that’s the reason it is said that small kids who can write and understand English can learn Python.

Get Started with Python:

To write code in Python, what software/IDE, environment is required.

Very first step is to download Python compiler.
You can download the same either from https://www.python.org/

Or from https://www.microsoft.com/store/apps/9NJ46SX7X90P?ocid=badge

It is not a difficult installation process.

Once installation is done, you can check the version on Python from command prompt using below command.

Py –version

Or

Python –version

In my case output is Python 3.7.3

In your case, it depends which version you are installing.

First Python Program

Once the installation/setup process is done. You may start writing code in Python.

To write Python code – Open a notepad or any text editor.

Write below line of code –

print(“My first Python Program”)

Save the file with name “First.py”

Note: You may also use Python Shell to write program.

Now open command prompt and change the directory to the path where “First.py” file is located.
Then type below command and press enter. You will see the text printed as output.

Python First.py

Python blog

You may notice how easy it is to write code in Python.

To get similar output in C, C++, Java and C#, we have to maintain lines of code including main function, namespaces etc. But this is not the case with Python. It is simple and easy.

Python Editor/IDE

When it comes to write program in any language, developers looks for a easy and user-friendly editor or IDE.

For Python, there are lots of IDE tool available in market.

1. IDLE – When you install Python, IDLE is installed by default. IDLE is lightweight and IDE tool specially for Python. IDLE makes it easy to write code in Python.

2. Visual Studio Code – Visual Studio Code is a free and open-source IDE tool given by Microsoft. A developer has to add extension for Python to start coding in Visual Studio Code.

3. PyCharm is another IDE tool available in market for Python. PyCharm is given by JetBrains.

For demonstration purpose, I will be using Visual Studio Code.

Sample Python Codes

1. Python Code to print a value

print(“hello world”)
Note – Python is a case sensitive programing language.
print(“”) is ok but Print(“”) will throw error.

2. Add to values in Python

x=10
y=20
print(x+y)

3. If Else Condition

x=10
y=20
if x>y: print(“X is greater and Y is smaller”)
else: print(“Y is greater and X is smaller”)

4. For Loop in Python

for i in range(10):
   print(i)
It will print the value from 0 to 10
for i in range(10,20):
    print(i)
It will print the value from 10 to 20

5. Functions in Python –

In Python, you must use a keyword def before a function name while defining the function.

def Testfunction():print(“Function Called”);
To call this function simply use the name – Testfunction()
Pass parameters to a function in Python
def Add(a, b):
   print(a+b)
  
Add(25, 25)

6. To comment a line of code-

To comment a line of code in Python you can use # before the line.Below line of code is commented.
# print(“hello world”)

7. Classes in Python-

class Test:
   def PrintFunction(self):
       print(“Hello from Class”)
obj = Test()
obj.PrintFunction()
————————–
class Test:
   def PrintFunction(self,a,b):
       print(a+b)
obj = Test()
obj.PrintFunction(10,20)

8. Array in Python

language = [“C#”,“C”, “C++”, “Python”]
print(language[0])
Print all values from an Array –
language = [“C#”,“C”, “C++”, “Python”]
for i in language:
   print(i)
Adding and Removing items from an Array –
language = [“C#”,“C”, “C++”, “Python”,“F#”]
language.pop(4) #Delete 5th element from the Array
language.append(“Visual Basic”)
for i in language:
   print(i)

That’s all for the first blog on Python.

Share this page on social platform and write your feedback below this post.

Next Blog – Exception Handling in Python with Example

Hope you like this blog. Keep following this blog

Subscribe to our YouTube channel – SharePointCafe.Net YouTube

Watch this Video of Python Programming-


You may like other blogs –

Interview Questions and Answers Series –

Leave a Comment

RSS
YouTube
YouTube
Instagram