Posts

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. ...

Summary

You have reached the end of this topic. In this topic, you learned how to: Use the Python interpreter, IDLE. Write a basic program to display the text, Hello World. You also had a chance to practice some code using the Code Challenges. Here are some ideal solutions for these code challenges. Code Challenge 1 Problem Statement Write suitable arithmetic expressions for the displayed descriptions. Ideal Solution 1. Sum of 21 and 62 21+62 2. Difference between 43 and 17 43-17 3. 36 mod 5 35%5 4. Sum of 6 and 4 multiplied by 10 (6+4)*10 5. 23 to the 5th power 23**5 6. An expression that will result in 19 and which includes multiplication 4*5-1 7. Sum of 177 mod 4 and 223 mod 5 (177%4)+(223%5) 8. Difference between 3rd power of 14 and 3rd power of 13 14**3-13**3 9. An expression that will result in 0, which does not include multiplication and contains at least 3 arithmetic operators (27%11)+10-15 10. An expression that includes all six a...

Code Challenge 3

Code Challenge: Conduct this experiment. Run the following program to see what happens. Note your observations so that you can revisit them when you learn about data types. a = input("Enter any number: ") b = input("Enter any number: ") print(a+b) Clue: You will find the answer very surprising... a and b are not added but joined or concatenated. This is because by default, an input is always treated as non-numeric in Python even if it contains digits. You will learn all about numeric and non-numeric datatypes in the next module.