Posts

Showing posts with the label tutorial

Identifiers

In a Python program or any other programming language, it is common to use programming elements such as variables, keywords, functions, classes, modules and packages. These elements have names which are predefined in the language, or can be used by the programmer at the time of writing code. These names are called Identifiers . Examples : variables    -----     _bookname, num1 keywords   -----      if, while, yield functions    -----      print(), int(), len() classes        -----      Complex, Exception modules     ------     Calender, random packages    -----       Pillow, tkinter An identifier can only start with an alphabet that may be  abcdefghijklmnopqrstuvwxyz                                           ...

Syntax of Python

What is Syntax ? Computers are machines that can interpret your instructions only if you type them in the exact format that the computer expects. This expected format - the spelling, formatting and grammar - is called the 'Syntax'. For example : #This function divides two numbers def divide(x,y):                                                            return x/y                                                                                                                                    ...

Comparing Python 2.x and Python 3.x

Although development of Python started in the late 80’s, Python 1.0 was published in January 1994. Some important features like cycle-detecting garbage collector and Unicode support were added in Python 2.0 which was released in October 2000. Python 3.0 was released in December 2008. It was designed to rectify certain flaws in earlier version. The guiding principle of Python 3 was: "reduce feature duplication by removing old ways of doing things". Python 3.0 doesn’t provide backward compatibility. That means a Python program written using version 2.x syntax doesn’t execute under python 3.x interpreter. Since all the Python libraries are not fully ported to Python 3, Python Software Foundation continues to support Python 2. The foundation has recently announced it will discontinue Python 2 support by year 2020. Some of the obvious differences between the Python 2.x variants and Python 3.x variants are displayed below. 1. Print Python 2.x It is not mandatory to use pa...

Features of Python

Let us take a closer look at some of the salient features of Python. 1. Enhanced Readability In Python, uniform indents are used to delimit blocks of statements instead of curly brackets like in C, C++ and Java. This enhances readability. In  C, C++, Java if (X%2==0) { if (x%3==0)    System.out.println("divisible by 2 and 3"); else    System.out.println("divisible by 2 not divisible by 3"); } else { if (x%3==0)    System.out.println("divisible by 3 not divisible by 2"); else    System.out.println("not divisible by 3 nor by 2"); } In Python if num%2 == 0:    if num%3 == 0:        print ("Divisible by 3 and 2")    else:        print ("Divisible by 2 not divisible by 3") else:    if num%3 == 0:        print ("Divisible by 3 not divisible by 2")    else:       ...

What is Python ?

Python is a popular "High Level Programming Language" Lets learn how it came about and what makes it so popular. According to the TIOBE (The Importance Of Being Earnest) community index, Python is currently ranked no. #4. Which is pretty high than the rest. The index is calculated taking various search engine results into account. Python is being used in various application areas such as : Web Development Scientific Computing Data Analytics and so on.... Python supports multiple programming paradigms including : Imperative Object Oriented Procedural Functional programming styles. It is also an Open Source Language. Many developers contribute towards its development. However, Guido van Rossum (BDFL) is the principle author. The Python community has affectionately given him the title of Benevolent Dictator for Life. The Design Philosophy of Python is summarized in a collection of 20 principles.Some of which are : Beautiful is better than ugly. ...

Using Variables

Image
In the last post, we saw how to print Hello World using the print() command. Now suppose you want to print your name with Hello World. Sounds interesting. But for that, you have to save your name at some location, that is where variables are used. Lets see an example : As you can see, 'anyname' is a variable used to take input from user. The input() command is used to take input from user. The user inputs his/her name and it is stored in 'anyname' Next is printing the name. So, in print() command, you can see  print("Hello World! This is", anyname) 'Hello World! This is' is written within inverted commas but not 'anyname' This is because 'anyname' is a variable that contains certain value inside it but the rest of it is a string. When the program is run, the output is Hello World! This is <name entered by user>

Simple Hello World Program Tutorial

Image
Now that you know how to use IDLE, lets begin with writing our first program. The Hello World Program ( We usually do this in every language 😎) Lets begin. Fire up IDLE. Go to File>New File. Once it opens up, write the code in the new file. print("Hello World!") Now click on Save As and save the file with .py extension. See the image below. Go to Run> Run Module or Press F5 to check whether the code is working or not. It works.

IDLE

Image
After installing Python, we need to start using it to <CODE>. In this post, we will learn how to use the Python Interpreter IDLE, and how to print "Hello World" using IDLE. So, lets get started. The Python Interpreter we are using here is IDLE (Integrated Development Environment) . Click on START > IDLE It will launch the IDLE Interpreter which looks like this : You can write appropriate python codes after the >>> For example :  The above image shows illustration of +, -, *, /, % These are the basic arithmetic operators, addition (+), subtraction (-), multiplication (*), division (/), modulo (%) and power (**) You can practice with the Code Challenge in the next post.

Installation Of Python

Image
Windows  Visit https://www.python.org and download Python for Windows. Direct Link (Python v3.6.5) Installation process is simple like every other application. Guide for installing python is given below : 1. Open the installer 2. Check Add Python 3.6 to PATH and click on Customize Installation. 3. Click Next on Optional Features. 4. Change the install location to C:\Python rather than C:\Users\AppData\... and click on Next. (You can check Install or All Users if you want to.) 5. Wait for installation to complete successfully. After Python is installed, Open Start Menu and search for IDLE. Linux  Using the Package Manager 1.    If you are using Ubuntu Linux, at the command prompt type: $  sudo  apt-get install python3 2.    If you are using Fedora, at the command prompt type: $  sudo  yum install python3 3.    The most recent version of Python 3 wil...