Posts

Showing posts from 2018

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.

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>

Code Challenge 2

Code Challenge: In the given code, add the text that will display the text, Hello World! print() Well, this is a simple one, but you gotta learn everything.

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.

Code Challenge 1

Code Challenge: Write suitable arithmetic expressions for the displayed descriptions. This is meant as a practice exercise for you to get used to some basic syntax in IDLE. So there may be several correct responses for some of the descriptions. 1. Sum of 21 and 62 2. Difference between 43 and 17 3. 36 mod 5 4. Sum of 6 and 4 multiplied by 10 5. 23 to the 5th power 6. An expression that will result in 19 and which includes multiplication 7. Sum of 177 mod 4 and 223 mod 5 8. Difference between 3rd power of 14 and 3rd power of 13 9. An expression that will result in 0, which does not include multiplication and contains at least 3 arithmetic operators 10. An expression that includes all six arithmetic operators, addition (+), subtraction (-), multiplication (*), division (/), modulo (%) and power (**) Post your answers in the comments.

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

Welcome

Python Tutorial for Beginners Hey, Welcome to the blog. This is the complete Python Tutorial from scratch. In the blog, you will learn the basic concepts of  Introduction to Python ,  Using Variables in Python ,  Basics of Programming in Python ,  Principles of Object-oriented Programming (OOP) ,  Connecting to SQLite Database ,  Developing a GUI with PyQT ,  Application of Python in Various Disciplines   and more in a fun-filled and interactive manner. You will also do some assignments and a project for practical understanding of the concepts. Below you will find the details on how to pursue this training program by making the best use of your time How much time should I spend on this training? You will need to spend at least 1-2 hours per day. The following chart shows a break-up of the timeline for the training. Try to pace your learning accordingly so as to finish the program comfortably on time. This is the average time...