PythonAnyWhere is a real cool place to create your Python web application – absolutely free – and share with the world.
And I have very recently created a simple Python Tutorials for Beginners
up there. The design is quite simple and I think not attractive. It is for the beginners. It looks like this:
I would also like the DropBox share link of the file and taste how it works!
The HTML Design at DropBox
‘While loop’ is the simplest form of loop in Python. But you need to understand it properly. Otherwise it can end up in eating up your memory running the infinity loop. Usually most of the jobs are done by ‘for loop’. But in some special cases, you need to use ‘while loop’. A basic understanding is important.
In plain English we often say – ‘While it is true it keeps on running. While it is not true it stops.’ Logically same thing happens here. While a statement is true, the process is going on. You need a mechanism to stop that process. That is important. Otherwise that statement will eat up your memory.
Consider this code:
b = 1
while b < 50:
b = b + 1
What does it mean? It means, the statement ‘b is less than 50’ is true until the suite or block of code is true inside it. Inside the block we wrote ‘b = b + 1’ and before the beginning of the while loop we defined the value of b as 1.
So in each step b progresses by adding 1 in its value and finishes at 49. In the output you will get 1 to 49.
Continue reading “Loops in Python”
Good VS Bad Game
So far we have learned many things. I hope you have written the codes and tested them and it executed perfectly. Now the time has come to write a simple game in Python. It is a game called “Good Vs Bad”. The game is simple. But as a beginner you may find this code a bit longer. Write them down. Try to add more features.
If you are in Linux environment, save this file as ‘good-vs-bad.py’ and change the file executable by running this command:
Sudo chmod +x good-vs-bad.py
And then run it on your terminal like this:
If you are in Windows, run the IDLE and save the file as ‘good-vs-bad.py’. Press F5 and play the game.
Continue reading “Write Your Own Game in Python3”
If you are a complete beginner, you probably for the first time hear about “Object Oriented Programming and Class.” Let us give a brief introduction to Object Oriented Programming (OOP).
10.1 – Object Oriented Methodology
It is based on real world programming. An object is a representation of a real world entity. If there is an object, there must be a class or blueprint behind it. In that class the behaviour of that object is designed or described in detail. These details consist of all the properties and actions that the object performs. There could be many types of objects coming from different classes and they might have relationships. It could be very complicated but you can always break those objects from one another and make some changes. The advantage of Object Orientation is that when you work on a part of a big, complicated project the other part remains unaffected. Our goal is simple. We want to join different objects to create big, complicated software. At the same time we want to make the relations of those objects as loose as possible.
A car object is built of many other objects like tyre, wheel, engine, accelerator etcetera. If you get a flat tyre does the engine stop? It is interrelated and depends on one another. But finally you can work on them individually without affecting other. That is Object Orientation.
Consider an object ‘GoodHuman’. This object must be different from another object ‘BadHuman’. Both come from ‘Human’ class. Now these two objects might have interrelationships and data interactions. Can you imagine how many kinds of properties and methods are there in ‘Human’ class? It could be very complex. Imagine a situation where a ‘BadHuman’ does something ugly. At the same time a ‘GoodHuman’ does some good thing. Whoever does whatever thing the life goes on and that is also Object Orientation.
The Foundation of Object Orientation
Object Orientation is a type of methodology used for building software applications. An Object Oriented program consists of classes, objects and methods. The Object Oriented methodology in software development revolves around a single concept called the object. You can develop software by breaking the application into component objects. These objects interact with each other when the whole application is put together. An object is a combination of messages and data. The object receives and sends messages and those messages contain data or information.
You (an object) interact with your Television (another object) via messages sent through a remote controller (another object).
Consider another real world example of a football. A football has a boundary. It has a specific defined property like bouncing. You can direct or apply few specific actions by kicking it or throwing it.
An object has a state. It may display behaviour. It has a unique ID.
The difference between an object and class is subtle but important. Whereas a class is an abstract concept, an object is a concrete entity. From a class objects with specific properties can be created or instantiated. That is why an object is often called an instance of a class.
One of the major feature of Object Oriented Programming is ‘Polymorphism’. Polymorphism is the capability of something to assume different forms. In Object Oriented Programming polymorphism is the property that a message can mean different things depending on the objects receiving it. The message ‘Accelerate’ means one thing if it sent to an object “OldCar”. But it means different thing to if it is sent to the object “NewCar”. It is a natural concept that can be applied to objects. It also means that similar objects often accept the same message but do different things.
Consider a web page. It is an object. There are billions of such objects around us. When you send a request to an object like web page you actually apply a verb “GET” to a noun “WebPage”. Now every “WebPage” object does not behave the same way when the “GET” verb is applied. Someone open up a PDF file, someone simply shows some texts and pictures and someone may harm your computer. When you double-click a file. It may execute if it is an executable file. Or it may opens up in a text editor if it is a text file. The message is same. That is “Double Click”. But the behaviour displayed by the file object depends on the object itself.
This is Polymorphism. You will learn it heart as you progress through this chapter.
The advantage of Python classes is that it provides all the standard features of Object Oriented Programming. It has the class inheritance mechanism. That allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name. Objects can contain arbitrary amounts and kinds of data.
Finally remember, in Python everything is an object. It means there is an abstraction or encapsulation behind it. Your first job is understand that avstraction and the final job is create your own abstraction.
You can do some funny staff with Python. it simply amuses.
Issue this command in your Linux terminal:
sudo apt-get install python3-tk
It will install the necessary packages. Now it is time to write some code:
pen = turtle.Pen()
colors =['red', 'orange', 'yellow', 'blue', 'pink', 'white', 'purple', 'green', 'light blue']
sides = 8
for x in range(360):
pen.pencolor(colors[x % sides])
pen.forward(x * sides / 3 + x)
pen.left(360 / sides + 1)
pen.width(x * sides / 100)
If you run this program, you will see something like this:
Python with turtle module
Normally when we use for loop and range() method in Python it stops at just before the range we mentioned.
It looks like :
for i in range(10):
And it reads from 0 to 9 and stops.
Basically through the range() method of Python, we pass three variables – start, stop and step. In between these three variables, by default the values of ‘start’ and ‘stop’ are pre-set. ‘start’ is 0 and ‘stop’ is 1. So it starts from 0 and each step it iterates 1 step and stops at 9. In it the ‘stop’ value remains inconclusive.
But we can make the ‘stop’ value conclusive by the following method:
Continue reading “Function generator in Python”
How we can catch exceptions in Python?
Here is a short example how we can do it.
Continue reading “Exception in Python”
In Python string is an immutable object and can be written within double quotes or single quote.
Continue reading “Strings in Python”
In Python there are two kind of numbers. One is integer and the other is float. We have in built methods in Python that can change an integer to float and change float to integer.
Continue reading “Numbers in Python”
In Python everything is an object.
To start with, you need to remember few things:
1) Variables, functions and even code are object.
2) Every object has an ID, type and value.
Continue reading “Variable, Object and Value”