Posts

Python 3.4 - Indexing and slicing

Image
  Indexing and slicing   Indexing and slicing are two approaches, used to access or retrieve elements of a sequence datatype such as string, list, tuple etc. Indexing Indexing is used to access a single element at a time. We can use index number of an element of a sequence to access that particular element. Every element in a sequence such as string, list, tuple, bytes etc. has an index number. An index number starts from 0 (zero) in Python. So an index number is used to access an element . For example, in a string “india” the index number of first element ‘i’ is 0, the index number of second element ‘n’ is 1, the index number of third element ‘d’ is 2 and so on. The index number of last element ‘a’ is 4. So remember that, the indexing starts from 0 and ends at n-1 in a sequence of n length. In our case, string “india” has length 5 i.e. 5 characters or elements. So the indexing started from 0 and ended at 4 (n-1 or 5-1). Same concept applies to all the other sequences. S...

Python 3.3 - Datatypes in Python

Image
  Datatypes in Python As we have already discussed that data type refers to the type of data. So datatype implies that what type of data we can store in our Python program. There are two categories of datatypes available in Python – Built-in data types and User defined data types Built-in data types The data types which are pre-defined in Python are called built-in data types. These data types have already been developed by Python developer team which we can use directly. So we do not need to do anything in it. Following are built-in data types – ·          None ·          Numbers ·          Boolean ·          Sequences ·          Sets ·          Dictionary   These datatypes are further classified in some more datatypes...

Python 3.2 - Comments in Python

  Comments Comments are always used for programmer’s convenience in any programming language. A comment is used to describe the features of a program . When we write program then we can write comments for classes, functions, statements etc. Comments help to understand the code easily. For example, you are working in a software company. You wrote a program of ten thousand lines. After some time, you left the job and a new programmer joined the company. Now the new programmer opens the file which you had written. So before proceeding he has to understand the code first so that he can continue. Now, if you used proper comments in your program then the new guy can understand it very easily and it will take very less time. But if you did not use proper comments in your program then the new programmer will have a lot of difficulties understanding what and why you have written this. So it will waste the time because the new programmer has to understand the whole lengthy code of 10000 li...

Python 3.1 - Variables, Identifiers and Keywords

Image
Data is very important in every field so it should be handled very carefully. Almost all applications that we see in digital world, need data. Best examples are Facebook and YouTube which contain an infinite amount of data. Every day, millions or billions of data is uploaded and downloaded on\from Facebook and YouTube such as images, videos, user names, passwords, date of birth, age, e mails etc. Every data also has some type. For example, when we talk about a user’s age such as ‘Age=28’ then 28 is called the data and ‘number’ is the type of data since 28 is a number. When we write programs then we perform operations on data. Data type refers to the type of data . Type of data must be known before storing data into system because if data type is not known then it may lead to various problems such as undesirable output. These are the common types of data that we use generally. There are many more data types in Python that we will discuss later in this chapter. In pro...

Python 2.4 - Points to remember

  Points to remember ·          A Python program file is always saved with “.py” extension. ·          Python programs can be written in either interactive mode or script mode. ·          Interactive mode should be used only for practise. ·          Script mode should be used to write actual Python programs. ·          Python’s command line window and Python’s IDLE window can be used as interactive mode. Python’s IDLE window and any Text Editor can be used as script mode. ·          I recommend Python’s IDLE window to write programs for new learners. ·          Python has a very vast library used to develop variety of applications. ·          Python is...

Python 2.3 - Executing Python program

Image
Executing Python program There are three ways to execute Python programs. ·          Using Python’s IDLE window ·          Using Python’s command line window ·          Directly from System prompt The first two methods are called interactive modes where we can type the program one line at a time and the PVM executes it immediately. Remember we can use Python’s IDLE window as script mode too . And the last one is called script mode where we ask the PVM to execute our program after typing the entire program. Note : In interactive mode, one statement is executed at a time. And in script mode, complete program is executed. 1.    Using Python’s IDLE window IDLE (integrated development environment or integrated development and learning environment) is a graphical interface i.e. it provides graphical interface to the user. We can also modify the fo...