Skip to main content

Posts

Showing posts from March, 2018

Files I/O

Printing to the Screen: The simplest way to produce output is using the  print  statement where you can pass zero or more expressions separated by commas. This function converts the expressions you pass into a string and writes the result to standard output as follows − print " python is really a great language," This produces the following result on your standard screen - Python is really a great language, Reading Keyboard Input: Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. These functions are − raw_input input The  raw_input  Function: The  raw_input([prompt])  function reads one line from standard input and returns it as a string (removing the trailing newline). Example: str = raw_input("Enter your input: ") print "Received input is: ", str This prompts you to enter any string and it would display same string on the screen. When I typed...

Conditional Statements in Pyhton

        In programming and scripting the languages, conditional statemnets are used for different computations or actions o whether a condition evaluates to true or false. (In Python it is written as True or False). usually, the condition uses comparisions and arithematic expressions with  variables. These expressions are evaluated to the Boolean values True or False. The statements for the decision making are called conditional statements. They are also called as conditional expressions or conditional constructs. Types of Conditional Checking Statements in Python:         Python programming language provides following types of conditional checking statements. if Statement if....else Statement if....elif....else Statement Nested if Statement Rules: The colon (:) is required at the end of the condition. The body of the if statement is indicated by the identification. In Python, four spaces are used for indenting. All the...

List Loop

Looping in a list is used to access every element in the list. There are two tyes of looping. Normal for-in loop as specified in line 8 & 9. Range of length loop as specified in line 13 & 14. Try Yourself: #List looping example   colors_list = ["red", "green", "yellow", "violet"] months_list =  ["Jan", "Feb", "Sep", "Mar"]   print "Colours in the list: " # Normal Looping for i in range(len(colors_list)):     print(colors_list[i])   # Range Looping print "Months in the list: " for month in months_list:     print(month)         The lines 8 & 9 are like normal for loop; where each element in the list are accessed using the index value. The lines 13 & 14 are special case; where every elements in the list of months_list is taken. The iteration stops when all the elements in the list are exhausted.

Python Modules

Introduction:         Module is a file that is contain Pyhton definitions and statements. It defines functions, classes and variables and includes runnable code also. Functions are group of code and Modules are group of functions. Steps: Create a python file with one or more functions and save it with .py  extension. Include the import  statement.  The import has the following syntax: import [module1,module2,........,module N] When the intrepeter comes across an import statement, it imports the module if the module is already present in the search path. A search path is a list of directories that the interpreter searches before importing a module. Module is loaded only once, even if multiple imports occur. Using the module name the function is accessed using dot(.) operation. The from....import Statement: For a module involving hundreads of functions, from.....import Statement is recommended to save loading time Syntax: ...

LISTS in Python

One of the basic data structures in Python is sequences. There are six built-in types of sequence - strings, Unicode strings, lists, tuples, buffers, and xrange objects. The most common are lists and tuples.  A list is an ordered set of values, where each value is identified by an index. The values in the list are known as elements. Each element of a sequence is assigned with a number - its position or index. The first index is zero, second is one and so forth. Creation and Accessing List: The list can be created by putting values separated by commas between square brackets []. List is one of the most freuently used and very versatile data type used in Python.  Syntax for List:                <List_name> = [<Value_1>,<Value_2>,........,<Value_N>] NOTE:  Nested lists are also acceptable. List Operations: Two operations + and * is allowed in list. + for concatenation ...