Author Archive

Files in Python (Part I)

When we want to read or write a file (say on your hard drive), we first must open the file. Opening the file communicates with your operating system which knows where the data for each file is stored. When you open a file, you are asking the operating system to find the file by name and make sure

Strings in Python (Part II)

A segment of a string is called a slice. Selecting a slice is similar to selecting a character: >>> s = 'Monty Python' >>> print s[0:5] Monty >>> print s[6:13] Python If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the second index, the

Strings in Python (Part I)

A string is a sequence of characters. The index indicates which character in the sequence you want to access and similar to Java indexing in python also starts from zero.You can access the characters one at a time with the bracket operator: >>> fruit = 'banana' >>> letter = fruit[1] >>> print letter a len

Iteration in Python (Part II)

For Loop Sometimes we want to loop through a set of things such as a list of words, the lines in a file or a list of numbers. When we have a list of things to loop through, we can construct a definite loop using a for statement. We call the while statement an indefinite loop because it

Iteration in Python (Part I)

Updating Variables A common pattern in assignment statements is an assignment statement that updates a variable – where the new value of the variable depends on the old. x = x+1 This means “get the current value of x, add one, and then update x with the new value.” If you try to update a variable that

Conditional Execution in Python (Part II)

In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Conditional statements give us this ability. The simplest form is the if statement: if x > 0 : print ‘x is positive’ The boolean expression after the if statement is called the

Conditional Execution in Python (Part I)

Boolean Expressions A boolean expression is an expression that is either true or false. The following examples use the operator ==, which compares two operands and produces True if they are equal and False otherwise. >>> 5 == 5 True >>> 5 == 6 False True and False are special values that belong to the type bool;

Functions in Python

In the context of programming, a function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. It is common to say that a function “takes” an argument and “returns” a result. The result is called the return value. It may not be clear why

Variables in Python

Variables are placeholders for important values. Programmers generally choose names for their variables that are meaningful. Variable names can be arbitrarily long. They can contain both letters and numbers, but they have to begin with a letter. It is legal to use uppercase letters, but it is a good idea to begin variable names with a lowercase letter. The underscore character

Arithmetic Expressions in Python

Arithmetic expressions in python are very simple and easy.  There are two types of numbers: integers (e.g. 1, -3) and decimal numbers (i.e. 3.45) in python there are two corresponding data types int() and float () respectively. # Arithmetic expressions - numbers, operators, expressions print 3, -1, 3.14159, -2.8 # numbers – two types, an