An Introduction to Python for the Experienced Programmer, Part 2
Now continue where the first article in this series left off. If you have not read the first article in this series, please do so before reading this article.
The first article discusses installing Python, using some core functions along with manipulating strings. Now, take a look at how statements and lists work in Python.
Statements and Comparison Operators
Statements are similar across most development technologies including for Java, C++, .NET, and Python. Some of the most common statements are your conditional statements such as the if statement, for statement, and while loops. Statements using comparison operators such as >, <, <=, >=, and != are also common. For example, try executing the following in the Python console:
x = 5 print(x == 5)
The result should be True, meaning that x is equal to 4 is a true statement. If you changed the print function to be x==3, then you would get False. As with other languages, such as Java, you can use == to determine if a statement is true or false. To expand on this, try the following:
print(x >= 5) print(x <= 5) print(x > 5)
After executing these statements, you should get True, True, and False, respectively.
The If, Elif, and Else Multistatement
Probably the most common statement in any language is the if condition. The if condition can be considered a decision statement. It simply makes comparisons between values, and if the statement is true, the indented code is executed. In Python, the Elif statement is different from most other languages. Elif is simply a short-hand notation for else if. Below is an if, elif, and else construct in Python:
x = 15 if x == 15: print('x is 15') elif x == 10: print('x is 10') else: print('x is not either 15 or 10')
In Python, the above construct is referred to as a multistatement. To execute a multistatement, you may recall from the first article that you have to open a new shell and save the multistatement as a script. When saved, you can run the script with the F5 key, and the result appears back in the console window. If you run the script above, the result is 'x is 15'. You can use multiple elif statements, as many as you like, for building large if, elif, and elsemultistatements.
Most object-oriented and procedural languages have loop statements. In Python, there are only two loops: for and while. A simple while loop appears here:
x = 0 while x <= 10: print('x is',x) x+=1
After executing the script, the console should show the result of the print statement on 11 lines because you start the counter x at 0 instead of 1.
To convert the while above to a for loop in Python, do the following:
for x in range (0,11): print('x is', x)
After executing the script, the console should show the same result as with the whileloop. Notice you do not need to increment the x variable. The syntax for the Python for loop is different from that of other languages. Python, like Visual Basic .NET, uses the English language more than other languages, making the language a bit more intuitive. A difference that should also be noted between the Python for and while loops is that the second number in the range for the for loop is excluded. In other words, the range is actually up to the second number -1.
Lists in Python differ from other OO languages, such as Java and .NET, in that several different object types can be stored in the same list. In Java, for example, you have to store the same type in the list, such as for a String or Integer list array. In Python, you could store both Strings and Integers in the same list. Here’s an example of a Python list:
x = 15 helloStr = 'hello' compositeList = [x, helloStr] z=0 for z in range(0,2): print(compositeList[z])
After executing the script, the result is the values of the list printed on each line. To change a value in the list, just do the following:
compositeList  = 'new string'
Another way to print the contents of the list is by doing the following:
for valueStr in compositeList: print(valueStr)
Notice that Python still doesn't care what the type is for each item in the list. It still prints out the values, regardless.
If you do not know what the contents of the list will be at compile time, or if you want to dynamically populate the list, the list should be declared this way:
compositeList = 
To add a new value to the list, use the append function:
To remove an item from the list, use the remove function:
If you do not pass a value to the removefunction, the first item in the list is removed.
Notice the remove function can take the actual value of the item to be removed. However, you cannot use a counter or index position as a value to the remove function. Instead, use the delfunction:
A list in Python behaves like a linked list. If your remove a value, say from the third position of the list, the fourth position then becomes the third position. The list actually shrinks by one. If you were to insert an item in the third position, the value that was in the third position then becomes the item in the fourth position of the list. In this instance, the list size increases by one.
This article picked up where the first article in this series left off. Here you learned how to use different types of statements in Python, along with how to declare and manipulate lists.
The next article in this series shows you how to use Dictionaries, Functions, and Import Modules.